LYGIA Shader Library

srgb2rgb (lygia/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 1e-10
#endif

#ifndef FNC_SRGB2RGB
#define FNC_SRGB2RGB
// 1.0 / 12.92 = 0.0773993808
// 1.0 / (1.0 + 0.055) = 0.947867298578199
float srgb2rgb(const in float v) {   return (v < 0.04045) ? v * 0.0773993808 : pow((v + 0.055) * 0.947867298578199, 2.4); }
vec3 srgb2rgb(const in vec3 srgb) {  return vec3(   srgb2rgb(srgb.r + SRGB_EPSILON),
                                                    srgb2rgb(srgb.g + SRGB_EPSILON),
                                                    srgb2rgb(srgb.b + SRGB_EPSILON)); }
vec4 srgb2rgb(const in 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 1e-10
#endif

#ifndef FNC_SRGB2RGB
#define FNC_SRGB2RGB
// 1. / 12.92 = 0.0773993808
// 1.0 / (1.0 + 0.055) = 0.947867298578199
float srgb2rgb(float c) {       return (c < 0.04045) ? c * 0.0773993808 : pow((c + 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

Use:

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

Check it on Github


#ifndef SRGB_EPSILON 
#define SRGB_EPSILON 1e-10
#endif

#ifndef FNC_SRGB2RGB
#define FNC_SRGB2RGB
// 1.0 / 12.92 = 0.0773993808
// 1.0 / (1.0 + 0.055) = 0.947867298578199
float srgb2rgb( float v) {   return (v < 0.04045) ? v * 0.0773993808 : pow((v + 0.055) * 0.947867298578199, 2.4); }
float3 srgb2rgb( float3 srgb) {  return float3(   srgb2rgb(srgb.r + SRGB_EPSILON),
                                                    srgb2rgb(srgb.g + SRGB_EPSILON),
                                                    srgb2rgb(srgb.b + SRGB_EPSILON)); }
float4 srgb2rgb( float4 srgb) {  return float4(   srgb2rgb(srgb.rgb), srgb.a); }
#endif

Check it on Github


fn srgb2rgb_mono(channel: f32) -> f32 {
    if (channel < 0.04045) {
        return channel * 0.0773993808;
    }
    else {
        return pow((channel + 0.055) * 0.947867298578199, 2.4);
    }
}

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

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