How HEX to HSL conversion works
HSL rearranges the same screen colour into hue (which colour, as an angle), saturation (how far from grey) and lightness (how far from black or white). To get there from HEX you first decode the three channels, then measure the brightest and dimmest of them: their average is lightness, their spread is saturation, and which channel is brightest decides the hue sector.
- Decode the HEX pairs to R, G, B (0–255), then divide each by 255 to get R′, G′, B′ between 0 and 1.
- Cmax = max(R′, G′, B′), Cmin = min(R′, G′, B′), Δ = Cmax − Cmin.
- L = (Cmax + Cmin) ÷ 2.
- S = 0 if Δ = 0, otherwise Δ ÷ (1 − |2L − 1|).
- H = 60° × ((G′ − B′) ÷ Δ mod 6) if R′ is largest; 60° × ((B′ − R′) ÷ Δ + 2) if G′ is largest; 60° × ((R′ − G′) ÷ Δ + 4) if B′ is largest.
- Round to whole degrees and percentages:
hsl(H, S%, L%).
Worked example: #3B82F6 to HSL
Here is the converter's own calculation for #3B82F6, step by step. The result, hsl(217, 91%, 60%), is exactly what the tool above shows for this input.
- Split into pairs: 3B, 82, F6.
- Red: 3B = 3 × 16 + 11 = 59.
- Green: 82 = 8 × 16 + 2 = 130.
- Blue: F6 = 15 × 16 + 6 = 246.
- R′ = 59 ÷ 255 = 0.231, G′ = 0.51, B′ = 0.965.
- Cmax = 0.965, Cmin = 0.231, Δ = 0.733.
- L = (0.965 + 0.231) ÷ 2 = 0.598.
- S = 0.733 ÷ (1 − |2 × 0.598 − 1|) = 0.912.
- Blue is largest: H = 60° × ((0.231 − 0.51) ÷ 0.733 + 4) = 217.2°.
- Rounded:
hsl(217, 91%, 60%).
Tailwind CSS blue scale in HEX and HSL
Tailwind's default blue, from 50 to 950. Note that the scale doesn't hold hue fixed: it drifts a few degrees as lightness changes.
| Colour | HEX | HSL |
|---|---|---|
| blue-50 | #EFF6FF | hsl(214, 100%, 97%) |
| blue-100 | #DBEAFE | hsl(214, 95%, 93%) |
| blue-200 | #BFDBFE | hsl(213, 97%, 87%) |
| blue-300 | #93C5FD | hsl(212, 96%, 78%) |
| blue-400 | #60A5FA | hsl(213, 94%, 68%) |
| blue-500 | #3B82F6 | hsl(217, 91%, 60%) |
| blue-600 | #2563EB | hsl(221, 83%, 53%) |
| blue-700 | #1D4ED8 | hsl(224, 76%, 48%) |
| blue-800 | #1E40AF | hsl(226, 71%, 40%) |
| blue-900 | #1E3A8A | hsl(224, 64%, 33%) |
| blue-950 | #172554 | hsl(226, 57%, 21%) |
Source: Tailwind CSS v3.4 default palette (tailwindcss 3.4.17)
Precision test: every HEX value through the converter
Whole-number HSL is coarser than HEX. Across all 16,777,216 HEX codes the converter produced only 2,427,032 different HSL values, and when we converted those back, just 10.583% of the original codes came back exactly. The worst drift was 5 levels on one channel (#02E4E6 returned as #02DFE3); the average was 1.2753. Invisible on screen, but it means a HEX → HSL → HEX round trip will often change the code in a design token file.
Method: every whole-number HEX value, through the converter's own code and back, compared on the 0–255 scale (how we test).
| Measure | Result |
|---|---|
| HEX values tested | 16,777,216 |
| Distinct HSL results | 2,427,032 |
| Came back exactly | 10.583% |
| Largest change (0–255 levels) | 5 |
| Average change | 1.2753 |
Last updated · Source: Pixohub exhaustive test of lib/color-math.ts
Where you'll use HEX to HSL
Building tints and shades in CSS
With hsl() you change one number to get a lighter or darker version: hsl(217 91% 60%) to hsl(217 91% 75%). Doing the same in HEX means recalculating all three pairs. The Tailwind blue scale below shows how a real design system steps lightness while drifting hue and saturation slightly.
Theming with custom properties
Many design systems store hue and saturation as CSS variables (--brand-h: 217) and compute lightness per component, so a single hue change re-themes everything. That pattern starts with converting the brand HEX to HSL.
Checking contrast by eye
Lightness gives a rough sense of how light a colour looks, though it isn't perceptual: yellow at L = 50% looks far lighter than blue at L = 50%. For accessibility checks use a contrast-ratio tool rather than HSL lightness.
Common mistakes
- Expecting HSL saturation to match the S in a Photoshop or Figma HSB picker: they are different formulas, so the same colour has different S values (see the HEX to HSV converter).
- Storing colours as rounded HSL and converting back to HEX for production: as the test above shows, only about one code in ten survives exactly.
- Pasting values from Python's
colorsys.rgb_to_hls(): it returns hue, lightness, saturation in that order and on a 0–1 scale. - Using the classic Windows colour dialog or Microsoft Office's HSL fields: they use 0–240 and 0–255 scales respectively, not degrees and percentages.