Colour: Palettes, Contrast and How Others See It
Build a palette from hue, saturation and lightness, convert between HSL and hex, check text contrast for every pair, and simulate how a colour-blind visitor sees the palette, with conversions verified against Python's standard library.
In this lesson you will
- Describe a colour with hue, saturation and lightness
- Generate a palette by hue relationships
- Check contrast for every text and background pair
- Simulate colour-blind vision
About 28 minutes
A calm palette for a calm studio
Lina wants Still Water Yoga to feel calm, clean and natural. Colour is the quickest way to set a mood, and it must also work: text must be readable, links must be recognisable and the main button must stand out. Instead of choosing hex codes by eye, designers describe colours with three numbers in the HSL model. Hue is the position on the colour wheel, in degrees from 0 to 360: 0 is red, 120 is green and 240 is blue. Saturation is how vivid the colour is, from grey at 0 percent to pure at 100. Lightness runs from black at 0 through the pure colour at 50 to white at 100. HSL makes it easy to build related colours: change only the lightness for a tint or shade, or turn the hue for a companion.
Hue relationships give ready palettes. Analogous colours sit next to each other on the wheel, and feel harmonious. Complementary colours are opposite, 180 degrees apart, and give strong contrast, which suits an accent, such as a button against a calm background. A triad uses three hues 120 degrees apart. Whatever the palette, text colours must pass the contrast test from the Front-end course, 4.5 to 1 for normal text. Finally, about one man in twelve and one woman in two hundred have some form of colour vision deficiency, most often reduced sensitivity to red or green, so a design must not rely on hue alone to carry meaning: add text, icons or patterns.
| Relationship | Hue offsets | Feel |
|---|---|---|
| Analogous | -30, 0, +30 | Calm, harmonious |
| Complementary | 0, 180 | Strong contrast, accent |
| Triad | 0, 120, 240 | Lively |
| Monochrome | Same hue, vary lightness | Quiet, cohesive |
function hslToHex(h, s, l) {
s /= 100; l /= 100;
const k = n => (n + h / 30) % 12, a = s * Math.min(l, 1 - l);
const f = n => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
return '#' + [f(0), f(8), f(4)].map(x => Math.round(x * 255).toString(16).padStart(2, '0')).join('');
}
const lum = hex => { const c = [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16) / 255).map(x => x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4)); return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]; };
const ratio = (a, b) => { const [x, y] = [lum(a), lum(b)].sort((p, q) => q - p); return (x + 0.05) / (y + 0.05); };
const base = 190;
const palette = { 'water (base)': hslToHex(base, 45, 38), 'mist (tint)': hslToHex(base, 40, 94), 'deep (shade)': hslToHex(base, 50, 14), 'accent (complement)': hslToHex((base + 180) % 360, 55, 40), 'neighbour (analogous)': hslToHex(base + 30, 40, 38) };
const bg = palette['mist (tint)'], dark = palette['deep (shade)'];
const rows = Object.entries(palette).map(([k, v]) => ({ k, v, onMist: Number(ratio(v, bg).toFixed(2)), onDeep: Number(ratio(v, dark).toFixed(2)) }));
rows.forEach(r => console.log(r.k.padEnd(22), r.v, ' contrast on mist', r.onMist.toFixed(2), ' on deep', r.onDeep.toFixed(2)));
const sim = hex => { const rgb = [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16) / 255); const M = [[0.152286, 1.052583, -0.204868], [0.114503, 0.786281, 0.099216], [-0.003882, -0.048116, 1.051998]]; return M.map(r => Math.round(Math.max(0, Math.min(1, r[0] * rgb[0] + r[1] * rgb[1] + r[2] * rgb[2])) * 255)); };
const prot = Object.fromEntries(Object.entries(palette).map(([k, v]) => [k, sim(v)]));
console.log('protanopia view of base and accent:', JSON.stringify(prot['water (base)']), JSON.stringify(prot['accent (complement)']));
console.log('DATA ' + JSON.stringify({ palette, rows, prot }));
water (base) #357e8d contrast on mist 4.15 on deep 3.01
mist (tint) #eaf4f6 contrast on mist 1.00 on deep 12.50
deep (shade) #123036 contrast on mist 12.50 on deep 1.00
accent (complement) #9e412e contrast on mist 5.78 on deep 2.16
neighbour (analogous) #3a5488 contrast on mist 6.69 on deep 1.87
protanopia view of base and accent: [112,119,142] [83,74,45]
Starting from a base hue of 190 degrees, a blue-green that suggests water, the program built five colours: the base #357e8d, a very light tint #eaf4f6 for backgrounds, a dark shade #123036 for text, a complementary accent #9e412e at the opposite hue of 10 degrees, and an analogous neighbour #3a5488 30 degrees along the wheel. Tints and shades keep the hue and change only the lightness, which is why they look like members of one family. The accent, from the opposite side of the wheel, will draw the eye to the booking button against the cool background.
The contrast table tests every colour against the light background and the dark one. Dark text #123036 on the mist background scores 12.50 to 1, which is excellent, and the base colour on mist scores 4.15, enough for normal text if above 4.5. The accent on mist scores 5.78, and on the dark background 2.16. Those numbers decide where each colour may be used: a pair below 4.5 may serve for large headings or decoration, never for body text. Checking every pair of a palette takes a few lines and prevents the commonest accessibility fault.
The last step simulates protanopia, a form of red-green colour blindness, with a published matrix. The base colour, #357e8d, is seen as RGB [112, 119, 142], and the red-toned accent #9e412e as [83, 74, 45]. Compare them: the red accent loses much of its warmth and moves towards the darker olive values, so if the booking button relied on being “the red one”, some visitors would not see it as special. That is why the button also needs a clear label and a high-contrast outline or fill, not just a hue. Real colour vision varies a lot between people, so treat the simulation as a warning light, and use tools and real testers for important decisions.
Independent check: Python’s colorsys module converted each hue, saturation and lightness triple to RGB, and every channel of every colour agrees with JavaScript’s result within one unit of rounding. The contrast ratios were recomputed in Python with its own implementation of the formula and agree to two decimals, and numpy applied the same matrix to the colours and reproduced the simulated values. Three implementations, two languages, one result.
Check by hand with the HSL formula for the tint: lightness 94 percent and saturation 40 percent mean that the colour is very close to white with a hint of the base hue, so the red, green and blue channels are all high, above 230, which is what the output hex shows. A tint always has high luminance, so it is the right choice for a background behind dark text.
Error states shown only in red, charts distinguished only by hue, and links without underlines are invisible to many people. Add text, icons, patterns or underlines.
Name colours by what they do, such as text, background, accent and border, not by what they look like. Then a redesign changes the values in one place.
Every calculation in this lesson was run while the course was written, in JavaScript (Node.js 22) and checked independently in Python 3.13 (and, where it says so, with real font files). Design guidelines are rules of thumb and tools change, so check current documentation, and test your own designs with real people. The colour-blindness matrix is a published approximation of one kind of colour vision deficiency. Check current accessibility tools for fuller simulations.
Practice
Build a monochrome palette from your own base hue with five lightness steps and check every text and background pair. Find the lightest version of your accent that still passes 4.5 to 1 on white. Print your design in grey scale and see whether the hierarchy survives.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does the hue in HSL describe?
- The position on the colour wheel
- How bright
- How grey
- How dark
In degrees from 0 to 360.
What is a complementary colour?
- A darker version
- A neighbour
- A grey
- The hue opposite on the wheel
180 degrees apart.
Why check contrast for every pair?
- To make colours brighter
- To remove hues
- It is a CSS rule
- To make sure text is readable
4.5 to 1 for normal text.
What does a protanopia simulation show?
- The printed colours
- Dark mode
- How some colour-blind people see the colours
- Browser support
A warning, not a verdict.
Why name colours by role?
- Browsers need it
- It is shorter
- A redesign changes one place
- It lowers contrast
text, background, accent.
Summary
- HSL describes colour as hue, saturation and lightness.
- Palettes come from hue relationships: analogous, complementary and triad.
- Check every text and background pair for contrast.
- Never rely on colour alone, and simulate colour blindness.
Questions
How many colours should a site use?
A small set: one or two brand colours, a neutral range for text and backgrounds, and a few for states such as success and error. Fewer colours look more consistent.
Are there tools for palettes?
Yes, many, and most are based on the same ideas as this lesson. Whatever you use, check the contrast yourself.
What about dark mode?
Provide a second set of values for the same roles, and test contrast again, since pairs that pass on light backgrounds may fail on dark ones.
