This piece covers two things the formula lists online never do: where the formulas break on real input, and why contrast cannot be estimated from HSL lightness. The selection workflow lives in the colour conversion guide; palette methods and design tokens in the colour theory guide. For live conversion use the in-site colour converter.
Three coordinate systems, three jobs
| Model | Channels | Best for |
|---|---|---|
| HEX | #RRGGBB |
literals in code, design handoff |
| RGB | R/G/B, 0–255 each | editing numbers, pixel-exact reproduction |
| HSL | H 0–360°, S and L 0–100% | adjusting colours, deriving a hue family |
HEX is just RGB written in base 16: #FF8800 is R=255, G=136, B=0, so the conversion is pure radix arithmetic with no loss — anything that goes wrong is a parsing or zero-padding mistake.
HSL is the genuinely different view. RGB answers "how much light does each channel emit"; HSL answers "which hue is this, how pure, how light". The practical difference is predictability, not taste: in HSL, moving L from 50 to 70 is understood as brightening, whereas in RGB, moving R from 200 to 230 may brighten, darken or shift hue. Because the three channels are coupled, RGB is the wrong tool for a lightness-only change; use L instead.
The three conversions
HEX to RGB is radix arithmetic, provided parseInt sees exactly six digits:
// HEX → RGB
const n = parseInt(hex.slice(1), 16)
const rgb = [(n >> 16) & 255, (n >> 8) & 255, n & 255]
// RGB → HEX
const hex = '#' + rgb.map(v => v.toString(16).padStart(2, '0')).join('')
RGB to HSL normalises, then derives S and L from the max/min pair:
function rgbToHsl(r, g, b) {
r /= 255; g /= 255; b /= 255
const max = Math.max(r, g, b), min = Math.min(r, g, b)
const l = (max + min) / 2
if (max === min) return { h: 0, s: 0, l }
const d = max - min
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
let h
if (max === r) h = ((g - b) / d + (g < b ? 6 : 0))
else if (max === g) h = (b - r) / d + 2
else h = (r - g) / d + 4
return { h: h * 60, s, l }
}
HSL to RGB reverses it, with a branch worth noticing:
function hslToRgb(h, s, l) {
h = ((h % 360) + 360) % 360 / 360
if (s === 0) { const v = Math.round(l * 255); return [v, v, v] }
const q = l < 0.5 ? l * (1 + s) : l + s - l * s
const p = 2 * l - q
const f = t => {
t = (t + 1) % 1
if (t < 1 / 6) return p + (q - p) * 6 * t
if (t < 1 / 2) return q
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
return p
}
return [f(h + 1 / 3), f(h), f(h - 1 / 3)].map(v => Math.round(v * 255))
}
The input-edge table
| Input | Symptom | Fix |
|---|---|---|
3-digit shorthand #f80 |
slice(1) yields three characters and every channel is wrong |
expand to six digits first |
Greyscale, max === min |
difference is zero, hue evaluates to NaN |
take the early-return branch |
| Negative or >360 hue | hue falls outside 0–360 and the RGB drifts | normalise with ((h % 360) + 360) % 360 |
| Out-of-range or fractional channels | output 256 or negative values, silently clipped by CSS | clamp to 0–255 before rounding |
Computing contrast, and the thresholds
WCAG contrast walks relative luminance, not HSL lightness: inverse-gamma each sRGB channel, weight as 0.2126R + 0.7152G + 0.0722B, then take (Lmax + 0.05) / (Lmin + 0.05). Thresholds are 4.5:1 for AA body text and 7:1 for AAA, easing to 3:1 and 4.5:1 for large text (18pt, or 14pt bold).
Why L cannot stand in for luminance
The reason is one sentence: HSL's L is a normalised geometric midpoint shared across hues, while luminance is physical. The same L=50 lands on wildly different brightness depending on hue — pure yellow #FFFF00 reads L=50% yet reaches relative luminance 0.928, only 1.07:1 on white and unusable as body text; pure blue #0000FF also reads L=50% but hits 8.59:1 and clears AA comfortably; mid grey at L=50 sits at 0.184, or 4.73:1. So a brand colour paired with light and dark text variants cannot be judged by equal L — compute both pairs.
One hex step at the compliance boundary
On white, #767676 measures 4.54:1 and passes AA for body text, while #777777 measures 4.48:1 and fails. A single step in one channel flips the verdict, because channels are quantised to 8 bits and luminance therefore moves in discrete jumps; around 4.5:1 the passing band is extremely narrow. Two things get misread: compliance refers to that specific foreground/background pair rather than the brand colour in isolation, and 4.54:1 clears AA while staying far from AAA's 7:1. Verify numbers in the colour converter — and that is why colours belong in tokens: validated once, applied everywhere.