Converts an sRGB-encoded value in [0, 1] to linear light, using the exact sRGB curve. Values outside [0, 1] are not clamped.
#import "Math";
assert(abs(srgb_to_linear(0.5) - 0.21404114) < 0.00001);
assert(srgb_to_linear(0.0) == 0);↗#import "Srgb";
Conversion between sRGB-encoded and linear-light color values.
sRGB spends more of its range on dark values than linear light does, so blend, filter and light in linear space and encode on output.
srgb_to_linear and linear_to_srgb are the exact curves, each for a float, a Vector3 and a Vector4 (alpha is left alone). The _fast forms use a table, and the _pow2 and _sqrt forms a gamma of 2.0; both are cheaper and less accurate.
#import "Math";
encoded := xyz(0.5, 0.5, 0.5);
linear := srgb_to_linear(encoded);
assert(abs(linear.x - 0.214) < 0.001);
back := linear_to_srgb(linear);
assert(abs(back.x - 0.5) < 0.00001);Converts an sRGB-encoded value in [0, 1] to linear light, using the exact sRGB curve. Values outside [0, 1] are not clamped.
#import "Math";
assert(abs(srgb_to_linear(0.5) - 0.21404114) < 0.00001);
assert(srgb_to_linear(0.0) == 0);↗Converts linear light in [0, 1] to sRGB encoding, the inverse of srgb_to_linear. Values outside [0, 1] are not clamped; clamp first when the result goes into 8 bits.
#import "Math";
assert(abs(linear_to_srgb(0.21404114) - 0.5) < 0.00001);
assert(abs(linear_to_srgb(srgb_to_linear(0.3)) - 0.3) < 0.00001);↗A faster srgb_to_linear for values in [0, 1], using a lookup table instead of pow. Off by less than 0.002.
#import "Math";
assert(abs(srgb_to_linear_fast(0.5) - srgb_to_linear(0.5)) < 0.002);↗A faster linear_to_srgb for values in [0, 1], using a lookup table instead of pow. Off by less than 0.003.
#import "Math";
assert(abs(linear_to_srgb_fast(0.5) - linear_to_srgb(0.5)) < 0.003);↗A rough srgb_to_linear that squares each channel (gamma 2.0). Cheap, and close in the middle of the range, but off near black.
#import "Math";
assert(srgb_to_linear_pow2(xyz(0.5, 1, 0)) == xyz(0.25, 1, 0));↗A rough linear_to_srgb that takes the square root of each channel, the inverse of srgb_to_linear_pow2. A negative channel gives NaN.
#import "Math";
assert(linear_to_srgb_sqrt(xyz(0.25, 1, 0)) == xyz(0.5, 1, 0));↗