Understanding Colour: From Three Numbers to a Whole Theme - Dennis Forbes

· Dennis Forbes on Software Development ·

28 min read Original article ↗

Understanding Colour: From Three Numbers to a Whole Theme

This entry is almost entirely interactive, and needs JavaScript. There are twenty-six demos and very few paragraphs. Nearly all of the demos share one colour: change it in any of them and the rest follow. Nothing leaves your browser. This is a long-in-waiting follow-up to my “yaflaColor” tool1 that was widely used in the 2000s and 2010s. It was a popular tool for interactively finding colour themes and for converting between the colour spaces. The theme generator at the end also has a page of its own, for when you just want to use it.

Spoiler: a colour is three numbers. Every system below is a different choice of which three, and the choice decides what’s easy: building a screen, picking a paint, printing a page, or making six colours look like they belong together.

Light Adds Up

Your eye has three kinds of colour sensor, so three lights are enough to fool it. Not with every colour, it turns out, but with most of the ones you meet in a day2. Start in the dark and add. Drag the lights over one another.

The demo loads here, if JavaScript is on.

Red and green light make yellow. There is no yellow light there at all, and your eye can’t tell the difference3. All three make white.

Three Cones and a Rod

The three sensors are cones, cells at the back of the eye, four or five million of them. Each kind answers to a broad stretch of the spectrum, and they’re named for where on it they peak: short, medium and long wavelengths, or S, M and L. They are not blue, green and red sensors. L peaks in yellow-green, only about 25 nanometres from M, and the two curves overlap nearly everywhere4. And a cone reports one number: how much light it caught, with nothing about which wavelengths that light was5. Three counts are all your brain is sent. Drag along the spectrum.

The demo loads here, if JavaScript is on.

That’s the trick behind yellow. Pure yellow light, at 580 nm, has L near its best and M at about two thirds. A red lamp and a green lamp, set right, put L and M on exactly the same two counts, and with the same three numbers coming in, the brain can’t know which light it was.

There’s a fourth kind of sensor, the rod, and the eye has far more of them: about ninety million, twenty for every cone. A rod can answer to a single photon, but there’s only one kind, and one count can say brighter or darker but never which colour. In daylight the rods are swamped and take no part. By moonlight they’re nearly all you have, which is why a moonlit garden has no colour in it. Switch the demo to night. Rods do best at blue-green and barely notice deep red, so at dusk, as the cones give way, red flowers go dark first while the blues and greens hold on6. A red lamp leaves the rods alone, which is why astronomers read their charts by one.

Why Blue Is Blurry

The cones don’t come in equal numbers either. On average there are about two L cones for every M, but people with ordinary colour vision have been measured at anywhere from about even to sixteen to one, and they still name colours the same7. S cones are rare, well under one in ten, and in the very centre of your gaze, the spot you point at whatever you’re trying to read, there are none at all.

That matters because detail is carried by brightness, and the eye builds its sense of brightness from L and M alone8. S adds nothing to it. The fine detail of a scene is caught by L and M cones packed tight at the centre. The S cones, spread thin and missing from the middle, can tell you a thing is blue, and not much about its shape.

Then the eye’s lens isn’t corrected for colour. A camera lens is several pieces of glass, chosen so that every colour comes to the same focus. The eye has one lens, so when yellow-green is sharp, blue comes to a focus a little short of the retina: most of a dioptre short, which is about what a mildly near-sighted person’s glasses put right. Up close the eye can refocus for it. For anything far away its focus is already as relaxed as it goes, and blue would need it to relax further. At night the pupil opens wide, and the same error spreads into a wider smear.

All of which is why lettering that glows blue is a blur from down the road while green or red is crisp. Here’s the centre of a retina, two thirds of a degree of it, with one letter of a sign falling on it. Try blue at night.

The demo loads here, if JavaScript is on.

It’s a reason to keep thin text out of pure blue on screens too. The contrast check further down fails #0000FF on black, at 2.4 to 1, because it counts blue as only 7% of white’s brightness. That’s the same S cones, seen from the other end.

