Sub topic 9
sRGB and gamma: why channel values are not proportional to light
This chapter explains why the number stored in an sRGB channel is not the amount of light a screen emits. It assumes you know what an 8-bit RGB channel is (0 to 255) and how hex codes write those values. It also assumes you can read short Python or JavaScript snippets. By the end you should be able to explain gamma encoding, convert between encoded and linear values, and avoid the most common mistakes that follow from mixing them up.
The problem: 128 is not half
An 8-bit channel runs from 0 to 255. It is natural to assume that 128 means "about half as bright as 255". On a standard screen it does not. A channel value of 128 produces roughly 22% of the light of a value of 255.
The reason is that sRGB stores colour with a non-linear curve. The number in the file is an encoded value, and the light a display emits is a linear quantity. The two are related by a formula, not by simple proportion.
Two terms are used throughout this chapter:
- Linear light is a value proportional to physical light energy. Doubling it means twice as many photons. Maths such as blending and averaging light is only physically correct on linear values.
- Encoded value (or gamma-encoded value) is the number stored in the file after a non-linear curve has been applied. Hex codes, CSS
rgb()values and most image files hold encoded values.
Why encode non-linearly
Human vision is much more sensitive to differences between dark tones than between bright ones. Two dark greys that differ by 1% of linear light can look clearly different. Two bright greys that differ by 1% look identical.
With only 256 steps per channel, a linear encoding would waste most of them. Many steps would sit in the bright range, where we cannot tell them apart. Too few would sit in the dark range, where we would see visible bands (posterization) between neighbouring values.
A non-linear curve fixes this by giving dark tones more of the 256 codes and bright tones fewer. It is a form of compression: it spends precision where the eye can use it. This is the main reason 8-bit images look smooth in the shadows.
What gamma means
The curve is called a gamma curve because it is approximately a power function. If a linear value L runs from 0 to 1, the encoded value E is roughly:
E = L ^ (1 / 2.2)Decoding reverses it:
L = E ^ 2.2The exponent 2.2 is the "gamma". Raising a number between 0 and 1 to a power above 1 makes it smaller. So a mid-range encoded value of 0.5 decodes to about 0.5^2.2, which is about 0.22. That matches the 22% figure above.
Historically, cathode-ray tube displays responded to voltage with roughly this power curve. Standards kept the curve after CRTs disappeared, because it is also a good perceptual compression.
The sRGB transfer function
sRGB does not use a pure power function. It uses a transfer function, a piecewise formula with a short straight segment near black and a power segment above it. The straight segment avoids an infinitely steep slope at zero, which is awkward for hardware and for rounding.
To decode, first divide the 8-bit value by 255 to get c in the range 0 to 1. Then:
- If
c <= 0.04045, linear =c / 12.92. - Otherwise, linear =
((c + 0.055) / 1.055) ^ 2.4.
The exponent is 2.4 rather than 2.2, but the offset and scale in the formula make the overall curve behave much like a gamma of about 2.2. This is why people say "sRGB gamma is roughly 2.2" even though the formula contains 2.4.
Here is the decode function from the spine, as runnable code:
def srgb_to_linear(value_8bit):
c = value_8bit / 255
if c <= 0.04045:
return c / 12.92
return ((c + 0.055) / 1.055) ** 2.4
print(srgb_to_linear(128)) # about 0.2158, not 0.5The result, about 0.2158, is the fraction of full linear light that a channel value of 128 represents.
The encoding direction
You also need the inverse. When you compute something in linear light and want to store or display it, you must encode it again:
def linear_to_srgb(linear):
if linear <= 0.0031308:
c = linear * 12.92
else:
c = 1.055 * (linear ** (1 / 2.4)) - 0.055
return round(c * 255)
print(linear_to_srgb(0.2158)) # 128
print(linear_to_srgb(0.5)) # 188The second line is worth noticing. To get a surface that emits half the light of white, you need a channel value of about 188, not 128.
A table of values
This table shows how encoded values map to linear light, rounded to three decimals.
| Encoded value (0-255) | Encoded fraction | Linear light |
|---|---|---|
| 0 | 0.000 | 0.000 |
| 32 | 0.125 | 0.014 |
| 64 | 0.251 | 0.051 |
| 128 | 0.502 | 0.216 |
| 188 | 0.737 | 0.500 |
| 255 | 1.000 | 1.000 |
The lower half of the encoded range, 0 to 128, covers only about the darkest fifth of linear light. The upper half covers the remaining four fifths. The curve is steep and dark-heavy in how it spends its codes.
The lower curve is the real sRGB response. The upper line is what you would get if channel values were proportional to light.
Where this matters: blending and averaging
Most graphics software historically did its maths directly on encoded values. That is simple, and it is often wrong in a way you can see.
Take a pixel of pure red (255, 0, 0) and one of pure green (0, 255, 0), and average them to blend. Averaging encoded values gives (127.5, 127.5, 0), a dark olive. Physically, mixing equal amounts of red and green light gives half the linear light in each channel, which encodes back to about (188, 188, 0), a brighter yellow.
function srgbToLinear(v) {
const c = v / 255;
return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
}
function linearToSrgb(l) {
const c = l <= 0.0031308 ? l * 12.92 : 1.055 * Math.pow(l, 1 / 2.4) - 0.055;
return Math.round(c * 255);
}
function mixNaive(a, b) {
return a.map((v, i) => Math.round((v + b[i]) / 2));
}
function mixLinear(a, b) {
return a.map((v, i) =>
linearToSrgb((srgbToLinear(v) + srgbToLinear(b[i])) / 2)
);
}
const red = [255, 0, 0];
const green = [0, 255, 0];
console.log(mixNaive(red, green)); // [128, 128, 0]
console.log(mixLinear(red, green)); // [188, 188, 0]Neither answer is "wrong" in every context. Many design tools deliberately blend in encoded space because designers expect that look, and CSS gradients do the same by default. But if you are simulating light, resizing images, or anti-aliasing text, linear-light maths gives more faithful results. Naive averaging darkens fine detail: a downscaled black-and-white checkerboard turns out darker than a 50% grey patch should be.
Common errors
- Treating 128 as 50% brightness. It is about 22% of linear light. A 50% light level is about 188.
- Averaging or scaling encoded values and expecting physical results. Convert to linear first, do the maths, then encode again.
- Applying the curve twice. If an image is already encoded and you encode it again, it looks washed out and too light. If you decode twice, it looks too dark and contrasty.
- Forgetting the straight segment. Using only
c ** 2.2is a fine approximation for display, but it differs from true sRGB in the darkest tones. For exact round trips use the piecewise formula. - Assuming gamma is the same everywhere. Other colour spaces use different curves. Display P3 uses the same transfer function as sRGB, while Adobe RGB uses a pure gamma of about 2.2. Always check which space your data is in.
- Confusing gamma with brightness. Gamma changes the shape of the response curve, not the display's maximum light output. Brightness controls change the backlight or peak level.
Gamma and the rest of the colour pipeline
This chapter sits between the idea of a colour space and the problem of conversion. An sRGB colour space is defined by three things: its primary colours (which fix the gamut), its white point, and its transfer function (the gamma curve). The earlier chapter on gamut covered the first two. This chapter covered the third.
The transfer function matters for conversion too. A correct conversion between spaces decodes to linear light, applies a matrix to change primaries, and then encodes with the target space's curve. The naive RGB-to-CMYK formula in the next chapters skips all of this, which is one reason it is only an approximation.
Practical guidance
- For web and app design, you usually work directly with encoded sRGB values and hex codes. That is correct and expected. You only need linear light when you write code that simulates lighting, blends physically, or resizes images with care.
- For contrast and accessibility checks, the WCAG relative luminance formula decodes each channel to linear light first, using the sRGB curve, then weights the channels. If you compute contrast from raw 0 to 255 values, your ratios will be wrong.
- For image processing code, decide where in the pipeline values are linear and keep a clear convention. Name variables
linearorencodedso mistakes are visible. - For 8-bit linear storage, avoid it. Storing linear light in 8 bits puts too few codes in the shadows and produces visible banding. Use 16-bit or floating-point formats if you must store linear data.
Exercise
A designer wants a flat grey that emits exactly 50% of the linear light of white on an sRGB display. Roughly what 8-bit channel value should they use for red, green and blue, and why is 128 the wrong choice?
Show answer
About 188, which is the hex colour #bcbcbc. The sRGB curve is non-linear, so a channel value of 128 decodes to about 0.216 of full linear light, roughly 22%. To reach 0.5 linear light you apply the encoding function: 1.055 × 0.5^(1/2.4) − 0.055 ≈ 0.737, and 0.737 × 255 ≈ 188.
Exercise
You downscale an image by averaging neighbouring pixels directly on their 8-bit sRGB values. A black pixel (0) and a white pixel (255) average to 128. Is that the grey you would get from physically mixing equal light from them? What value would the linear-light method give?
Show answer
No. Averaging encoded values gives 128, which is only about 22% of full light. Averaging in linear light gives (0 + 1) / 2 = 0.5, and encoding 0.5 gives about 188. Averaging encoded values therefore makes the result too dark.
Exercise
Why do 8-bit images use a gamma curve instead of storing linear light values?
Show answer
Human vision is more sensitive to differences in dark tones than in bright ones. A gamma curve gives dark tones more of the 256 available codes and bright tones fewer. A linear 8-bit encoding would waste codes in the bright range and cause visible banding in shadows.