color-hsl

xod/color/color-hsl

Constructs a color value from hue, saturation, and lightness values (HSL model)
color-hsl
@/color-hsl
Constructs a color value from hue, saturation, and lightness values (HSL model)
Hnumber
0 is for red, 0.33 for green, 0.66 for blue, and 0.99 is for red again. Some systems use degrees for the hue component. The value of 1.0 corresponds to 360° of such systems. When out out of [0; 1) range only the fractional part is taken into account
Snumber
Saturation. Should be in the range [0, 1]. 0.0 corresponds to fully-gray shade and 1.0 to saturated color shade. Values out of the range are truncated to 0 or 1
Lnumber
Lightness. Should be in the range [0, 1]. 0.0 corresponds to black; 0.5 to pure color; 1.0 to white. Values out of the range are truncated to 0 or 1
color-hsl
OUT
H
S
L
OUT@/color

C++ implementation

node {
    float hueToRgb(float v1, float v2, float vH) {
      if (vH < 0) vH += 1.0f;

      if (vH > 1.0f) vH -= 1.0f;

      if ((6.0f * vH) < 1.0f) return (v1 + (v2 - v1) * 6.0f * vH);

      if ((2.0f * vH) < 1.0f) return v2;

      if ((3.0f * vH) < 2.0f) return (v1 + (v2 - v1) * ((2.0f / 3.0f) - vH) * 6.0f);

      return v1;
    }

    void evaluate(Context ctx) {
      float h = getValue<input_H>(ctx);
      float s = getValue<input_S>(ctx);
      float l = getValue<input_L>(ctx);

      uint8_t r, g, b;

      if (s == 0) {
        r = g = b = ceil(l * 255.0f);
      } else {
        float v2 = (l < 0.5) ? (l * (1.0f + s)) : ((l + s) - (l * s));
        float v1 = 2.0f * l - v2;

        r = round(255.0f * hueToRgb(v1, v2, h + (1.0f / 3.0f)));
        g = round(255.0f * hueToRgb(v1, v2, h));
        b = round(255.0f * hueToRgb(v1, v2, h - (1.0f / 3.0f)));
      }

      typeof_OUT obj = {r, g, b};
      emitValue<output_OUT>(ctx, obj);
    }
}