LYGIA Shader Library

xyz2lab (lygia/color/space/xyz2lab)

Converts a XYZ color to Lab color space.

Use:

xyz2rgb(<vec3|vec4> color)

Check it on Github


#ifndef FNC_XYZ2LAB
#define FNC_XYZ2LAB
vec3 xyz2lab(in vec3 c) {
    vec3 n = c / vec3(95.047, 100.0, 108.883);
    vec3 c0 = pow(n, vec3(1.0 / 3.0));
    vec3 c1 = (7.787 * n) + (16.0 / 116.0);
    vec3 v = mix(c0, c1, step(n, vec3(0.008856)));
    return vec3((116.0 * v.y) - 16.0,
                500.0 * (v.x - v.y),
                200.0 * (v.y - v.z));
}

vec4 xyz2lab(in vec4 c) { return vec4(xyz2lab(c.xyz), c.w); }
#endif

Use:

xyz2rgb(<float3|float4> color)

Check it on Github


#ifndef FNC_XYZ2LAB
#define FNC_XYZ2LAB
float3 xyz2lab(in float3 c) {
    float3 n = c / float3(95.047, 100.0, 108.883);
    float3 c0 = pow(n, float3(1.0 / 3.0));
    float3 c1 = (7.787 * n) + (16.0 / 116.0);
    float3 v = lerp(c0, c1, step(n, float3(0.008856)));
    return float3((116.0 * v.y) - 16.0,
                500.0 * (v.x - v.y),
                200.0 * (v.y - v.z));
}

float4 xyz2lab(in float4 c) { return float4(xyz2lab(c.xyz), c.w); }
#endif

Check it on Github

fn xyz2lab(c: vec3<f32>) -> vec3<f32> {
    let n = c / vec3<f32>(95.047, 100.0, 108.883);
    let c0 = pow(n, vec3<f32>(1.0 / 3.0));
    let c1 = (7.787 * n) + (16.0 / 116.0);
    let v = mix(c0, c1, step(n, vec3<f32>(0.008856)));
    return vec3<f32>(   (116.0 * v.y) - 16.0,
                        500.0 * (v.x - v.y),
                        200.0 * (v.y - v.z));
}

License

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository