HSL to RGB color conversion

后端 未结 21 3090
忘了有多久
忘了有多久 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:13

    I needed a really light weight one, Its not 100%, but it gets close enough for some usecases.

    float3 Hue(float h, float s, float l)
    {
        float r = max(cos(h * 2 * UNITY_PI) * 0.5 + 0.5, 0);
        float g = max(cos((h + 0.666666) * 2 * UNITY_PI) * 0.5 + 0.5, 0);
        float b = max(cos((h + 0.333333) * 2 * UNITY_PI) * 0.5 + 0.5, 0);
        float gray = 0.2989 * r + 0.5870 * g + 0.1140 * b;
        return lerp(gray, float3(r, g, b), s) * smoothstep(0, 0.5, l) + 1 * smoothstep(0.5, 1, l);
    }
    

提交回复
热议问题