Image and video compression is built on the same fact. A JPEG, or almost any video you stream, doesn’t store red, green and blue. It stores one number for brightness and two for colour: roughly, how much bluer the pixel is than its brightness suggests, and how much redder. Every pixel keeps its own brightness. In nearly all video, and most JPEGs, the colour is kept only once for each square of four pixels. That throws away three quarters of the colour detail, and halves the data before any of the cleverer compression has started9. Hardly anyone notices, because the eye takes its fine detail from brightness and sees colour far more coarsely.

Blue loses the most. It counts for a tenth or less of the brightness number, so nearly everything that makes blue look blue is in the colour numbers, the ones cut to a quarter. That’s the bet working as intended: the sliver of blue that’s kept sharp is roughly what L and M can see of it, and what gets blurred is what only the S cones see, which they couldn’t have made out anyway. On photographs the bet pays. It loses on screenshots, where thin coloured lettering on flat colour is the whole picture, which is why a screenshot saved as a JPEG goes fuzzy around red and blue text long before a photo shows anything.

RGB

So a screen is told how hard to drive each of the three lamps. One byte each, 0 to 255, and the six-digit hex code is those same three bytes written in base 16.

The demo loads here, if JavaScript is on.

Each slider’s track shows what moving it would do, given the other two. Notice how hard it is to get anywhere on purpose. “The same, but lighter” means moving all three by different amounts. RGB is how the machine thinks, and nobody else does.

How Many Bits Is Enough

Eight bits a channel, over the three channels, is 16,777,216 colours. That counts every shade of every hue as its own colour: the hues you could name are few, and nearly all of the sixteen million are lighter, darker and greyer versions of them. Take bits away and see when you notice a difference. The rainbow hides it well. The night sky under it, which is all slow change between dark tones, gives up much sooner.

The demo loads here, if JavaScript is on.

The Numbers Aren’t the Light

One catch that bites everyone eventually: the stored number is not proportional to the light. 128 is not half as bright as 255. It’s about a fifth. Drag along the chart.

The demo loads here, if JavaScript is on.

The bend is deliberate. Your eye notices a small change in a dark tone far more readily than the same change in a bright one, so levels spent evenly on light are wasted at the top and scarce at the bottom. The curve spends them evenly on what you can tell apart instead. Pull the bits down and the second strip falls apart in the shadows while the first holds10.

Photographers count light in stops. One stop brighter is twice the light and one stop darker is half, so three stops is eight times as much and ten stops is about a thousand times11. Counting in doublings suits the eye, which judges brightness by ratios more than by amounts: a second candle beside the first is a big change, and a second one beside fifty is hardly any. The usual place to count from is mid grey, a card that reflects 18% of the light falling on it and looks halfway between black and white.

Switch the chart to stops, and the same idea keeps going:

  • Camera log. A sensor records maybe fourteen stops and a screen’s white is only two and a half stops above mid grey. A log curve gives every stop about the same share of the numbers, so the highlights survive to be dealt with later. Shown raw it looks flat and grey, which is what the last strip is.
  • PQ and HLG, the two HDR curves. Same purpose, for screens that really can go that bright. PQ was built directly from measurements of the smallest step in brightness a person can see12.

The practical consequence for anyone writing code: if you average two colours’ numbers, you don’t get the average of their light.

The demo loads here, if JavaScript is on.

Red to green is the famous one: the naive blend goes through a muddy brown that is darker than either end.

Negative Red

Back at the start, three lights could fool your eye with most colours, but not all of them. How anyone found that out is where the chart in the next section comes from.

In England in the 1920s, W. D. Wright and John Guild, working separately, sat seventeen people in front of something very like that first demo13. Each looked at a small circle split down the middle. One half showed a single pure wavelength, and the other was lit by three lamps, red, green and blue, with a knob for each. Turn the knobs until the halves match, write down the three settings, and move along the spectrum.

Most of it could be matched. The blue-greens could not. Green and blue together came out paler than the real thing, and adding red only made them paler still. What worked was putting the red on the other side, paling the pure colour down to meet the mix. Light added over there counts as light taken away here, so it went into the notebook as a negative amount of red. Any three lamps you choose have the same problem: some pure colours are always out of reach. The cone curves say why. L’s curve covers M’s nearly everywhere, so no lamp can move M without moving L as well, and a pure blue-green gets more M for its L than any mix of lamps can.

