LYGIA Shader Library

xyz2rgb (lygia/v1.1.6/color/space/xyz2rgb)

Converts a XYZ color to RGB color space.

Dependencies:

Use:

xyz2rgb(<vec3|vec4> color)

Check it on Github



#ifndef FNC_XYZ2RGB
#define FNC_XYZ2RGB
vec3 xyz2rgb(in vec3 c) {
    const mat3 mat = mat3( 3.2404542, -0.9692660,  0.0556434,
                            -1.5371585,  1.8760108, -0.2040259,
                            -0.4985314,  0.0415560,  1.0572252);

    vec3 v = mat * (c / 100.0);
    vec3 c0 = (1.055 * linear2gamma(v)) - 0.055;
    vec3 c1 = 12.92 * v;
    vec3 r = mix(c0, c1, step(v, vec3(0.0031308)));
    return r;
}

vec4 xyz2rgb(in vec4 xyz) { return vec4(xyz2rgb(xyz.rgb), xyz.a); }
#endif

Dependencies:

Use:

xyz2rgb(<float3|float4> color)

Check it on Github



#ifndef FNC_XYZ2RGB
#define FNC_XYZ2RGB
float3 xyz2rgb(in float3 c) {
    const float3x3 mat = float3x3(  3.2404542, -0.9692660,  0.0556434,
                                    -1.5371585,  1.8760108, -0.2040259,
                                    -0.4985314,  0.0415560,  1.0572252);

    float3 v = mul(mat, c / 100.0);
    float3 c0 = (1.055 * linear2gamma(v)) - 0.055;
    float3 c1 = 12.92 * v;
    float3 r = lerp(c0, c1, step(v, float3(0.0031308)));
    return r;
}

float4 xyz2rgb(in float4 xyz) { return float4(xyz2rgb(xyz.rgb), xyz.a); }
#endif

Dependencies:

Check it on Github


vec3 xyz2rgb(in vec3 c) {
    const M = mat3x3<f32>(  vec3<f32>(3.2404542, -0.9692660,  0.0556434),
                            vec3<f32>(-1.5371585,  1.8760108, -0.2040259),
                            vec3<f32>(-0.4985314,  0.0415560,  1.0572252));
    let v = M * (c / 100.0);
    let c0 = (1.055 * linear2gamma(v)) - 0.055;
    let c1 = 12.92 * v;
    return mix(c0, c1, step(v, vec3<f32>(0.0031308)));
}

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