Jump to content

  • Log In with Google      Sign In   
  • Create Account


Gamma correct gradient


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Krzysztof Narkowicz   Members   -  Reputation: 1134

Like
0Likes
Like

Posted 03 October 2012 - 11:21 AM

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.

blog | twitter | "Don't have any friends? Still a virgin? Programming is for you!"


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8505

Like
0Likes
Like

Posted 03 October 2012 - 04:43 PM

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.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Krzysztof Narkowicz   Members   -  Reputation: 1134

Like
0Likes
Like

Posted 03 October 2012 - 05:24 PM

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.

blog | twitter | "Don't have any friends? Still a virgin? Programming is for you!"


#4 Hodgman   Moderators   -  Reputation: 29395

Like
2Likes
Like

Posted 03 October 2012 - 08:41 PM

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.

Edited by Hodgman, 03 October 2012 - 08:44 PM.


#5 Krzysztof Narkowicz   Members   -  Reputation: 1134

Like
0Likes
Like

Posted 04 October 2012 - 03:20 AM

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:
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:
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 Posted Image.

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.

blog | twitter | "Don't have any friends? Still a virgin? Programming is for you!"


#6 Hodgman   Moderators   -  Reputation: 29395

Like
1Likes
Like

Posted 04 October 2012 - 03:53 AM

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.

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.

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.

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 (pow(1/255,1/2.2)*255), 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

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.

Edited by Hodgman, 04 October 2012 - 04:46 AM.


#7 Krzysztof Narkowicz   Members   -  Reputation: 1134

Like
0Likes
Like

Posted 04 October 2012 - 10:18 AM

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.

blog | twitter | "Don't have any friends? Still a virgin? Programming is for you!"


#8 Hodgman   Moderators   -  Reputation: 29395

Like
1Likes
Like

Posted 04 October 2012 - 11:19 PM

You've mentioned that you correct your final image by using pow(x, 1/2.2), 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 pow(x, 1/1.8) correction curve.

#9 pixalot   Members   -  Reputation: 145

Like
-1Likes
Like

Posted 05 October 2012 - 09:36 AM

"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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS