LYGIA Shader Library

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

sRGB to linear RGB conversion.

Use:

<float|vec3\vec4> srgb2rgb(<float|vec3|vec4> srgb)

Check it on Github


#ifndef SRGB_EPSILON 
#define SRGB_EPSILON 0.00000001
#endif

#ifndef FNC_SRGB2RGB
#define FNC_SRGB2RGB

// 1.0 / 12.92 = 0.0773993808
// 1.0 / (1.0 + 0.055) = 0.947867298578199

float srgb2rgb(float channel) {
    return (channel < 0.04045) ? channel * 0.0773993808 : pow((channel + 0.055) * 0.947867298578199, 2.4);
}

vec3 srgb2rgb(vec3 srgb) {
    return vec3(srgb2rgb(srgb.r + SRGB_EPSILON), 
                srgb2rgb(srgb.g + SRGB_EPSILON), \
                srgb2rgb(srgb.b + SRGB_EPSILON));
}

vec4 srgb2rgb(vec4 srgb) {
    return vec4(srgb2rgb(srgb.rgb), srgb.a);
}

#endif

Use:

<float|float3\float4> srgb2rgb(<float|float3|float4> srgb)

Check it on Github


#ifndef SRGB2RGB_EPSILON
#define SRGB2RGB_EPSILON 0.00000001
#endif

#ifndef FNC_SRGB2RGB
#define FNC_SRGB2RGB

// 1. / 12.92 = 0.0773993808
// 1.0 / (1.0 + 0.055) = 0.947867298578199

float srgb2rgb(float channel) {
    return (channel < 0.04045) ? channel * 0.0773993808 : pow((channel + 0.055) * 0.947867298578199, 2.4);
}

float3 srgb2rgb(float3 srgb) {
    return float3(srgb2rgb(srgb[0] + SRGB2RGB_EPSILON), srgb2rgb(srgb[1] + SRGB2RGB_EPSILON), srgb2rgb(srgb[2] + SRGB2RGB_EPSILON));
}

float4 srgb2rgb(float4 srgb) {
    return float4(srgb2rgb(srgb.rgb), srgb.a);
}

#endif

Check it on Github

fn srgb2rgb_mono(srgb:f32) -> f32 {
    if (channel <= 0.04045)
        return channel * 0.0773993808; // 1.0 / 12.92;
    else
        return pow((channel + SRGB_ALPHA) / (1.0 + SRGB_ALPHA), 2.4);
}

fn srgb2rgb(srgb:vec3<f32>) -> vec3<f32> {
    return vec3<f32>(
            srgb2rgb_mono(srgb.r + 0.00000001),
            srgb2rgb_mono(srgb.g + 0.00000001),
            srgb2rgb_mono(srgb.b + 0.00000001)
        );
}

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