Nobody wanted to do sums with minus signs in them by hand, so in 1931 the international lighting commission, the CIE, swapped the three real lamps for three imaginary ones, chosen so that every colour comes out in positive amounts. No lamp can make them. They are bookkeeping, called X, Y and Z, and Y was picked to be the brightness on its own.

Last comes the flattening. Divide a colour’s X, Y and Z by their total and two numbers are left, x and y, which say what colour it is but not how bright. Plot them for every pure wavelength and you get the curve below. Matches add up, and the dividing keeps straight lines straight, which is why mixing two lights lands on the line between them. And with brightness divided out, brown isn’t on the chart anywhere. Brown is a dark orange, and the dark is gone: pick a brown and its dot lands among the oranges.

A Fourth Lamp

If three lamps are good, are four better? Around 2010 Sharp sold televisions with a yellow sub-pixel beside the red, green and blue, and plenty of OLED televisions today have a white one.

Here is that chart mentioned in the prior section, known as the CIE 1931 colour space. The curved edge is the pure wavelengths, and mixing two lights gives you the colours on the straight line between them, so three lamps can reach exactly the triangle between them and nothing outside it. The great bulge of blue-greens and greens past its upper-left side is the negative red, drawn. A fourth lamp makes the triangle a quadrilateral. It does not add a dimension: your eye still has three kinds of sensor.

The triangle is the standard itself. sRGB doesn’t say what wavelengths a screen must emit. It names four points on this chart, one each for red, green and blue, and one for white, where all three at full have to land. Any light that lands on a point will do. None of the three lamps is a pure wavelength, or it would sit on the curved edge. Each is a broad smear of wavelengths that the eye reads as that point, and a tube, an LCD and an OLED get there with three quite different smears14.

The demo loads here, if JavaScript is on.

Try yellow, then cyan. Yellow buys about 3% more colours on an sRGB screen, because the red-to-green edge of the triangle already runs close to the curve there. Cyan buys about 31%15. The room was always on the other side. Switch to the 1976 chart and drag the lamp along the bottom, and there’s more room still, in the purples that the 1931 chart squashes almost flat.

So why yellow? Brightness. A yellow sub-pixel lets through far more light than a red and a green one side by side, which is the same reason for the white one in an OLED panel. And there is a catch that no fourth lamp gets around: the picture still arrives as three numbers. Nobody ever broadcast a yellow channel, so the set has to invent the fourth value from the other three, and it cannot put back colours the signal never carried16.

Every Panel Is a Little Wrong

The standard names three points, and a factory turns out panels that land near them, not on them. Two screens off the same line differ a little in where their lamps sit on the chart, in how their brightness bends, and in whether a mid grey comes out faintly pink or faintly green. So the better ones are measured before they’re boxed. A sensor is held to the glass, the panel is shown a run of known colours, and the gap between what was asked for and what came out is written into the display as a lookup table, a LUT. Every colour the computer sends goes through the table first, and comes out as whatever this particular panel has to be told to land on the right point17.

A table can only take away. If the panel’s red is deeper than sRGB’s, it can water it down with a little green and blue to land on the standard’s point. If it falls short, nothing can push it further out, which is the triangle again. So a panel that’s meant to be corrected is built a little wider than its target, and with more bits inside than it’s sent, so the reshuffling doesn’t show up as the banding from earlier.

The white the table aims for is the standard’s fourth point, and it has a name: D65, the CIE’s average of midday daylight, worked out from measurements of real skies. It’s the dot marked on the chart above. Heat something until it glows and its colour runs along the thin curve beside it, from a candle’s orange at around 1,900 K, through an old light bulb at 2,700 K, to daylight and on into blue. A white’s temperature is the point on that curve it sits nearest, and D65’s is about 6,500 K18.

Here is one panel, made up, and drawn in your own screen’s greys. Start with no table, then give it the factory’s.

The demo loads here, if JavaScript is on.

Then the panel ages, and the table is still describing the one that left the factory. Drag the years along with the factory’s table on. Backlights dim and drift. OLEDs have it worse, because every sub-pixel is its own lamp and wears with use. Blue tends to wear fastest, so an old OLED’s white slides toward yellow, and anything left on screen long enough, a channel logo or a taskbar, wears its own pixels more than their neighbours. None of it shows from one day to the next, because your eye soon takes whatever white it’s given as white. A screen that has slid a few hundred kelvin toward yellow looks fine until something new sits beside it, which is why it has to be measured, not eyeballed.

