LYGIA Shader Library

saturationMatrix (lygia/v1.1.4/color/saturationMatrix)

generate a matrix to change a the saturation of any color

Use:

saturationMatrix(<float> amount)

Check it on Github


#ifndef FNC_SATURATIONMATRIX
#define FNC_SATURATIONMATRIX
mat4 saturationMatrix(in float amount) {
    vec3 lum = vec3(.3086, .6094, .0820 );

    float invAmount= 1. - amount;

    vec3 red = vec3(lum.x * invAmount);
    red += vec3(amount, .0, .0);

    vec3 green = vec3(lum.y * invAmount);
    green += vec3( .0, amount, .0);

    vec3 blue = vec3(lum.z * invAmount);
    blue += vec3( .0, .0, amount);

    return mat4(red,        .0,
                green,      .0,
                blue,       .0,
                .0, .0, .0, 1.);
}
#endif

Use:

saturationMatrix(<float> amount)

Check it on Github


#ifndef FNC_SATURATIONMATRIX
#define FNC_SATURATIONMATRIX
float4x4 saturationMatrix(in float amount) {
    float3 lum = float3(.3086, .6094, .0820);
    float invAmount= 1. - amount;

    float3 red = float3(1.0, 1.0, 1.0) * lum.x * invAmount;
    red += float3(amount, .0, .0);

    float3 green = float3(1.0, 1.0, 1.0) * lum.y * invAmount;
    green += float3( .0, amount, .0);

    float3 blue = float3(1.0, 1.0, 1.0) * lum.z * invAmount;
    blue += float3( .0, .0, amount);

    return float4x4(red.x, green.x, blue.x, .0,
                    red.y, green.y, blue.y, .0,
                    red.z, green.z, blue.z, .0,
                    .0, .0, .0, 1.);
}
#endif

Check it on Github

fn saturationMatrix(amount : f32) -> mat3x3<f32> {
    let lum = vec3<f32>(0.3086, 0.6094, 0.0820 );
    let invAmount = 1.0 - amount;
    return mat3x3<f32>( vec3<f32>(lum.x * invAmount) + vec3<f32>(amount, .0, .0), 
                        vec3<f32>(lum.y * invAmount) + vec3<f32>( .0, amount, .0),
                        vec3<f32>(lum.z * invAmount) + vec3<f32>( .0, .0, amount));
}

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