To fill a render image with a linear black and white gradient, for example, across the entire width of the image, in Blender’s Compositor, we can use the Gradient Texture node, or implement the gradient using several math nodes.
Let’s say we have a rendered image 1024 pixels wide. We need to fill it with a linear black and white gradient from left to right across the entire width.
Execute render, switch to the Compositing workspace, check the “Use Nodes” box, and add several math nodes to the node tree to create the linear gradient.
Here, we used the Image Coordinates node to get the image’s pixel dimensions from its “Pixel” output. Using the Separate XYZ node, we extract the image width (X-coordinate) from its size. To create a linear gradient at each image point from 0 to 1024, we divide the current X-coordinate by the image width —1024. This creates the desired gradient fill across the image width.
The color changes from 0.0 at the zero coordinate along the image width (the left edge of the image) to 1.0 at the 1024 pixel coordinate (the right edge of the image).
We can verify that our gradient is truly linear and uniform. Open the Image Editor area, switch to the Viewer Node display mode, and, holding down the left mouse button, move the cursor so that it is exactly in the middle of the image, at the 512 pixel X-coordinate. In the status bar at the bottom of the area, we can check the color value at the current point and confirm that it is 0.5, so it the middle of the image, we have exactly the half color value, equal to 0.5.
It would seem that our problem is solved.
However, let’s first save the resulting image with the gradient to a file. Press Alt + s and specify a location and name for the saved file.
Now, let’s open the saved file in a graphics editor, such as Gimp.
Open the color settings window, switch to the Eyedropper tool, and, holding down the left mouse button, place the cursor, as we did before, exactly in the middle of the image, at pixel 512.
And, as we can see, something strange is happening. The color value at this point is somehow equal to 188.
It’s clear that when saving the image, Blender converted all the colors from Blender’s internal format to the standard RGB format, so that all other graphics editors can understand it. So now all our colors range from 0 to 255. That is, on the left side of the image (the zero coordinate on the X axis), where we previously had 0, we still have 0. And on the right side of the image (the 1024 coordinate on the X axis), instead of 1.0, we now have 255.
But then why is it that in the middle of the image, at the 512 coordinate on the X axis, where we previously had a value of 0.5 (half of the maximum possible value), we now have a value of 188, which is not at all equal to half of the maximum value of 255?
This happens because, in addition to simply converting values from the 0.0–1.0 range to the 0–255 range, Blender also takes into account that the colors must be in the sRGB color space to be displayed correctly on monitors and in other graphics editors. But the sRGB color space is not linear! That is, when saving, it didn’t simply multiply everything by 255, but applied a more complex formula to preserve the image’s colors. And if it hadn’t done this, we would have seen a visual difference between the image opened in Blender and the same image opened in Gimp.
So, our linear gradient remains linear only until it’s saved to a file. But what if we want a linear gradient in the final image file?
To ensure the gradient remains linear after saving, we need to apply the same transformation to its colors that Blender applies when saving, but in reverse.
By applying the inverse transformation from a linear color range to the sRGB color range to the gradient colors, we get a nonlinear gradient on the screen in Blender, which, however, becomes linear when Blender applies the sRGB transformation to it when saving to a file.
In other words, let’s assume that the linear gradient we see on the screen in Blender is already in the sRGB color space, and we’ll convert it to a linear color space. We’ll get a nonlinear gradient. When saving to a file, Blender will apply a transformation from linear space to sRGB, and we’ll end up with the desired linear gradient in the sRGB color space.
The easiest way to do this is with the Convert Colorspace node, specifying the original color space as “sRGB” in the “From” parameter and “Linear” in the “To” parameter.
Alternatively, we can reproduce the conversion formulas for the sRGB color space to linear using math nodes.
As we can see, the gradient display in Blender has changed.
Let’s save the resulting image as a file again and open it in Gimp.
Now, by activating the Eyedropper tool and placing the cursor in the middle of the image width, we see the expected value of 127 – a half of the maximum possible value of 255.
Moving the mouse over the image, we can see that the gradient color changes linearly across the image width.
This way, the output from Blender’s compositor is a linear gradient in the sRGB color space.

.blend file on Patreon




