FastLED: rgb2hsv_approximate returns completely wrong color
Hello,
it feels like I found a bug in rgb2hsv_approximate. For most colors it approximates correctly but for some it is completely wrong.
Sample code:
#include "FastLED.h"
void setup() {
Serial.begin(115200);
Serial.println("");
CRGB color1 = CRGB(255,153,0);
Serial.println("rgb: " + String(color1.red) + "," + String(color1.green) + "," + String(color1.blue));
CHSV color2 = rgb2hsv_approximate(color1);
Serial.println("hsv: " + String(color2.hue) + "," + String(color2.sat) + "," + String(color2.val));
}
void loop() {
}
Serial output:
rgb: 255,153,0
hsv: 122,255,255

Paint.net says rgb(255,153,0) results in hsv(36,100,100) for 360 degrees which would be hsv(26,255,255) for FastLED if I’m not mistaken. Instead rgb2hsv_approximate returns hsv(122,255,255) in FastLED which is not even closely orange-ish (it’s green).
The code runs on an ESP8266.
Bug or am I just missing something?
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 15 (4 by maintainers)
This bug still exists:
Console Output:
“Request Col_Set” comes from the calling function from before this code, this is printing purely the color string. this string is than converted into an actual number via strtol();
Color codes are OK until the rgb2hsv_approximate. This happens only in for those orange-ish colors. Also, colors close to Hue = 255 are a bit “very” approximated (it returns red all the time untill it suddenly jumps to a purple-ish color)
Edit; I implemented the “rgb2hsv_precise” from @5chmidti and can confirm this works how I expected it to work 😃 Thank you!
Edit2: Out of curiosity I did some measurements. First a FastLED “rgb2hsv_approximate” and than a “rgb2hsv_precise”. At least on the ESP32 the “precise” implementation seems to be 2 times faster than the approximate??
I’ve been looking into this and while I haven’t ‘fixed’ any of the existing functions I’ve added more color accurate conversion functions in the pr linked above.
More info in the pr, but here are the conversions from this thread rerun with the new functions:
and from the first post: