Sub topic 3
The RGB model: red, green and blue channels
This chapter assumes you have read the previous chapter on additive mixing, so you know that a screen makes colour by combining light. It also assumes you can read basic arithmetic. The JavaScript and CSS examples need only basic scripting knowledge. Here the focus is the RGB model itself: what its three numbers mean, how they are arranged, and where the model stops being reliable.
What RGB is
RGB is a colour model that describes a colour as three numbers. One number is the amount of red light, one is the amount of green light, and one is the amount of blue light. Each of these numbers is called a channel. A primary is one of the three base colours that a model mixes to make all the others. In RGB the primaries are red, green and blue.
A colour model is only a way of writing colours down. The same orange can be written as rgb(255 136 0), as #ff8800, or as numbers in some other model. RGB is the natural choice for screens because the hardware works the same way: every pixel is built from three small light sources, and the three numbers say how bright each one should be.
Why red, green and blue
These three primaries are chosen because of the human eye, not because of physics. The retina has three kinds of cone cells. Each kind responds most strongly to a different part of the visible spectrum, roughly long, medium and short wavelengths. The brain builds colour from the relative signals of these three cone types.
A display does not need to reproduce every wavelength of light. It only needs to stimulate the three cone types in the right proportions. Red, green and blue lights are placed so that each one mostly excites a different cone type. By varying their brightness, the display produces a wide range of cone responses, and so a wide range of perceived colours.
Two consequences follow:
- The primaries are not unique. Another display could use slightly different reds, greens and blues and still be an RGB display.
- Some colours cannot be reached with any three real primaries. Highly saturated cyan-greens are a classic case. The chapter on colour spaces and gamut covers this.
How a screen makes the three channels
Most displays divide each pixel into three subpixels, one per primary. An LCD places a coloured filter over each subpixel. An OLED display uses subpixels that emit their own light. At normal viewing distance the eye cannot separate the subpixels, so their light blends into one colour.
When your software says a pixel is rgb(255, 136, 0), the display receives three drive levels:
- The red subpixel is driven at full level.
- The green subpixel is driven at a little over half level.
- The blue subpixel is switched off.
The orange you see is the blend of a bright red, a medium green and no blue.
The RGB cube
Because a colour is three numbers, you can treat it as a point in three-dimensional space. Put red on one axis, green on another and blue on the third. Every possible colour is a point inside a cube. This is the RGB cube.
The eight corners of the cube are the easiest colours to remember. In the common 8-bit notation, where each channel runs from 0 to 255, they are:
| Corner | R | G | B | How it forms |
|---|---|---|---|---|
| Black | 0 | 0 | 0 | No light at all |
| Red | 255 | 0 | 0 | One primary at full |
| Green | 0 | 255 | 0 | One primary at full |
| Blue | 0 | 0 | 255 | One primary at full |
| Yellow | 255 | 255 | 0 | Red and green lights |
| Cyan | 0 | 255 | 255 | Green and blue lights |
| Magenta | 255 | 0 | 255 | Red and blue lights |
| White | 255 | 255 | 255 | All three at full |
The table shows a pattern that is worth memorising. Mixing two primaries gives a secondary colour, and the three secondaries (yellow, cyan, magenta) are exactly the primaries of the subtractive CMYK model you will meet later. Mixing all three at full gives white. This is the reverse of paint, where mixing everything tends toward a dark brown.
Reading a colour from its channels
With a little practice you can estimate a colour from its numbers.
- The largest channel sets the hue family.
rgb(200, 40, 30)is red-dominant, so it is a red. - The gap between largest and smallest sets saturation. A big gap means vivid colour. A small gap means a washed-out colour.
- The overall size of the numbers sets lightness. Higher values are lighter, lower values are darker.
- Equal channels give grey. When R, G and B are the same, no primary dominates, so the result has no hue.
rgb(0 0 0)is black,rgb(128 128 128)is mid grey, andrgb(255 255 255)is white. The greys form the diagonal of the cube from black to white.
Here is a short function that applies the last rule. It is a complete program you can run with Node.js.
function isGrey([r, g, b]) {
return r === g && g === b;
}
function addLight([r1, g1, b1], [r2, g2, b2]) {
// Two lights shining on the same spot add, up to the display maximum.
return [r1 + r2, g1 + g2, b1 + b2].map((v) => Math.min(255, v));
}
const red = [255, 0, 0];
const green = [0, 255, 0];
console.log(addLight(red, green)); // [255, 255, 0] (yellow)
console.log(isGrey([128, 128, 128])); // true
console.log(isGrey([128, 128, 127])); // falseThe Math.min(255, v) call is a clamp. A display cannot be brighter than its maximum, so any sum above 255 is cut to 255. Without it, adding two bright colours would produce values the hardware cannot show.
Writing RGB in CSS
CSS gives you several ways to write the same RGB colour. The three declarations below all describe orange, or close to it.
.swatch {
background-color: rgb(255 136 0);
border-color: rgb(100% 53.3% 0%);
color: rgb(0 0 0 / 50%);
}The first uses 0 to 255 integers. The second uses percentages, where 100% equals 255, and 53.3% of 255 is about 136. The third adds an alpha value after the slash. Alpha is not a colour channel. It is a fourth number that controls opacity, with 0% fully transparent and 100% fully opaque. An RGB colour with alpha is often called RGBA, but the three colour channels still work as before.
Older CSS used commas, as in rgb(255, 136, 0). Modern browsers accept both forms. Pick one and keep it consistent across a project.
Averaging is not the same as mixing
A common mistake is to assume that blending two RGB colours means averaging their numbers. Take red [255, 0, 0] and green [0, 255, 0]. Adding the light gives yellow, [255, 255, 0]. Averaging gives [127.5, 127.5, 0], a dark olive. These are different results answering different questions: the first is two lights both shining, the second is a half-and-half cross-fade.
Both operations are legitimate. Which one you need depends on the job. A cross-fade between two images averages values. Stage lighting adds them. Be clear about which you mean before you write the code.
RGB values are meaningless without a colour space
This is the most important limit of the model. The numbers 255, 136, 0 say how much of each primary to use. They do not say which red, which green and which blue those primaries are. A display with a wider, more saturated red will show 255, 0, 0 as a stronger red than a display with a duller red.
An RGB model that does not name its primaries is called device-dependent. The same triplet can look different from one screen to another. To pin the numbers to a definite colour, the numbers must be paired with a colour space, which defines the exact primaries, the white point and how the numbers map to brightness. On the web the default is sRGB, and you can assume it unless something says otherwise. Colour spaces get their own chapter, so for now remember the rule: a bare RGB triplet is a recipe without a named set of ingredients.
Common mistakes and edge cases
- Treating channels as equally bright. At the same numeric value, green looks much brighter to the eye than blue.
rgb(0 255 0)looks far lighter thanrgb(0 0 255). Do not pick text colours by comparing raw channel numbers. - Out-of-range values. Browsers clamp CSS values outside 0 to 255, so
rgb(300 0 0)renders asrgb(255 0 0). Your own code may not clamp, so do it yourself. - Fractions. CSS accepts decimals such as
rgb(127.5 0 0). Many other tools and file formats round to integers, which can shift a value by one step. - Assuming black is "no ink". In RGB, black is no light. On a dark OLED screen black pixels are truly off. On an LCD they are still lit by the backlight, so black is only close to dark.
- Assuming RGB suits print. Printers use ink, not light. An RGB file sent to print has to be converted, and some vivid screen colours have no printed equivalent. The CMYK chapters cover this.
What to take forward
RGB is three channel values, one per primary, that tell a display how bright to make its red, green and blue subpixels. The colours form a cube whose corners are black, white, the three primaries and the three secondaries. Equal channels give greys. The numbers only become a definite colour when paired with a colour space. The next chapter looks at how the numbers themselves are stored, starting with the 0 to 255 range and 8-bit colour.
Exercise
A designer sets a heading to rgb(30 30 30) and a button to rgb(30 30 31). A teammate says the two are the same grey. Is the teammate right? Then say what colour you get from adding the light of rgb(0 255 0) and rgb(0 0 255), and what a naive sum would give for adding rgb(200 200 0) and rgb(100 100 0).
Show answer
The teammate is only nearly right. rgb(30 30 30) is a true grey because all three channels are equal. rgb(30 30 31) has a blue channel one step higher, so it is technically a very slightly blue-tinted dark grey, though most people cannot see the difference. Adding green and blue light gives cyan, rgb(0 255 255). Summing rgb(200 200 0) and rgb(100 100 0) gives (300, 300, 0), which is clamped to rgb(255 255 0), pure yellow, because the display cannot exceed 255.
Exercise
Two monitors both show the value rgb(255 0 0) for a logo. One looks a deeper red than the other. Explain why, using the terms primary and colour space.
Show answer
The numbers only say "full red channel", not which red. Each monitor has its own red primary, and if the two have different primaries, or the images are not tagged with a colour space that is converted correctly, the same triplet produces different colours. A colour space defines the exact primaries and how the numbers map to light, so pairing the value with a space such as sRGB makes the colour definite.