So the careful ones measure again. Monitors for photo and film work often have a sensor in the bezel that swings out on a schedule and rebuilds the table by itself, and most others can be fixed with a colorimeter, a puck hung over the screen for ten minutes or so. OLED televisions do a rough version whenever they’re switched off: they keep track of how much each sub-pixel has been used, and run a short pass that lifts the tired ones to match the rest. That works for as long as there’s spare drive to lift them with. Once a worn patch has used it up, nothing is left to lift it, and that is when burn-in shows. Measure the demo again and keep dragging to watch it happen.

Brighter Than White

Everything so far has had a ceiling. 255 is white, and white is as bright as the screen is prepared to go. High dynamic range, HDR, takes the ceiling off. It is not a brighter picture: a white page stays about where it always was. The new room is above it, for the things that really are brighter than a page, like the sun, a flame, or a glint off chrome. Brightness is measured in nits. Drag along the chart.

The demo loads here, if JavaScript is on.

So is HDR just more bits? It needs more, but that isn’t what it is. Range is how far apart the two ends are, and bits are how fine the steps between them are. Stretch eight bits over a range this long and the steps show, so HDR video uses ten or twelve. It also swaps the old curve for PQ, which spends them better: far more in the shadows, where the old curve all but runs out, and fewer at the top.

The demo loads here, if JavaScript is on.

Then the catch. A PQ number means a particular brightness, not a share of whatever the screen has, and almost no screen has all of it. A film can ask for 1,000 nits from a laptop that stops at 400, and the laptop has to decide what to do about it. This is tone mapping, and it’s why the same film looks different on every HDR television.

The demo loads here, if JavaScript is on.

For a colour theme, none of this changes anything, and that’s on purpose. An interface stays in the ordinary range, because a button brighter than the page’s white is a button nobody wants to look at19.

The Same Colours, Rearranged

RGB is a cube: three axes at right angles. Every other system on this page that describes a screen is that same cube, bent into a more convenient shape. Each dot keeps its colour throughout. Only its address changes.

The demo loads here, if JavaScript is on.

Stand the cube on its black corner and look down from white. The six vivid corners make a hexagon, and the angle round it is what we call hue. That one move is the whole idea behind HSV and HSL.

HSV and HSL

Both keep hue as an angle, and both add “how colourful” and “how bright”. They differ only in where white goes.

  • HSV (or HSB): value is the brightest of the three channels. Full value and full saturation is the pure colour. White is full value with no saturation. This is the square in nearly every colour picker.
  • HSL: lightness runs from black through the pure colour at 50% to white at 100%. This is the one CSS speaks.

The demo loads here, if JavaScript is on.

The same colour has the same hue in both, and usually a different saturation. “50% saturated” means nothing until you say which system.

The Lie in “Lightness”

Both are just arithmetic on RGB, and they know nothing about eyes. HSL insists that pure yellow and pure blue have the same lightness: 50%. Take the colour out and see.

The demo loads here, if JavaScript is on.

OKLCH

The fix is a space built from measurements of what people actually see. CIE Lab was the 1976 attempt, and OKLab (2020) is the one that finally got into CSS20. Its polar form, OKLCH, has the same three ideas as HSL: lightness, chroma (colourfulness), hue. The difference is that the lightness is honest: change the hue and it keeps looking as light as it did.

The demo loads here, if JavaScript is on.

The price is that the space is not a tidy box. You can ask for colours no screen can show, and the tracks above flatten out where sRGB runs out of room. This is the space the theme generator at the end works in.

Ink Subtracts

Paper starts white and ink takes light away. Cyan ink absorbs red, magenta absorbs green, yellow absorbs blue. They are the exact opposites of the three lights, which is not a coincidence.

The demo loads here, if JavaScript is on.

CMYK

In theory cyan, magenta and yellow together make black. Real inks make a wet, expensive brown. So printers add a fourth, black, the K21, and hand it whatever grey the other three have in common.

The demo loads here, if JavaScript is on.

