Sub topic 6
Colour from red, green, and blue
This chapter assumes you know how an image is a grid of pixels, how bit depth sets the number of values per sample, and how channels give each pixel several numbers. It covers how three channel values become one colour.
Why three numbers are enough
The human eye has three kinds of cone cells. Each responds most strongly to a different range of wavelengths, roughly long (reddish), medium (greenish), and short (bluish). Your brain builds colour from the relative strength of those three signals. Two different light mixtures that produce the same three signals look identical, and this is why a screen can imitate a rainbow without emitting every wavelength.
A display exploits this by combining three light sources: a red one, a green one, and a blue one. These are the primaries of the RGB model. A pixel stores how strongly each primary should shine.
The additive model
RGB is an additive model. Light from each primary adds to the others, and black is the absence of light. With all three off you see black. With all three at full strength you see white.
The mixing rules at full strength are:
| Red | Green | Blue | Result |
|---|---|---|---|
| 255 | 0 | 0 | Red |
| 0 | 255 | 0 | Green |
| 0 | 0 | 255 | Blue |
| 255 | 255 | 0 | Yellow |
| 0 | 255 | 255 | Cyan |
| 255 | 0 | 255 | Magenta |
| 255 | 255 | 255 | White |
| 0 | 0 | 0 | Black |
Paint and ink work the opposite way. They are subtractive, because pigments absorb light. Printers therefore use cyan, magenta, yellow, and black (CMYK). Do not carry mixing intuitions from a paint box over to RGB. Mixing red and green light gives yellow, not brown.
A pixel is a triple
In the common 8-bit case, each channel is an integer from 0 to 255, so a pixel is a triple (R, G, B). Each channel has 256 levels, which gives 256 × 256 × 256 = 16,777,216 distinct colours. That is the "16.7 million colours" figure on display boxes.
The same idea scales with bit depth. A 10-bit-per-channel image has 1,024 levels per channel and 1,024³ = 1,073,741,824 colours. Higher depth does not add new hues to the display's range. It adds finer steps between existing ones, which reduces visible banding in smooth gradients.
Numeric libraries often store RGB in a different form. Deep learning code usually converts to floating point in the range 0.0 to 1.0:
import numpy as np
pixel = np.array([255, 128, 0], dtype=np.uint8) # orange
normalised = pixel.astype(np.float32) / 255.0
print(normalised) # [1. 0.5019608 0. ]The range convention (0–255 integers versus 0.0–1.0 floats) is a property of the array's dtype and your library, not of RGB itself. Code that expects one and receives the other produces images that are nearly all black or nearly all white.
Working with RGB values in code
Because pixels are small unsigned integers, arithmetic needs care. NumPy's uint8 wraps around on overflow instead of saturating:
import numpy as np
a = np.array([200], dtype=np.uint8)
b = np.array([100], dtype=np.uint8)
print(a + b) # [44], because 300 wraps around to 300 - 256
wide = a.astype(np.int16) + b
print(wide) # [300]
print(np.clip(wide, 0, 255).astype(np.uint8)) # [255]The safe pattern is to widen the dtype, compute, clip to the valid range, and convert back. Adding two colours this way mimics additive light, as in this red-plus-green example:
import numpy as np
red = np.array([255, 0, 0], dtype=np.uint8)
green = np.array([0, 255, 0], dtype=np.uint8)
yellow = np.clip(red.astype(np.int16) + green, 0, 255).astype(np.uint8)
print(yellow) # [255 255 0]Averaging two images or colours has the same hazard. (a + b) / 2 on uint8 arrays overflows before the division. Cast first, or use a // 2 + b // 2 and accept a small rounding error.
Common errors
- Assuming RGB values are physical light amounts. Stored values are usually gamma-encoded, so doubling a value does not double the light. Averaging or blending directly on stored values is therefore slightly wrong. A later chapter covers linear light.
- Assuming RGB is a single universal colour.
(255, 0, 0)means "full red" only relative to a particular set of primaries and a transfer function. The same triple can look different on a wide-gamut display. The chapter on colour spaces covers this. - Using Euclidean distance in RGB as perceived difference. Two colours that are equally far apart in RGB can look very different to a person. Green steps are easier to see than blue ones. Use a perceptual space such as CIE Lab when you need that.
- Mixing up channel order. Some libraries return blue first. The sample
(255, 0, 0)is red in RGB order but blue in BGR order. A swapped image looks like a person with blue skin. - Mixing up the range. Feeding a 0–255 array to code that expects 0.0–1.0 saturates nearly every pixel.
What RGB does well, and what it does not
RGB matches how cameras and screens work. A sensor records red, green, and blue filtered light, and a display emits it. This makes RGB the natural format for capture, storage, and display, and the default input for most computer vision models.
It is less convenient for tasks that depend on human concepts. Selecting "all the orange pixels" needs thresholds on all three channels at once, and changes in lighting move all three. Separating brightness from colour is also awkward, because every channel carries both. Later chapters introduce HSV, YCbCr, and CIE Lab, which re-express the same colours in coordinates that suit those tasks.
Exercise
A 10-bit-per-channel RGB image has how many distinct colours? Then, what does np.array([250], dtype=np.uint8) + np.array([10], dtype=np.uint8) produce, and how would you get the saturated result 255 instead?
Show answer
A 10-bit channel has 2^10 = 1,024 levels, so there are 1,024^3 = 1,073,741,824 colours. The uint8 sum wraps around: 260 - 256 = 4, so the result is [4]. To saturate, widen the dtype first, add, clip, and convert back: np.clip(a.astype(np.int16) + b, 0, 255).astype(np.uint8) gives [255].
Exercise
Which RGB triple (in 8-bit RGB order) gives cyan at full strength, and what triple does the same pixel have if your library stores channels in BGR order?
Show answer
Cyan is green plus blue with no red, so RGB order is (0, 255, 255). In BGR order the first and last entries swap, which here gives (255, 255, 0). That is the same array values you would read as yellow if you wrongly assumed RGB order.