LYGIA Shader Library

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

sRGB to OKLab https://bottosson.github.io/posts/oklab/

Use:

<vec3\vec4> srgb2oklab(<vec3|vec4> srgb)

Check it on Github


#ifndef MAT_SRGB2OKLAB
#define MAT_SRGB2OKLAB
const mat3 SRGB2OKLAB_A = mat3(  
    0.2104542553, 1.9779984951, 0.0259040371,
    0.7936177850, -2.4285922050, 0.7827717662,
    -0.0040720468, 0.4505937099, -0.8086757660);

const mat3 SRGB2OKLAB_B = mat3(  
    0.4121656120, 0.2118591070, 0.0883097947,
    0.5362752080, 0.6807189584, 0.2818474174,
    0.0514575653, 0.1074065790, 0.6302613616);
#endif

#ifndef FNC_SRGB2OKLAB
#define FNC_SRGB2OKLAB
vec3 srgb2oklab(const in vec3 srgb) {
    vec3 lms = SRGB2OKLAB_B * srgb;
    return SRGB2OKLAB_A * (sign(lms)*pow(abs(lms), vec3(0.3333333333333)));

}
vec4 srgb2oklab(const in vec4 srgb) { return vec4(srgb2oklab(srgb.rgb), srgb.a); }
#endif

Use:

<float3\float4> srgb2oklab(<float3|float4> srgb)

Check it on Github


#ifndef MAT_SRGB2OKLAB
#define MAT_SRGB2OKLAB
const float3x3 SRGB2OKLAB_A = float3x3(  
    0.2104542553, 1.9779984951, 0.0259040371,
    0.7936177850, -2.4285922050, 0.7827717662,
    -0.0040720468, 0.4505937099, -0.8086757660);

const float3x3 SRGB2OKLAB_B = float3x3(  
    0.4121656120, 0.2118591070, 0.0883097947,
    0.5362752080, 0.6807189584, 0.2818474174,
    0.0514575653, 0.1074065790, 0.6302613616);
#endif

#ifndef FNC_SRGB2OKLAB
#define FNC_SRGB2OKLAB
float3 srgb2oklab(float3 rgb) {
    float3 lms = mul(SRGB2OKLAB_B, rgb);
    return mul(SRGB2OKLAB_A, sign(lms)*pow(abs(lms), float3(0.3333333333333)));

}
float4 srgb2oklab(float4 rgb) { return float4(srgb2oklab(rgb.rgb), rgb.a); }
#endif

Check it on Github

const inv_oklab_A : mat3x3<f32>  = mat3x3<f32>( vec3f(0.2104542553, 1.9779984951, 0.0259040371), 
                                                vec3f(0.7936177850, -2.4285922050, 0.7827717662), 
                                                vec3f(-0.0040720468, 0.4505937099, -0.8086757660) );

const inv_oklab_B : mat3x3<f32>  = mat3x3<f32>( vec3f(0.4121656120, 0.2118591070, 0.0883097947), 
                                                vec3f(0.5362752080, 0.6807189584, 0.2818474174), 
                                                vec3f(0.0514575653, 0.1074065790, 0.6302613616) );

fn srgb2oklab(srgb: vec3f) -> vec3f {
    let lms = inv_oklab_B * srgb;
    return inv_oklab_A * (sign(lms) * pow(abs(lms), vec3f(0.3333333333333)));
}

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