Hi, in my project I am always working with Palettes for every effect to

Hi,

in my project I am always working with Palettes for every effect to get the flexibility to influence all effects with changing and blending Palettes. As a standard I just use the rainbow palette so it is kind of not visible that I use them, since each input hue maps to the same output hue.

However palettes have an inbuilt saturation, so with the getColorFromPalette functions I always get fully saturated colors back (in the rainbow case) or I get those saturations back that are part of the palette in other cases.

What I would like is a chance to desaturate the colors in the same way, as I can also scale down the brightness already within the getColorFromPalette functions.

I looked through the code and it seems easy to add another scale8_video to the ones using CHSV pixels. A new parameter could just be added to the function call, defaulting to 255, so no change to the interface as such.

However, for CRGB its much different, as the saturation is not known. I checked the rgb2hsv_approximate function, but as seen in the comments it takes a lot of time and is focused on getting back the hue and not so much the saturation, in fact it does not work well for already low-saturated colors.

So what I am thinking about is not getting HSV back but just apply some destauration to an RGB so maybe some of the complicated steps in rgb2hsv_approximate can be left out.

Do you think that starting to work on that even makes sense? If yes, I might try to better understand this color conversion to come up with something. Or maybe there is an easier way that I am not seeing.

Thanks @Sebastian_Stuecker for the great question – and for reading through the source code and comments! Always good to know that folks are reading them.

So to desaturate a CRGB color, here’s a pretty basic way to do it:

CRGB desaturate( CRGB c, uint8_t desat)
{
// first scale down existing RGB channels
uint8_t saturation = 255 - desat;
c.nscale8_video( saturation);

// now add some white to the color,
// add ‘desat’ on each RGB channel
CRGB addedWhite = CRGB( desat, desat, desat);
c += addedWhite;;

// done
return c;
}

This operation should be pretty fast. Does this help?

(Update: fixed a typo!)

Thats very interesting with just adding white. What a trivial way. I googled for desaturation algorithms and found many which mostly focus on grayscale but this is more simple than anything I found yet. I will definetly try that out and let you know. Thank you!