Sub topic 9
sRGB and gamma: why channel values are not proportional to light
This chapter assumes you know that RGB colours are stored as three 8-bit channels (0 to 255) and that hex codes are the same numbers in base 16. It also assumes you know what a colour space is. Here you will see why a channel value of 128 does not mean "half the light".
The surprise: 128 is not half
It is natural to expect that a channel value of 128 on a 0 to 255 scale produces half the light of 255. On a real screen it does not. In sRGB, a value of 128 produces about 22% of the light of 255.
sRGB is the standard colour space for the web and most consumer screens. Its channel values are encoded with a non-linear curve, usually called gamma. The stored number is not the amount of light. It is a coded version of it.
Linear light and encoded values
Light adds up in a physical way. Two identical lamps give twice the light of one. A value that follows this rule is called linear. Blending, blurring and lighting maths are only physically correct on linear values.
An encoded value is the linear value passed through a curve before storage. sRGB uses a curve that is close to a power of 1/2.2 on the way in. On the way out, the screen undoes it with a power of about 2.2 (the exact sRGB function is slightly different, as shown below).
Why encode at all
Human vision is more sensitive to differences between dark tones than between bright ones. With only 256 steps per channel, a linear scale would waste most steps on bright tones that look nearly the same. It would leave too few steps in the shadows, and you would see banding there.
The sRGB curve spends more of its 256 steps on dark tones. This gives smoother shadows from the same 8 bits. The curve is a form of compression that matches how we see.
The sRGB formula
The sRGB decoding function has a short straight segment near black and a power curve above it. The code below converts an 8-bit value to linear light.
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 exponent is 2.4, but the offset and scale make the overall curve behave like a power of about 2.2. That is why people say "sRGB has a gamma of 2.2".
The inverse function turns linear light back into a stored value:
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.5)) # 188, not 128To store half the light of full white, you need the channel value 188.
A worked example: averaging two colours
Suppose you blend pure black (0) and pure white (255) in equal parts. The naive result is the average of the stored values.
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
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)
a, b = 0, 255
naive = round((a + b) / 2)
correct = linear_to_srgb((srgb_to_linear(a) + srgb_to_linear(b)) / 2)
print(naive) # 128
print(correct) # 188The naive average gives 128, which is darker than a true half mix of light. The correct method decodes to linear, averages, then encodes again, and gives 188. This is why some blur and gradient results look too dark or have dark fringes between saturated colours.
Common errors
- Averaging or blending encoded values. Image resizing, blurring and alpha blending done directly on sRGB numbers are slightly wrong. Many tools do this anyway, so know which one you use when accuracy matters.
- Treating 128 as 50% grey. It is a mid-tone in appearance, but only about 22% of the light.
- Applying gamma twice. If you decode a value and then decode it again, the image looks too dark. If you encode twice, it looks washed out.
- Mixing up gamma and brightness. Gamma changes the shape of the curve. It does not simply lighten or darken the image.
Where this matters for print
The gamma curve belongs to the RGB side. CMYK values are usually ink percentages, and conversion between the two goes through colour profiles. The next chapters cover how those profiles handle tone curves.
Exercise
A pixel has the sRGB value 188 in one channel. Roughly what fraction of full linear light does it produce, and why is a straight average of 0 and 255 (which is 128) not the physically correct midpoint?
Show answer
A value of 188 gives about 0.5 (50%) of full linear light. The straight average of the stored values 0 and 255 gives 128, but stored values are gamma-encoded, so 128 produces only about 22% of full light. To get a true midpoint you decode both values to linear light, average them, and encode the result, which gives 188.