Fade through more more natural rainbow spectrum in HSV/HSB

Adam Haile picture Adam Haile · Mar 2, 2011 · Viewed 7.3k times · Source

I'm trying to control some RGB LEDs and fade from red to violet. I'm using an HSV to RGB conversion so that I can just sweep from hue 0 to hue 300 (beyond that it moves back towards red). The problem I noticed though is that it seems to spend far to much time in the cyan and blue section of the spectrum. So I looked up what the HSV spectrum is supposed to look like, and found thisL

enter image description here

I didn't realize that more than half the spectrum was spent between green and blue.

But I'd really like it to look much more like this:

enter image description here

With a nice even blend of that "standard" rainbow colors. I'd imagine that this would end up being some sort of s-curve of the normal hue values, but am not really sure how to calculate that curve.

An actual HSV to RGB algorithm that handles this internally would be great (any code really, though it's for an Arduino) but even just an explanation of how I could calculate that hue curve would be greatly appreciated.

Answer

jobevers picture jobevers · Jul 30, 2018

FastLED does a a version of this: https://github.com/FastLED/FastLED/wiki/FastLED-HSV-Colors

Fast LED HSV

HSLUV is another option: http://www.hsluv.org/. They have libraries in a bunch of different languages.

HSLUV

Also, this is an interesting technique: https://www.shadertoy.com/view/4l2cDm

const float tau = acos(-1.)*2.;

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = fragCoord.xy / iResolution.xy;

    vec3 rainbow = sqrt( //gamma
        sin( (uv.x+vec3(0,2,1)/3.)*tau ) * .5 + .5
    );

    fragColor.rgb = rainbow;
}

enter image description here

Also see: https://en.wikipedia.org/wiki/Rainbow#Number_of_colours_in_spectrum_or_rainbow for more info.