Gamma correct gradient

Started by
7 comments, last by pixalot 11 years, 6 months ago
Hi,

I'm wondering why sRGB gradients look "better" than linear space gradients. Fox example the gradient from black(0x000000) to white(0xFFFFFF) clearly lacks some black shades - http://scanline.ca/gradients. I tried to check it on different LCD's, but everywhere the sRGB one looks better. Additionally the CIELAB gradient looks just like the sRGB one. On the other hand examples like this one - http://filmicgames.com/archives/354 prove that the color in the middle of the gradient should be ~0.73 and not 0.5. So I'm quite lost here.
Advertisement
It's all about perception. The human eye sees dark shades much better than bright shades, whereas linear RGB doesn't care and gives equal brightness everywhere. We compensate for this by "squashing" the color curve so that we get more black shades, which gives the image a more natural and contrasted look. A raw, uncorrected RGB image will appear bland and dull.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I still don't see why linear space and gamma correction should be used while resizing images, but not when generating gradients.

For example let's take that black-white gradient from http://scanline.ca/gradients/. First one uses linear color space and gamma correction and the color in the middle is (187/255,187/255,187/255). Second one doesn't and color in it's middle is - (128/255,128/255,128/255). According to http://filmicgames.com/archives/354 the color in the middle should be like in the first gradient, but the second gradient looks much better.
I'm wondering why sRGB gradients look "better" than linear space gradients. Fox example the gradient from black(0x000000) to white(0xFFFFFF) clearly lacks some black shades - http://scanline.ca/gradients. I tried to check it on different LCD's, but everywhere the sRGB one looks better. Additionally the CIELAB gradient looks just like the sRGB one. On the other hand examples like this one - http://filmicgames.com/archives/354 prove that the color in the middle of the gradient should be ~0.73 and not 0.5. So I'm quite lost here.
This is a big vague -- are you talking about creating a linear gradient from and displaying it on an sRGB monitor, and then transforming that linear gradient into sRGB space and displaying it on an sRGB monitor?
If so, then this description should make it obvious why one looks worse -- the colour-space of your data and the colour-space of your display don't match in the former test.
I still don't see why linear space and gamma correction should be used while resizing images, but not when generating gradients.
If you're working with mathematical brightness values, then you should always work in linear space, however the default assumption is that the user's monitor is in sRGB space (and most image files are also saved in sRGB space, so when they're displayed, no correction is required). So if you're creating a gradient of intensity values, you can first create a linear gradient, and then transform it using the sRGB curve for storage/display.

First one uses linear color space and gamma correction and the color in the middle is (187/255,187/255,187/255). Second one doesn't and color in it's middle is - (128/255,128/255,128/255)
The second one is a linear gradient (being viewed on an sRGB monitor). The first one is a linear gradient that's been "gamma corrected" into sRGB space (and then viewed on an sRGB monitor). The second one is obviously not going to appear as intended due to the "gamma mismatch" between intended brightness and actual displayed brightness.

This is a big vague -- are you talking about creating a linear gradient from and displaying it on an sRGB monitor, and then transforming that linear gradient into sRGB space and displaying it on an sRGB monitor?


Sorry for confusion.

First gradient is created in linear space and gamma corrected.

vec3 colorA = vec3( 0, 0, 0 );
vec3 colorB = vec3( 1, 1, 1 );
vec3 linearColorA = pow( colorA, 2.2 );
vec3 linearColorB = pow( colorB, 2.2 );
for ( unsigned i = 0; i < 255; ++i )
{
vec3 currColor = lerp( linearColorA, linearColorB, i / 255 );
result[ i ] = pow( currColor, 1.0 / 2.2 );
}

And it looks like this:
[attachment=11616:gradientA.png]

The second one is created in sRGB space.

vec3 colorA = vec3( 0, 0, 0 );
vec3 colorB = vec3( 1, 1, 1 );
for ( unsigned i = 0; i < 255; ++i )
{
result[ i ] = lerp( colorA, colorB, i / 255 );
}

And it looks like this:
[attachment=11617:gradientB.png]

Now according to http://filmicgames.com/archives/354 if you resize a 2 pixel image, where one pixel is white and second is black then the resulting pixel should have color (187,187,187). So the middle color of the gradient should be (187,187,187) - just like in the first gradient. On the other hand second gradient looks better.

This is quite an issue, because in gamma correct UI rendering, when you set up a gradient on a button from black to white it looks bad and graphic artists don't like it smile.png.


So if you're creating a gradient of intensity values, you can first create a linear gradient, and then transform it using the sRGB curve for storage/display.


I'm doing exactly that, but the resulting gradient looks incorrect.
The second one is created in sRGB space.
That's a linear gradient, as it's just a linear interpolation of two RGB values.
I guess you could say that if the two inputs were sRGB colours, then the output is an sRGB gradient -- but seeing black/white are the same in linear and in sRGB -- it's basically a linear gradient (being displayed on an sRGB device).
First gradient is created in linear space and gamma corrected.[/quote]That's also a linear gradient approximately transformed to sRGB, because the sRGB curve is approximately equal to the "gamma 2.2" curve. This is what I'd call an sRGB gradient (except using the proper curve, instead of the pow2.2 approximation)

N.B. In both sRGB/Gamma2.2, 187 equates to 50% black/white. In linear, 128 corresponds to 50% black/white.
Now according to http://filmicgames.com/archives/354 if you resize a 2 pixel image, where one pixel is white and second is black then the resulting pixel should have color (187,187,187). So the middle color of the gradient should be (187,187,187) - just like in the first gradient.[/quote]Theres a reason for this -- if your monitor is correctly calibrated as an sRGB device, then when standing back from a black/white checkerboard image and squinting, it will appear exactly the same as an image filled with 187 grey. An image filled with 128 grey will appear much darker than the (averaged due to distance/squinting) black/white checkerboard.
On the other hand second gradient looks better.[/quote]this isn't a fair visual comparison because you're skipping over a huge number of values in your first gradient. After 0/255, your second value is 21/255 ([font=courier new,courier,monospace]pow(1/255,1/2.2)*255[/font]), so you've skipped 19 shades in there! Your first gradient would have to be almost 200000 pixels wide for you to see all the unique 256 shades of grey in the 8-bit gamma 2.2 gradient.
The second one might look better because it's made up of 256 unique shades, whereas your first is only made up of 183 shades.

Even though, as above there is a mathematical reason for 187 being "middle grey", if you're using this gradient for an artistic purpose, then the "best" gradient is going to be subjectively defined, not mathematically defined.
when you set up a gradient on a button from black to white it looks bad and graphic artists don't like it[/quote]If this is the root problem, can you describe it a bit more -- e.g. what this gradient is for?

Also there's something weird with your gradient images; the labelled shades are to the left of their markers by a bit.

The second one might look better because it's made up of 256 unique shades, whereas your first is only made up of 183 shades.

Even though, as above there is a mathematical reason for 187 being "middle grey", if you're using this gradient for an artistic purpose, then the "best" gradient is going to be subjectively defined, not mathematically defined.


"Best" as the one with linear perceptual brightness change.


If this is the root problem, can you describe it a bit more -- e.g. what this gradient is for?


For example there is a button, which is represented by rectangle consisting of 4 vertices. You can set a color for every vertex of that rectangle. When you use a fully white texture, set one edge to black and second one to white then you get gradient just like the first one in my previous post (UI rendering is gamma correct). This gradient doesn't look as if it would have linear perceptual brightness change - it clearly has too much of white shades.
You've mentioned that you correct your final image by using [font=courier new,courier,monospace]pow(x, 1/2.2)[/font], but are you sure that your monitor is actually an sRGB or "gamma 2.2" display?

On an sRGB display 186/187 should be perceptually half way between 0 and 255 (50%) and 136 should be perceptually half way between 0 and 187 (25%) -- but if your display is not calibrated to sRGB, then this obviously won't be the case.

You can experimentally discover these values for your specific monitor by creating a black/white checkerboard image next to a filled area of a certain shade (e.g. 187), standing back and squinting at the image, and seeing if the blurred checkerboard matches the filled shade.

Or, load up this calibration image, stand a few metres back from the screen and squint, and see where the bars fade together. On my cheap office LCD, that image actually blends together around the 1.8 mark, instead of the 2.2 mark as it's supposed to. So to display "gamma correct" images on this monitor, I'd have to use a [font=courier new,courier,monospace]pow(x, 1/1.8)[/font] correction curve.

"Best" as the one with linear perceptual brightness change.


There is your answer, RGB space is not perceptually linear, sRGB on the other hand is. So if it's a linear gradient you're looking for then you should skip the gamma compression stage at the end and simply work directly in the sRGB space which is linear.

This topic is closed to new replies.

Advertisement