Color Conversion for Frontend: HEX, RGB, HSL and Contrast Ratios

This piece skips the definitions and gives one executable path: settle a primary → derive neutrals and surfaces → gate every foreground/background pair through WCAG → collapse into tokens. For what HEX, RGB and HSL are, and the formulas that convert them, see colour codes in practice; for palette methods and token rules see the developer colour guide.

1. Fix a hue before asking for a palette

"Make the brand colour a little softer" is unanswerable in RGB: nudging R drifts the hue, nudging G or B tells you nothing about brightness. In HSL the answer is one number — lower S.

Starting from a palette is backwards. A ten-colour set someone handed you carries no knowledge of your brand, so you end up choosing whichever swatch looks least wrong. Pick one hue first, derive every step from it, and keep that hue across the whole system so the interface does not turn into a patchwork.

2. Derive neutrals and surfaces on the same hue

A neutral is not just "some grey": it is the same hue with saturation pushed very low and lightness spread out. That makes large grey areas carry a trace of the brand and feel related to the primary; pure black-and-grey neutrals make the primary look pasted on.

Two ways this fails: saturation not pushed low enough, so large backgrounds visibly skew; and lightness steps spread too wide, so cards, page and overlay lose their hierarchy.

3. Gate every foreground/background pair

Relative luminance is 0.2126R + 0.7152G + 0.0722B over linearised channels, and contrast is (lighter + 0.05) / (darker + 0.05). AA needs 4.5:1 for body text and 3:1 for large text, bold text and UI components; AAA raises those to 7:1 and 4.5:1.

Three things that actually bite:

  • Two text tones on one background usually split one pass, one fail. Measure both the light and dark variants instead of picking by eye.
  • Translucent text must be composited first. A contrast check on rgba() or opacity needs the colour as it renders over the surface; using the declared value overstates the ratio.
  • Borders and icons count too. Non-text UI components are judged at 3:1, not only body copy.

4. Generators produce candidates, not answers

Palette tooling typically takes a base hue, emits analogous or complementary candidates at 60° intervals, expands each into a same-hue lightness ramp, and returns a 10–12 colour set covering primary, neutrals and accent.

That output is a shortlist. Check three things before adopting it: whether analogous neighbours sit so close that hierarchy collapses; whether every swatch has a foreground tone that actually passes; and whether the primary can still work as an accent in both light and dark themes.

5. Collapse everything into tokens

Name every colour a token instead of repeating six-digit literals across components. Keep three layers: a raw layer that stores values and is never referenced directly, a semantic layer (--surface, --text-primary) that components consume, and a theme layer that only remaps semantics under [data-theme="dark"]. Theme switching then touches one mapping, and contrast gets validated once per semantic token.

Reproducible Results

The full conversion chain for this site's brand colour, in one line of Python (matches the colour converter output):

hexs = "0d9488"
r, g, b = int(hexs[0:2], 16), int(hexs[2:4], 16), int(hexs[4:6], 16)
print(f"#{hexs} → rgb({r}, {g}, {b})")     # → #0d9488 → rgb(13, 148, 136)

Feed the resulting RGB through the step-three formula for each pair, or read live numbers in the colour converter.

Frequently Asked Questions

What is the relationship between HEX, RGB and HSL?

They describe the same color in different coordinate systems. HEX is RGB's hex shorthand: #FF8800 = R=0xFF, G=0x88, B=0x00. RGB uses three 0–255 primaries. HSL uses hue H (0–360°), saturation S and lightness L (0–100%). HEX↔RGB is pure base conversion; RGB→HSL normalizes and computes max/min difference and lightness. The intuition: RGB answers 'what the color is', HSL answers 'how light/pure it feels and toward which hue'.

Why is HSL better for picking colors?

Because HSL splits hue/saturation/lightness into three independently adjustable axes: to darken or lighten, change L; to mute, change S; to shift to a neighbor, nudge H. In RGB these are entangled—you can't predict what tweaking one number will do visually. In practice you pick a primary hue, then derive a harmonious family of light/dark and saturated/desaturated variants by holding H and varying S and L—exactly how many palette generators build 'same hue, multiple lightness' sets.

How is contrast computed? What are the WCAG AA/AAA levels?

Contrast = (brighter relative luminance + 0.05) / (darker relative luminance + 0.05). Relative luminance uses linearized RGB with sRGB gamma correction (≈ L = 0.2126R+0.7152G+0.0722B). WCAG levels (normal text): AA ≥ 4.5 for body, ≥3 for large/bold; AAA ≥7 (≥4.5 for large). To decide whether a color pair is usable for body text, compare against these thresholds—the same number a color tool reports is directly matched to the levels.

← Back to Blog