Introduction#
Part 1 of the fundamentals said that colours are stored as numbers from 0 to 255, and warned that 128 isn’t half as bright as 255. This post explains that warning properly, because it affects almost every texture, every blend and every lighting calculation a game does:
- Why images store brightness unevenly, using a curve called sRGB
- The difference between encoded values and linear light
- What goes wrong when you do maths on encoded values
- How graphics cards convert between the two for free
Find the Real Half#
The left swatch below is a fine checkerboard of pure black and pure white pixels: exactly half the pixels are fully lit, so it gives off exactly 50% of white’s light. The right swatch has the value 128, “half” of 255. The middle one is yours to adjust.
Step back from your screen, or squint, until the checkerboard blurs into a flat grey. Then move the slider until the middle swatch matches it.
A checkerboard (exactly 50% light), your grey, and the value 128
Below: 16 steps evenly spaced in stored value (top), and 16 steps evenly spaced in light (bottom).
On most screens, the match is around 188, not 128. Value 128 gives off only about 22% of white’s light.
Now look at the ramps. The top ramp’s values go up in even steps, and it looks even. The bottom ramp’s light goes up in even steps, but it looks lopsided: it rushes towards white, with hardly any dark shades. That’s the key: our eyes don’t see light evenly. We’re far more sensitive to differences between dark shades than between bright ones.
Why Images Are Stored This Way#
An 8-bit channel only has 256 values to spend. If they were spread evenly in light, many would be wasted on bright shades we can barely tell apart, and too few would be left for the dark shades, which would show visible bands. So images spend their values to match our eyes: more of them on dark shades, fewer on bright.
The standard that defines exactly how is called sRGB. To turn a stored value into an amount of light, it uses a curve that’s close to raising the value (from 0 to 1) to the power of 2.2, roughly “multiplying it by itself 2.2 times”. Raising a number between 0 and 1 to a power makes it smaller, and small numbers shrink fastest, so dark values end up much darker in light. (A short straight section near black avoids problems at zero.)
The exact sRGB formula
For a stored value c from 0 to 1, the amount of light is:
- If c †0.04045: light = c ÷ 12.92 (the short straight section)
- Otherwise: light = ((c + 0.055) Ă· 1.055)2.4
The power is 2.4, not 2.2, but the shift by 0.055 and the straight section make the overall curve follow a power of 2.2 very closely. Going the other way, light to stored value, uses the reverse: 12.92 Ă light near black, and 1.055 Ă light1 Ă· 2.4 â 0.055 above that. For example, a stored value of 128 (0.502) gives off about 21.6% of white’s light, not 50%.
That’s the “gamma” in “gamma correction”. (It dates back to old CRT (cathode-ray tube) televisions and monitors, whose brightness responded to the electrical signal in roughly this way, so storing images like this was convenient then too.)
So there are two ways to describe a colour:
- Encoded (or “gamma”) values: what’s stored in image files, textures and the framebuffer. Even steps look even.
- Linear values: proportional to the actual amount of light. Doubling the number doubles the light.
Maths Needs Linear Light#
Light adds up physically: two lamps give twice the light of one. So any calculation that adds, averages or scales light, such as lighting, blending, blurring and mipmap averaging, is only correct on linear values. Do it on encoded values and the results come out too dark, too bright or the wrong colour.
The demo shows the same two calculations both ways. The top half blends between two colours; the bottom adds together three coloured spotlights.
Blending and adding light: on encoded values versus on linear light
With red and green, the left gradient goes through a dark, muddy olive in the middle: averaging the stored values gives (128, 128, 0), which is far too little light. The right one passes through a bright yellow, as real red and green light do when mixed. Try other colours: blue to yellow, or black to white. In the spotlights, the left version’s overlaps are too bright and harsh, because it adds the stored values of the lights rather than the lights themselves.
The same mistake makes mipmaps and blurs too dark around bright details, makes anti-aliased edges look thin or chunky, and makes lighting falloff look harsh. The rule is simple: decode to linear, do your maths, encode at the end.
Graphics cards do the conversions for free in hardware. A texture created with an sRGB format (every API has one) is decoded to linear automatically every time it’s sampled, before filtering, so even bilinear blends and mipmap lookups are done on linear values. In the other direction, an sRGB render target (an image the graphics card draws into, covered in the next series) encodes the shader’s linear output automatically when it’s written, and blending then happens in linear space too. The only rule is to use sRGB formats for colour images (photos, painted textures) and plain formats for data that isn’t colour, such as normal maps and how rough each part of a surface is, which should never be decoded.
Questions You Might Have#
Does WebGL convert for me?
It can for textures: one created with an sRGB format such as SRGB8_ALPHA8 is converted to linear automatically every time it’s read. Output to the canvas isn’t converted automatically, so shaders usually convert back to sRGB at the end themselves.
Summary#
- Stored colour values are encoded with the sRGB curve, so even steps of value look even.
- The actual light is roughly value2.2: 128 gives about 22% of the light, and 50% light is about 188.
- Blending, lighting, blurring and averaging must happen on linear values.
- sRGB texture and render target formats convert automatically, in hardware.
That completes the Textures & Sampling series! Next, the Render Targets & Post-Processing series starts drawing into textures instead of the screen.
Further Reading#
- John Novak, What every coder should know about gamma: a thorough, well-illustrated explanation with many examples.
- LearnOpenGL, Gamma Correction: sRGB textures and framebuffers in OpenGL.
- Alexander Houghton, Visualising sRGB & Gamma Correction: more interactive demos on the same topic.
- Wikipedia, sRGB: the exact curves and the history of the standard.