LYGIA Shader Library

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

Converts a linear RGB color to sRGB.

Dependencies:

Use:

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

Check it on Github



#ifndef SRGB_EPSILON 
#define SRGB_EPSILON 0.00000001
#endif

#ifndef FNC_RGB2SRGB
#define FNC_RGB2SRGB
float rgb2srgb(const in float c) {   return (c <= 0.0031308) ? c * 12.92 : 1.055 * pow(c, 0.4166666666666667) - 0.055; }
vec3  rgb2srgb(const in vec3 rgb) {  return saturate(vec3(  rgb2srgb(rgb.r - SRGB_EPSILON), 
                                                            rgb2srgb(rgb.g - SRGB_EPSILON), 
                                                            rgb2srgb(rgb.b - SRGB_EPSILON))); }
vec4  rgb2srgb(const in vec4 rgb) {  return vec4(rgb2srgb(rgb.rgb), rgb.a); }
#endif

Use:

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

Check it on Github


#ifndef RGB2SRGB_EPSILON
#define RGB2SRGB_EPSILON 0.00000001
#endif

#ifndef FNC_RGB2SRGB
#define FNC_RGB2SRGB
// 1.0 / 2.4 = 0.4166666666666667 
float  rgb2srgb(float c) {      return (c <= 0.0031308) ? c * 12.92 : 1.055 * pow(c, 0.4166666666666667) - 0.055; }
float3 rgb2srgb(float3 rgb) {   return saturate(float3( rgb2srgb(rgb[0] - RGB2SRGB_EPSILON), 
                                                        rgb2srgb(rgb[1] - RGB2SRGB_EPSILON), 
                                                        rgb2srgb(rgb[2] - RGB2SRGB_EPSILON))); }
float4 rgb2srgb(float4 rgb) {   return float4(rgb2srgb(rgb.rgb), rgb.a);}
#endif

Check it on Github

// 1.0 / 2.4 = 0.4166666666666667 
fn rgb2srgb_mono(channel: f32) -> f32 {
    if (channel <= 0.0031308)
        return 12.92 * channel;
    else
        return (1.055) * pow(channel, 0.4166666666666667) - 0.055;
}

fn rgb2srgb(rgb: vec3) -> vec3 {
    return saturate(vec3(rgb2srgb_mono(rgb.r), rgb2srgb_mono(rgb.g), rgb2srgb_mono(rgb.b)));
}

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