Sub topic 11
What a colour space defines
This chapter assumes you know that an image is an array of integer samples, that each pixel has several channels, and that bit depth sets how many values a sample can take. Those ideas describe the container. This chapter covers what the numbers in it mean.
The same numbers can be different colours
Take the pixel (0, 255, 0). In a typical web image it is the most saturated green an sRGB display can show. In a Display P3 image the same triple is a noticeably more saturated green. Read as a linear-light value instead of a gamma-encoded one, it has a different brightness again. The array holds identical bytes in all three cases, and only the interpretation differs.
A colour space is the set of rules that maps a tuple of numbers to a physical colour. Without those rules, a pixel is only a label.
The four things a colour space fixes
An RGB colour space is defined by four decisions.
| Part | Question it answers | Example (sRGB) |
|---|---|---|
| Primaries | Which exact red, green, and blue does `(1, 0, 0)`, `(0, 1, 0)`, and `(0, 0, 1)` mean? | Rec. 709 chromaticities |
| White point | Which colour is `(1, 1, 1)`? | D65, about 6500 K daylight |
| Transfer function | How do stored numbers relate to light intensity? | The sRGB curve, close to a 2.2 gamma |
| Numeric encoding | How are values stored as integers? | 8-bit, full range 0 to 255 |
The primaries and white point together define the gamut, the set of colours the space can represent. Every colour inside the triangle formed by the three primaries is reachable by mixing them. Colours outside the triangle are not.
The transfer function is covered in the next chapter on gamma encoding. For now, treat it as a curve that stretches the stored numbers so that more of them go to dark tones, where the eye is more sensitive.
Why this matters in practice
Three failures come from ignoring the colour space.
- Dull or oversaturated pictures. A wide-gamut photo (Display P3, Adobe RGB) shown on a viewer that assumes sRGB looks washed out, because the viewer treats the larger set of colours as if it were the smaller one.
- Wrong arithmetic. Averaging or blending gamma-encoded values is not the same as averaging light. Resizing and blurring in an encoded space darken edges slightly.
- Silent mismatches between libraries. If one tool outputs BGR order, limited range, or a different transfer function and the next tool assumes otherwise, nothing crashes. The colours are just wrong.
Comparing two RGB spaces in code
To compare colours from different spaces, convert both to a common reference. The usual reference is CIE XYZ, a space tied directly to measured light and independent of any device. Each RGB space has a 3×3 matrix that takes linear RGB to XYZ. The matrices below are for sRGB and Display P3, both with a D65 white point.
import numpy as np
SRGB_TO_XYZ = np.array([
[0.4124, 0.3576, 0.1805],
[0.2126, 0.7152, 0.0722],
[0.0193, 0.1192, 0.9505],
])
P3_TO_XYZ = np.array([
[0.4866, 0.2657, 0.1982],
[0.2290, 0.6917, 0.0793],
[0.0000, 0.0451, 1.0439],
])
XYZ_TO_SRGB = np.linalg.inv(SRGB_TO_XYZ)
# Pure Display P3 green, as linear (not gamma-encoded) values
p3_green = np.array([0.0, 1.0, 0.0])
srgb_linear = XYZ_TO_SRGB @ (P3_TO_XYZ @ p3_green)
print(srgb_linear) # red and blue come out negative, green above 1
out_of_gamut = bool(((srgb_linear < 0) | (srgb_linear > 1)).any())
print(out_of_gamut) # TrueNegative or above-1 results mean the colour lies outside the sRGB triangle. A converter must then decide what to do. It can clip each channel, which is simple but shifts hue. It can compress the whole colour toward the white point, or it can follow the rendering intent set in an ICC profile. Clipping is the common default and the source of many "my colours changed" reports.
Note that the matrices act on linear values. Applying them to gamma-encoded 8-bit values gives wrong results. The correct order is to decode with the transfer function, apply the matrix, then re-encode with the target space's transfer function.
Numeric encoding: range and bit depth
Even with the primaries and curve fixed, the integers can follow different conventions.
- Full range uses the whole 0 to 255 span. JPEG and most computer graphics work this way.
- Limited (studio) range reserves headroom, so black is 16 and white is 235 in 8 bits. Much broadcast and video material uses it, particularly in YCbCr (a later chapter).
Mixing the two is a common video bug. Limited-range data shown as full range looks washed out, with black at about 6% grey. Full-range data shown as limited range crushes shadows and clips highlights.
Beyond RGB: other families
RGB spaces differ from each other by the four parts above. Other families change what the three numbers represent.
- HSV and HSL re-express RGB as hue, saturation, and value or lightness. They are convenient for picking colours or thresholding by hue. They are still defined relative to an underlying RGB space, so they inherit its primaries and curve.
- YCbCr separates a brightness signal from two colour-difference signals. It underlies JPEG and most video.
- CIE Lab is built so that equal numeric distances approximate equal perceived differences. It is used for colour-difference measurement and some segmentation work.
Later chapters take these in turn. The idea to keep from this chapter is that every one of them is a colour space, so each needs the same four questions answered.
Untagged images and defaults
Image files can carry a tag that names their colour space, such as an embedded ICC profile, a PNG sRGB chunk, or JPEG metadata. Many files carry none. The accepted convention is to treat an untagged image as sRGB, and the web does this. This is a convention, not a guarantee. A camera or a wide-gamut export can produce an untagged file whose numbers are not sRGB, and nothing in the array reveals that.
Practical rules:
- Record the colour space alongside the array when you store or pass images between stages.
- Convert to a single working space (usually sRGB) at the start of a pipeline.
- Keep the original bytes and the tag if you need to reproduce the original appearance.
- Convert to linear light before operations that model physical light, such as blending, blurring, and resizing.
Summary of what to check
When an image's colours look wrong, check these in order: channel order (RGB or BGR), numeric range (full or limited), transfer function (encoded or linear), and primaries (sRGB or wide gamut). Most errors are one of these four.
Exercise
A tool reads a wide-gamut Display P3 photo that has no embedded profile and treats it as sRGB. The array shape and dtype are unchanged. What visible effect do you expect, and which of the four parts of a colour space has been misassigned?
Show answer
Colours look less saturated and slightly shifted, because the P3 primaries are more saturated than the sRGB primaries the tool assumed. The misassigned parts are the primaries (and so the gamut). The white point and transfer function are close enough between the two spaces that they cause little difference. The numbers are valid, but they have been given the wrong meaning.
Exercise
In the code above, why must the matrices be applied to linear values instead of 8-bit stored values?
Show answer
The matrices describe how light mixes, and light adds linearly. Stored values are gamma-encoded, so their relationship to light intensity is non-linear. You must first decode them with the transfer function, apply the matrix, then re-encode for the target space.