14. HSLuv colorspace

HSLuv is a developer-friendly colorspace. I read a Blog Postabout it and knew I had to have it. Luckily, a C implementation exists with a permissive license, and I was able to easily drop it into this codebase.

The HSLuv colorspace converters (will) exist as 2 janet functions: monolith/rgb2hsluv and monolith/hsluv2rgb.

Both will return values as arrays.

RGB values are expected to be in range 0-1.

HSL values expect the following ranges:

H (Hue) is between 0 and 360.

S (Saturation) is between 0 and 100.

l (Lightness) is between 0 and 100.

<<gfx_aux_includes>>=
#include "hsluv/hsluv.h"
<<gfx_janet_entries>>=
{"monolith/rgb2hsluv", j_rgb2hsluv, NULL},
{"monolith/hsluv2rgb", j_hsluv2rgb, NULL},
<<gfx_janet>>=
static Janet j_hsluv2rgb(int32_t argc, Janet *argv)
{
    double r, g, b;
    double h, s, l;
    JanetArray *a;

    janet_fixarity(argc, 3);

    h = janet_unwrap_number(argv[0]);
    s = janet_unwrap_number(argv[1]);
    l = janet_unwrap_number(argv[2]);

    a = janet_array(3);

    r = 0;
    g = 0;
    b = 0;

    hsluv2rgb(h, s, l, &r, &g, &b);

    janet_array_push(a, janet_wrap_number(r));
    janet_array_push(a, janet_wrap_number(g));
    janet_array_push(a, janet_wrap_number(b));

    return janet_wrap_array(a);
}

static Janet j_rgb2hsluv(int32_t argc, Janet *argv)
{
    double r, g, b;
    double h, s, l;
    JanetArray *a;

    janet_fixarity(argc, 3);

    r = janet_unwrap_number(argv[0]);
    g = janet_unwrap_number(argv[1]);
    b = janet_unwrap_number(argv[2]);

    a = janet_array(3);

    h = 0;
    s = 0;
    l = 0;

    rgb2hsluv(r, g, b, &h, &s, &l);

    janet_array_push(a, janet_wrap_number(h));
    janet_array_push(a, janet_wrap_number(s));
    janet_array_push(a, janet_wrap_number(l));

    return janet_wrap_array(a);
}



prev | home | next