This function is only an approximation, and it is not nearly as fast as the normal HSV-to-RGB conversion. See extended notes in the .h file.
| 549 | // nearly as fast as the normal HSV-to-RGB conversion. |
| 550 | // See extended notes in the .h file. |
| 551 | CHSV rgb2hsv_approximate( const CRGB& rgb) |
| 552 | { |
| 553 | fl::u8 r = rgb.r; |
| 554 | fl::u8 g = rgb.g; |
| 555 | fl::u8 b = rgb.b; |
| 556 | fl::u8 h, s, v; |
| 557 | |
| 558 | // find desaturation |
| 559 | fl::u8 desat = 255; |
| 560 | if( r < desat) desat = r; |
| 561 | if( g < desat) desat = g; |
| 562 | if( b < desat) desat = b; |
| 563 | |
| 564 | // remove saturation from all channels |
| 565 | r -= desat; |
| 566 | g -= desat; |
| 567 | b -= desat; |
| 568 | |
| 569 | //Serial.print("desat="); Serial.print(desat); Serial.println(""); |
| 570 | |
| 571 | //uint8_t orig_desat = sqrt16( desat * 256); |
| 572 | //Serial.print("orig_desat="); Serial.print(orig_desat); Serial.println(""); |
| 573 | |
| 574 | // saturation is opposite of desaturation |
| 575 | s = 255 - desat; |
| 576 | //Serial.print("s.1="); Serial.print(s); Serial.println(""); |
| 577 | |
| 578 | if( s != 255 ) { |
| 579 | // undo 'dimming' of saturation |
| 580 | s = 255 - fl::sqrt16( (255-s) * 256); |
| 581 | } |
| 582 | // without lib8tion: float ... ew ... sqrt... double ew, or rather, ew ^ 0.5 |
| 583 | // if( s != 255 ) s = (255 - (256.0 * sqrt( (float)(255-s) / 256.0))); |
| 584 | //Serial.print("s.2="); Serial.print(s); Serial.println(""); |
| 585 | |
| 586 | |
| 587 | // at least one channel is now zero |
| 588 | // if all three channels are zero, we had a |
| 589 | // shade of gray. |
| 590 | if( (r + g + b) == 0) { |
| 591 | // we pick hue zero for no special reason |
| 592 | return CHSV( 0, 0, 255 - s); |
| 593 | } |
| 594 | |
| 595 | // scale all channels up to compensate for desaturation |
| 596 | if( s < 255) { |
| 597 | if( s == 0) s = 1; |
| 598 | fl::u32 scaleup = 65535 / (s); |
| 599 | r = ((fl::u32)(r) * scaleup) / 256; |
| 600 | g = ((fl::u32)(g) * scaleup) / 256; |
| 601 | b = ((fl::u32)(b) * scaleup) / 256; |
| 602 | } |
| 603 | //Serial.print("r.2="); Serial.print(r); Serial.println(""); |
| 604 | //Serial.print("g.2="); Serial.print(g); Serial.println(""); |
| 605 | //Serial.print("b.2="); Serial.print(b); Serial.println(""); |
| 606 | |
| 607 | fl::u16 total = r + g + b; |
| 608 |
no test coverage detected