LYGIA Shader Library

lab2xyz (lygia/v1.2.0/color/space/lab2xyz)

Converts a Lab color to XYZ color space. https://en.wikipedia.org/wiki/CIELAB_color_space

Use:

rgb2xyz(<vec3|vec4> color)

Check it on Github


#ifndef CIE_WHITE
#ifdef CIE_D50
#define CIE_WHITE vec3(0.96429567643, 1.0, 0.82510460251)
#else
// D65
#define CIE_WHITE vec3(0.95045592705, 1.0, 1.08905775076)
#endif
#endif

#ifndef FNC_LAB2XYZ
#define FNC_LAB2XYZ
vec3 lab2xyz(const in vec3 c) {
    float fy = ( c.x + 16.0 ) / 116.0;
    float fx = c.y / 500.0 + fy;
    float fz = fy - c.z / 200.0;
    return CIE_WHITE * 100.0 * vec3(
        ( fx > 0.206897 ) ? fx * fx * fx : ( fx - 16.0 / 116.0 ) / 7.787,
        ( fy > 0.206897 ) ? fy * fy * fy : ( fy - 16.0 / 116.0 ) / 7.787,
        ( fz > 0.206897 ) ? fz * fz * fz : ( fz - 16.0 / 116.0 ) / 7.787
    );
}

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

Use:

rgb2xyz(<float3|float4> color)

Check it on Github


#ifndef CIE_WHITE
#ifdef CIE_D50
#define CIE_WHITE float3(0.96429567643, 1.0, 0.82510460251)
#else
// D65
#define CIE_WHITE float3(0.95045592705, 1.0, 1.08905775076)
#endif
#endif

#ifndef FNC_LAB2XYZ
#define FNC_LAB2XYZ

#ifndef FNC_LAB2XYZ
#define FNC_LAB2XYZ
float3 lab2xyz(const in float3 c) {
    float fy = ( c.x + 16.0 ) / 116.0;
    float fx = c.y / 500.0 + fy;
    float fz = fy - c.z / 200.0;
    return CIE_WHITE * 100.0 * float3(
        ( fx > 0.206897 ) ? fx * fx * fx : ( fx - 16.0 / 116.0 ) / 7.787,
        ( fy > 0.206897 ) ? fy * fy * fy : ( fy - 16.0 / 116.0 ) / 7.787,
        ( fz > 0.206897 ) ? fz * fz * fz : ( fz - 16.0 / 116.0 ) / 7.787
    );
}

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

Check it on Github

fn lab2xyz(c : vec3f) -> vec3f {
    var f = vec3f(0.0);
    f.y = (c.x + 16.0) / 116.0;
    f.x = c.y / 500.0 + f.y;
    f.z = f.y - c.z / 200.0;
    let c0 = f * f * f;
    let c1 = (f - 16.0 / 116.0) / 7.787;
    return vec3f(95.047, 100.000, 108.883) * mix(c0, c1, step(f, vec3f(0.206897)));
}

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