Sub topic 3
Gamma Encoding and Linear Light
This chapter assumes you know NumPy arrays, 8-bit channel values (0 to 255), and RGB triples. Later sections build on the gamma idea, so read them in order.
Gamma encoding and linear light
Stored 8-bit values are not proportional to light. Linear light is physical intensity. Most colour spaces use gamma encoding, which spends more of the 256 levels on dark tones, where the eye is more sensitive. Convert stored values to linear light before averaging, blending, or resizing, because averaging gamma-encoded values produces results that are too dark.
import numpy as np
def srgb_to_linear(value_8bit):
c = np.asarray(value_8bit, dtype=np.float64) / 255.0
return np.where(c <= 0.04045, c / 12.92, ((c + 0.055) / 1.055) ** 2.4)
print(srgb_to_linear(128)) # about 0.216A stored 128 carries only about 21.6% of full light intensity.
sRGB, the default colour space of the web
sRGB is a colour space defined by a set of primary colours and a gamma curve. Browsers and most displays assume sRGB when a file gives no other description. The same RGB triple denotes the same colour only when the reader assumes the same colour space.
HSV and HSL: hue, saturation, and value
HSV and HSL re-express RGB as hue (an angle from 0 to 360 degrees), saturation (colour purity), and a brightness term: value in HSV and lightness in HSL. They suit colour pickers and hue-based selection. They are not perceptually uniform.
Separating brightness from colour with YCbCr
YCbCr stores luma Y, a weighted sum of RGB (weights 0.299, 0.587, and 0.114 in Rec. 601), plus two chroma differences, Cb and Cr. Video and JPEG use it because the eye resolves brightness detail more sharply than colour detail.
Chroma subsampling: 4:4:4, 4:2:2, and 4:2:0
Chroma subsampling stores Cb and Cr at lower resolution than Y.
| Scheme | Chroma resolution | Samples per pixel |
|---|---|---|
| 4:4:4 | Full | 3.0 |
| 4:2:2 | Half horizontally | 2.0 |
| 4:2:0 | Half both ways | 1.5 |
CIE XYZ and Lab: perceptual colour differences
CIE XYZ is a device-independent space modelled on human colour vision. CIE Lab reshapes XYZ so that lightness L and colour axes a and b* track perception more closely. The Euclidean distance between two Lab colours, called ΔE, approximates how different they look. Use Lab for comparing colours, not for storage.
File formats and lossless versus lossy compression
Lossless formats such as PNG reconstruct the exact original pixel values. Lossy formats such as JPEG discard detail that is hard to see, usually by subsampling chroma and quantising frequency coefficients. Choose lossless for masks and repeated editing, and lossy for photographs where file size matters more.
ICC profiles and colour management
An ICC profile is a small data block embedded in an image file. It describes the colour space the numbers refer to, so a viewer can convert them correctly for the display. Without a profile, viewers usually assume sRGB.
Exercise
Why does averaging a black pixel (0) and a white pixel (255) in stored sRGB values give a darker result than averaging in linear light?
Show answer
Averaging the stored values gives 128, which is only about 21.6% of full light. Averaging in linear light gives 0.5, which encodes back to about 187 in sRGB. The linear average is perceptually brighter because gamma encoding is nonlinear.