LYGIA Shader Library

rgb2oklab (lygia/color/space/rgb2oklab)

Linear rgb to OKLab https://bottosson.github.io/posts/oklab/

Use:

<vec3\vec4> rgb2oklab(<vec3|vec4> rgb)

Check it on Github


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

const mat3 RGB2OKLAB_B = mat3(
    0.4122214708, 0.2119034982, 0.0883024619,
    0.5363325363, 0.6806995451, 0.2817188376,
    0.0514459929, 0.1073969566, 0.6299787005);
#endif

#ifndef FNC_RGB2OKLAB
#define FNC_RGB2OKLAB
vec3 rgb2oklab(const in vec3 rgb) {
    vec3 lms = RGB2OKLAB_B * rgb;
    return RGB2OKLAB_A * (sign(lms)*pow(abs(lms), vec3(0.3333333333333)));

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

Use:

<float3\float4> rgb2oklab(<float3|float4> rgb)

Check it on Github


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

static const float3x3 RGB2OKLAB_B = float3x3(
    0.4122214708, 0.2119034982, 0.0883024619,
    0.5363325363, 0.6806995451, 0.2817188376,
    0.0514459929, 0.1073969566, 0.6299787005);
#endif

#ifndef FNC_RGB2OKLAB
#define FNC_RGB2OKLAB
float3 rgb2oklab(float3 rgb) {
    float3 lms = mul(RGB2OKLAB_B, rgb);
    return mul(RGB2OKLAB_A, sign(lms) * pow(abs(lms), float3(0.3333333333333, 0.3333333333333, 0.3333333333333)));

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

Use:

<float3\float4> rgb2oklab(<float3|float4> rgb)

Check it on Github


#ifndef MAT_RGB2OKLAB
#define MAT_RGB2OKLAB
constant matrix<float, 3, 3> RGB2OKLAB_A = matrix<float, 3, 3>(
    0.2104542553, 1.9779984951, 0.0259040371,
    0.7936177850, -2.4285922050, 0.7827717662,
    -0.0040720468, 0.4505937099, -0.8086757660);

constant matrix<float, 3, 3> RGB2OKLAB_B = matrix<float, 3, 3>(
    0.4122214708, 0.2119034982, 0.0883024619,
    0.5363325363, 0.6806995451, 0.2817188376,
    0.0514459929, 0.1073969566, 0.6299787005);
#endif

#ifndef FNC_RGB2OKLAB
#define FNC_RGB2OKLAB
float3 rgb2oklab( float3 rgb) {
    float3 lms = RGB2OKLAB_B * rgb;
    return RGB2OKLAB_A * (sign(lms)*pow(abs(lms), float3(0.3333333333333)));

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

Use:

<vec3\vec4> rgb2oklab(<vec3|vec4> rgb)

Check it on Github


const RGB2OKLAB_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 RGB2OKLAB_B : mat3x3<f32>  = mat3x3<f32>( vec3f(0.4122214708, 0.2119034982, 0.0883024619),
                                                vec3f(0.5363325363, 0.6806995451, 0.2817188376),
                                                vec3f(0.0514459929, 0.1073969566, 0.6299787005) );

fn rgb2oklab(rgb: vec3f) -> vec3f {
    let lms = RGB2OKLAB_B * rgb;
    return RGB2OKLAB_A * (sign(lms) * pow(abs(lms), vec3f(0.3333333333333)));
}

Licenses

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