HSL to RGB color conversion

后端 未结 21 2697
忘了有多久
忘了有多久 2020-11-22 01:59

I am looking for a JavaScript / PHP algorithm to convert between HSL color to RGB.

It seems to me that HSL is not very widely used so I am not having much luck search

21条回答
  •  你的背包
    2020-11-22 02:22

    Here's a fast, super-simple, branchless version in GLSL:

    vec3 hsl2rgb( vec3 c ) {
        vec3 rgb = clamp(abs(mod(c.x*6.0 + vec3(0.0, 4.0, 2.0), 6.0)-3.0)-1.0, 0.0, 1.0);
        return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));
    }
    

    Doesn't get much shorter than that ~


    Link to the original proof-of-concept: https://www.shadertoy.com/view/XljGzV

    (Disclaimer: not my code!)

提交回复
热议问题