Color conversion from Linear to sRGB color space and back in Blender

When working with colors, Blender operates on values from a Linear color space. This allows to show colors and gradients in a way that is more familiar to the human eye, but this should always be taken into account when working directly with colors in Blender.

If we try to print the pixel color values for a red color image obtained, for example, from the compositing Viewer Node, we will get a set of values in a Linear color space.

If we save such an array of pixels into an image, for example in PNG format, and then open it in a 2D graphics editor, the result will be noticeably different from what we expected.

This is because the sRGB color space is usually used for images, not Linear, as we got in the Blender output. In order to get the pixel values of an image in the sRGB color space suitable for saving, they need to be converted.

Let’s define a function that converts a value from the Linear color space to the sRGB color space:

In the function argument, we will pass a color value in the Linear color space in the range from 0.0 to 1.0. The function returns a value in the sRGB color space in the range from 0 to 255.

For example, for clear red color:

By converting all the color values of the pixels in this way, we can save it to a file without color loss.

We can also make the reverse conversion from the sRGB color space to the Linear.

We will pass a color in the sRGB color space in the range from 0 to 255 to this function as a parameter, and at the output we will get a color in the Linear color space in the range from 0.0 to 1.0.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments