Sub topic 3
The RGB model: red, green and blue channels
This chapter assumes you know that screens emit light and that digital images are grids of pixels. You don't need to know hex or CMYK yet. Later chapters cover channel bit depth, hex notation and gamma.
What RGB describes
RGB is a colour model that describes a colour as three numbers: how much red, how much green and how much blue light to emit. Each number is a channel. A pixel on a screen is made of three tiny light sources, one per channel. Your eye blends them into a single colour because the sources are too small to see separately.
RGB works because human vision has three kinds of cone cells, which respond most strongly to long, medium and short wavelengths of light. Red, green and blue lights stimulate those cones in different proportions. By changing the proportions, a screen can produce a wide range of perceived colours.
How channels combine
RGB is an additive model. Each channel adds light on top of the others, so more light means a brighter result. With all three channels at zero, no light is emitted and you see black. With all three at maximum, you see white.
The table uses a scale of 0 to 255 per channel. The next chapter explains where that scale comes from.
| Colour | R | G | B | What happens |
|---|---|---|---|---|
| Black | 0 | 0 | 0 | No light |
| Red | 255 | 0 | 0 | Only the red source is on |
| Yellow | 255 | 255 | 0 | Red and green light mix |
| Cyan | 0 | 255 | 255 | Green and blue light mix |
| Magenta | 255 | 0 | 255 | Red and blue light mix |
| White | 255 | 255 | 255 | All three at full strength |
| Mid grey | 128 | 128 | 128 | Equal amounts, about half of each |
Yellow from red and green surprises many people, because mixing red and green paint gives a muddy brown. Paint absorbs light, while a screen adds it. That difference is the subject of the subtractive mixing chapter.
Reading an RGB triple
A colour is usually written as a triple in the order red, green, blue. CSS writes it as rgb(255 136 0). This means red at full strength, green at about half and blue at zero. Mostly red with some green gives orange.
Some patterns help you read a triple at a glance:
- Equal values in all three channels give a grey. Larger values give a lighter grey.
- One channel clearly larger than the others sets the hue, which is the basic colour family.
- A small gap between the largest and smallest channel gives a muted, less saturated colour.
- A large gap gives a vivid, saturated colour.
Working with channels in code
You can manipulate colours by changing channel values directly. This function lightens a colour by blending each channel toward white. It returns a new triple and keeps every channel in the valid range.
function mixWithWhite(rgb, amount) {
// amount: 0 keeps the colour, 1 gives white
return rgb.map((v) => Math.round(v + (255 - v) * amount));
}
console.log(mixWithWhite([255, 136, 0], 0.5)); // [255, 196, 128]
console.log(mixWithWhite([255, 136, 0], 1)); // [255, 255, 255]Each channel moves the same fraction of the way to 255. The red channel is already 255, so it stays put. The result is a lighter, less saturated orange.
Common errors
- Assuming RGB is one fixed set of colours. "RGB" names a model, not a specific range of colours. The same triple can look different on two screens, and a colour space defines what the numbers mean. That topic comes later.
- Mixing up the order. Channels are red, green, blue. Swapping red and blue turns orange into a blue-ish colour, which is a common bug when reading raw image data from libraries that use BGR order.
- Going out of range. Adding to a channel can push it above 255, and subtracting can push it below 0. Clamp results to the valid range, or the colour will wrap around or fail.
- Using RGB for print without conversion. A printer does not emit light, so RGB values must be converted to ink amounts first.
- Judging colour by number. Equal numeric steps do not look like equal steps in brightness, because human perception and screen encoding are both non-linear.
Exercise
A pixel has the value rgb(0 200 200). Which two light sources are mostly on, what colour will it look like, and what happens to it if you raise all three channels by the same amount?
Show answer
The green and blue sources are on at 200 and red is off, so the pixel looks like a cyan or turquoise colour. Raising all three channels by the same amount adds red light and more green and blue, which makes the colour lighter and less saturated, moving it toward white.
Exercise
Your code reads an image and an orange pixel (255, 136, 0) appears as a blue pixel (0, 136, 255) after processing. What is the most likely cause?
Show answer
The red and blue channels have been swapped. The code probably reads or writes the data in BGR order instead of RGB order, so you need to reorder the channels.