Pull the last slider down: the colour doesn’t change, but the ink bill does. Many different CMYK recipes give the same colour, which is one reason CMYK numbers mean nothing without knowing the press, ink and paper they were made for22.

Dots

A press can’t print 40% cyan. It can print cyan or not. So it prints dots of different sizes and lets your eye average them, each ink’s grid turned to its own angle so the four don’t beat against each other.

The demo loads here, if JavaScript is on.

One Colour, Ten Spellings

Type into any of these.

The demo loads here, if JavaScript is on.

Colours That Go Together

Colour theory is mostly geometry on a wheel: pick a hue, then pick others by their angle from it.

Which wheel, though? Opposite red on a screen’s wheel is cyan. Opposite red on the painter’s wheel you learned at school is green. Neither is wrong. They’re spaced differently, and the traditional harmonies were worked out on the painter’s.

The demo loads here, if JavaScript is on.

Drag any dot. The usual advice for using a scheme is the 60-30-10 rule: one colour gets most of the room, a second supports it, and the loudest gets a tenth. The little poster does that.

Tints, Shades and Tones

One hue goes a long way. Add white for a tint, black for a shade, grey for a tone.

The demo loads here, if JavaScript is on.

Company Matters

No colour is ever seen alone, and the neighbours change it.

The demo loads here, if JavaScript is on.

Colours for Everyone

Two checks before any palette ships.

Contrast. Text needs enough difference in luminance from its background. Hue doesn’t count. The usual bar is 4.5 to 1 for body text23.

The demo loads here, if JavaScript is on.

Colour vision. About one man in twelve, and one woman in two hundred, has some form of colour vision deficiency, most often trouble telling red from green. If two colours in your palette differ only by hue, some people will see one colour.

The demo loads here, if JavaScript is on.

Everything above, put to work. Pick one colour and a harmony, on whichever of the three wheels you trust, and get:

  • Scales: eleven steps of each hue, built in OKLCH so that step 600 of one is as light as step 600 of any other.
  • Roles: which step is the page, the card, the text, the button, in light and in dark, chosen so the contrast holds.
  • Something to paste: CSS variables, Tailwind, Sass or JSON.

It also lives on a page of its own, so you can come back to it without scrolling past everything else.

The demo loads here, if JavaScript is on.

