Sub topic 6
Colour from Red, Green, and Blue
Assume you know basic Python and can read an image as an array. This chapter focuses on the additive RGB model: representing each pixel with separate red, green, and blue channel values.
The additive RGB model
RGB is a colour model in which light from three primary channels is added together. A pixel is represented by a tuple:
(red, green, blue)
Each value describes the strength of one channel. With 8-bit channels, each value ranges from 0 to 255:
(0, 0, 0)is black because no light is present.(255, 0, 0)is red.(0, 255, 0)is green.(0, 0, 255)is blue.(255, 255, 255)is white because all three channels are at full strength.(255, 255, 0)is yellow because red and green light are combined.
This is called additive colour because increasing a channel adds light. It differs from paint or ink, where mixing pigments removes light.
import numpy as np
pixels = np.array([
[0, 0, 0], # black
[255, 0, 0], # red
[0, 255, 0], # green
[0, 0, 255], # blue
[255, 255, 0], # yellow
[255, 255, 255], # white
], dtype=np.uint8)
print(pixels.shape) # (6, 3)
print(pixels[4]) # [255 255 0]The array has six pixels and three values per pixel. In a complete colour image, the usual shape is (height, width, 3). The final dimension contains the RGB channels.
Channel strength and colour
The same RGB tuple can be scaled to a range from 0 to 1. This is useful for calculations:
r = \frac{R}{255}, \quad
g = \frac{G}{255}, \quad
b = \frac{B}{255}For example, (128, 64, 0) is approximately (0.502, 0.251, 0.0). It contains half-strength red, quarter-strength green, and no blue.
| RGB tuple | Result | Channel interpretation |
|---|---|---|
| `(255, 128, 0)` | Orange | Full red, half green, no blue |
| `(64, 64, 64)` | Dark grey | Equal, low channel strengths |
| `(128, 128, 128)` | Mid grey | Equal, medium channel strengths |
Visualising channel mixing
RGB is convenient for displays and cameras because these devices commonly control or measure red, green, and blue components. However, RGB is not always convenient for selecting colours, comparing visual differences, or storing images efficiently. HSV, YCbCr, and Lab reorganise related information for those purposes.
Exercise
What colour does the 8-bit RGB tuple (20, 200, 20) represent approximately, and why?
Show answer
It represents a bright green. The green channel is high, while red and blue are both low.