sRGB to Linear

From “What the RiSpec never told you” we get this little nugget of useful information. Which is how to convert an sRGB colour to linear light. You can use this to convert textures, but best practice is to pre-convert your textures to linear before creating any mipmapped textures in order to preserve energy in the texture – see the Texture Painting section in Sony Pictures Imageworks colour pipeline for details. A much more common use would be to use this to set the colour of lights.

// decode from sRGB luma to linear light
float sRGB_decode_f(float F)
	float lin;
	if(F <= 0.03928)
		lin = F/12.92;
		lin = pow((F+0.055)/1.055, 2.4);
	return lin;

color sRGB_decode(color C)
	color D;
	setcomp(D, 0, sRGB_decode_f(comp(C,0)));
	setcomp(D, 1, sRGB_decode_f(comp(C,1)));
	setcomp(D, 2, sRGB_decode_f(comp(C,2)));
	return D;

The first function sRGB_decode_f does the majority of the work, the second function sRGB_decode uses that in order to operate on an input colour. To use this in SL we would use something along the lines of this. The first line here creates a colour variable with a mid-grey value in sRGB space. The second line converts that colour into a linear colour.

color myColour = (0.5);
myColour = sRGB_decode(myColour);


Leave a Reply

Your email address will not be published. Required fields are marked *