To convert between sRGB and linear color spaces in GLSL, you need to account for the gamma correction applied in the sRGB color space. The sRGB color space uses a non-linear encoding to better match human perception of light intensity. Converting between these spaces involves applying or reversing this gamma correction.
Here’s how you can write GLSL functions for both conversions:
1. sRGB to Linear Conversion
The formula to convert from sRGB to linear is:
- If the sRGB value Csrgb is less than 0.04045, use Clinear=12.92Csrgb.
- Otherwise, use Clinear=(1.055C.
2. Linear to sRGB Conversion
The formula to convert from linear to sRGB is:
- If the linear value Clinear is less than 0.0031308, use Csrgb=12.92⋅.
Below is the GLSL implementation of these conversions:
// Function to convert sRGB to Linear
vec3 srgbToLinear(vec3 srgb) {
bvec3 cutoff = lessThan(srgb, vec3(0.04045));
vec3 lower = srgb / vec3(12.92);
vec3 higher = pow((srgb + vec3(0.055)) / vec3(1.055), vec3(2.4));
return mix(higher, lower, cutoff);
}
// Function to convert Linear to sRGB
vec3 linearToSrgb(vec3 linear) {
bvec3 cutoff = lessThan(linear, vec3(0.0031308));
vec3 lower = linear * vec3(12.92);
vec3 higher = vec3(1.055) * pow(linear, vec3(1.0 / 2.4)) - vec3(0.055);
return mix(higher, lower, cutoff);
}
Explanation of the Code
-
srgbToLinear Function:
lessThan(srgb, vec3(0.04045)): This checks if each component of the sRGB vector is below the threshold (0.04045).
mix(higher, lower, cutoff): Combines the results of the two formulas based on the cutoff condition.
-
linearToSrgb Function:
lessThan(linear, vec3(0.0031308)): Checks if each component of the linear vector is below the threshold (0.0031308).
mix(higher, lower, cutoff): Combines the results of the two formulas based on the cutoff condition.
Usage Example
Here’s an example of how you might use these functions in a fragment shader:
#version 330 core
in vec3 fragColor; // Input color in sRGB space
out vec4 outColor;
void main() {
// Convert sRGB to Linear
vec3 linearColor = srgbToLinear(fragColor);
// Perform some lighting calculations in linear space
vec3 finalColor = linearColor * 1.5; // Example operation
// Convert back to sRGB for output
outColor = vec4(linearToSrgb(finalColor), 1.0);
}
Notes
- Precision: Ensure that your input values are in the range [0, 1]. If they are in the range [0, 255], normalize them by dividing by 255 before using these functions.
- Performance: These functions involve conditional logic (
mix) and power operations (pow), which can be computationally expensive. Optimize where possible, especially in performance-critical shaders.
- Gamma Correction: These conversions assume the standard sRGB gamma curve. If you’re working with a different color space, adjustments may be necessary.
Final Answer:
// Function to convert sRGB to Linear
vec3 srgbToLinear(vec3 srgb) {
bvec3 cutoff = lessThan(srgb, vec3(0.04045));
vec3 lower = srgb / vec3(12.92);
vec3 higher = pow((srgb + vec3(0.055)) / vec3(1.055), vec3(2.4));
return mix(higher, lower, cutoff);
}
// Function to convert Linear to sRGB
vec3 linearToSrgb(vec3 linear) {
bvec3 cutoff = lessThan(linear, vec3(0.0031308));
vec3 lower = linear * vec3(12.92);
vec3 higher = vec3(1.055) * pow(linear, vec3(1.0 / 2.4)) - vec3(0.055);
return mix(higher, lower, cutoff);
}