A few things it’s doing that are worth stealing even if you never use it:

  • Your colour is kept exactly, at whichever step it’s nearest. The rest of that scale is built round it.
  • Unless you pick another wheel, supporting hues are found by turning the OKLCH hue, so a triad looks evenly spaced instead of merely being evenly spaced.
  • Buttons get white labels if that passes and dark ones if it doesn’t. It’s decided by measuring. A button too pale to stand out from the page gets an edge that does, or, if you pick Always solid, is moved along the scale until it stands out by itself.
  • Dark mode is not the light theme inverted. It’s the same scales, read from the other end.
  • The neutrals carry a little of the brand hue. Pull that slider to zero and watch the whole thing go slightly dead.
  1. I Americanized the spelling of that original tool as it was hosted on a .com domain. Now that this is on a .ca domain, it’s back to Canadian spelling. ↩

  2. Measured by area on the chart further down, an ordinary screen’s three lamps reach only about a third of what the eye can see. That sounds worse than it is. Most of the rest is very vivid colour, of a kind almost nothing around you reflects: it takes something like a laser or a prism to get out there. ↩

  3. Two different mixes of light that look identical are called metamers, and every colour screen ever made depends on them. It’s also why a shirt can match your trousers in the shop and not in daylight. ↩

  4. The textbook peaks, about 420, 530 and 560 nanometres, are for the pigments on their own. In a living eye the lens, and a yellow pigment over the centre of the retina, soak up some of the blue on the way in, so measured from the front of the eye, as the curves here are, the peaks sit nearer 440, 545 and 570. ↩

  5. It’s called the principle of univariance. A cone can’t tell a little light at the wavelength it likes best from a lot of light at one it barely notices, and that is the whole reason three numbers are enough. ↩

  6. It’s called the Purkinje shift, after Jan Purkyně, who described it in 1825. ↩

  7. Counted by photographing the cones of living eyes, through optics that cancel out the eye’s own blur. The mosaics look like the demo below, and they’re just as jumbled. Nobody yet knows quite how the brain sorts out which cone is which. ↩

  8. The CIE’s 2006 brightness curve is defined exactly that way: about two parts L to one part M, and no S at all. ↩

  9. It’s called chroma subsampling, and the one-in-four version is written 4:2:0. The brightness number, luma, is worked out from the stored numbers rather than from the light, the same shortcut the mixing demo above warns about, so a little real brightness ends up in the colour numbers and gets cut along with them. That’s why strongly coloured edges can come back a touch dark. A JPEG also rounds its colour off more coarsely than its brightness. Video meant for editing keeps more colour, 4:2:2 or the full 4:4:4, and so does good screen sharing, where the picture is mostly text. ↩

  10. The usual story is that the curve exists to cancel out the response of cathode-ray tubes, and that is where the number 2.2 came from. It’s a lucky accident that a tube’s response is close to the inverse of the eye’s, and it’s why the curve outlived the tube. ↩

  11. The name comes from the camera lens, whose aperture settings are called f-stops: f/2.8, f/4, f/5.6 and so on, each letting in half the light of the one before. Shutter speeds and film speeds came to be counted the same way, so a photographer can trade a stop of one for a stop of another. ↩

  12. PQ, the “perceptual quantizer”, is absolute: a given number means a given brightness, up to 10,000 nits, whatever the screen. HLG is relative, like sRGB, and was designed so that an ordinary television showing it still looks about right. ↩

  13. Wright’s three lamps were pure wavelengths, at 650, 530 and 460 nanometres. Guild’s were filtered, and the CIE converted both sets of results to one common three before averaging them. Seventeen people looking at a patch about the width of a thumbnail at arm’s length is a slim foundation, and it shows: the 1931 numbers are known to be off at the blue end, and better ones have been published since. sRGB, and nearly everything else, is still written in the 1931 ones. ↩

  14. The points were borrowed from the HDTV standard, Rec. 709, which chose them to match the phosphors in the tubes of the day, so the sRGB triangle is more or less a portrait of a good 1990s television. Draw a line from white through each point out to the edge and you get its nearest pure hue: about 612, 549 and 464 nanometres. Rec. 2020 is the one that does what you might have expected. Its three points are on the edge itself, at 630, 532 and 467 nanometres, which only something like a laser can reach. That is why screens quote the percentage of it they cover. ↩

  15. The 1931 chart isn’t fair about size. Its big green lobe is far more room than the eye can actually tell apart, so the percentages are measured on the CIE’s 1976 redraw of it, u′v′, which spreads the colours out more evenly. The buttons above the diagram switch between the two, so you can see the room being counted. Neither is perfect: both leave out lightness, and the violets in the bottom corner are very dim colours. ↩

  16. What does get past it is a better signal. Display P3 and Rec. 2020 are still three numbers, with the three lamps moved further apart, and the picture has to be made for them in the first place. The buttons above the diagram show how much each one reaches. ↩

  17. The table lives in the display when the display has room for one, and otherwise in the computer, as a profile the operating system applies on the way out. OLED makers go a step further and photograph every panel, storing a correction for each pixel so that a flat grey comes out flat. The trade calls it demura, from the Japanese mura, unevenness. ↩

  18. Which runs backwards from the everyday words: a “warm” white is the cooler one. Not every standard picked the same white, either. Printing judges its proofs under D50, at about 5,000 K, and CSS’s lab() is measured against D50 too. ↩

  19. CSS has a property for exactly this, dynamic-range-limit, which lets a page hold somebody else’s HDR photo down to its own white. ↩

  20. Björn Ottosson’s original write-up is short and very readable. oklch() has worked in every current browser since 2023. ↩

  21. K for “key”, the plate the others were lined up against. Also, usefully, not B, which was taken by blue. ↩

  22. The CMYK here is the textbook formula, which is honest about the idea and useless for an actual print job. Real conversion goes through an ICC profile that describes one particular press, ink and paper. ↩

  23. These are the WCAG 2 thresholds: 4.5 to 1 for body text and 3 to 1 for large text at level AA, 7 and 4.5 at AAA. The formula has known blind spots, especially for light text on dark backgrounds, and its eventual successor works differently. For now it’s the one the rules are written in. ↩

See Also