How RGB to HSL conversion works
RGB says how much red, green and blue light to mix; HSL says what colour it is and how light and vivid. The conversion looks at the brightest and dimmest channels: their midpoint is lightness, their difference (scaled for lightness) is saturation, and the order of the channels gives the hue angle.
- R′ = R ÷ 255, G′ = G ÷ 255, B′ = B ÷ 255.
- Cmax, Cmin and Δ = Cmax − Cmin.
- L = (Cmax + Cmin) ÷ 2.
- S = Δ ÷ (1 − |2L − 1|), or 0 when Δ = 0.
- H from whichever channel is largest (red: 60° × ((G′ − B′) ÷ Δ mod 6); green: 60° × ((B′ − R′) ÷ Δ + 2); blue: 60° × ((R′ − G′) ÷ Δ + 4)).
Worked example: rgb(16, 185, 129) to HSL
Here is the converter's own calculation for rgb(16, 185, 129), step by step. The result, hsl(160, 84%, 39%), is exactly what the tool above shows for this input.
- R′ = 16 ÷ 255 = 0.063, G′ = 0.725, B′ = 0.506.
- Cmax = 0.725, Cmin = 0.063, Δ = 0.663.
- L = (0.725 + 0.063) ÷ 2 = 0.394.
- S = 0.663 ÷ (1 − |2 × 0.394 − 1|) = 0.841.
- Green is largest: H = 60° × ((0.506 − 0.063) ÷ 0.663 + 2) = 160.1°.
- Rounded:
hsl(160, 84%, 39%).
Tailwind CSS emerald scale in RGB and HSL
Tailwind's emerald from 50 to 950, written as RGB and converted.
| Colour | RGB | HSL |
|---|---|---|
| emerald-50 | rgb(236, 253, 245) | hsl(152, 81%, 96%) |
| emerald-100 | rgb(209, 250, 229) | hsl(149, 80%, 90%) |
| emerald-200 | rgb(167, 243, 208) | hsl(152, 76%, 80%) |
| emerald-300 | rgb(110, 231, 183) | hsl(156, 72%, 67%) |
| emerald-400 | rgb(52, 211, 153) | hsl(158, 64%, 52%) |
| emerald-500 | rgb(16, 185, 129) | hsl(160, 84%, 39%) |
| emerald-600 | rgb(5, 150, 105) | hsl(161, 94%, 30%) |
| emerald-700 | rgb(4, 120, 87) | hsl(163, 94%, 24%) |
| emerald-800 | rgb(6, 95, 70) | hsl(163, 88%, 20%) |
| emerald-900 | rgb(6, 78, 59) | hsl(164, 86%, 16%) |
| emerald-950 | rgb(2, 44, 34) | hsl(166, 91%, 9%) |
Source: Tailwind CSS v3.4 default palette (tailwindcss 3.4.17)
Precision test: every RGB value through the converter
Rounded HSL is a lossy summary of RGB: our exhaustive run over all 16,777,216 RGB colours produced 2,427,032 HSL values, and only 10.583% of colours survived the trip back. The worst case was 5 levels (#02E4E6 → #02DFE3); the average 1.2753. If you need an exact colour back, keep the RGB.
Method: every whole-number RGB value, through the converter's own code and back, compared on the 0–255 scale (how we test).
| Measure | Result |
|---|---|
| RGB 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 RGB to HSL
Readable colour code in CSS
hsl(160 84% 39%) tells a reader "green, vivid, medium" at a glance in a way rgb(16 185 129) doesn't, and adjusting it by hand is predictable.
Hover and disabled states
A common pattern is to keep H and S and change only L for hover (a few points darker) and disabled (much lighter). The emerald scale below shows how far L moves between neighbouring steps.
Data visualisation
Charts that need categories of equal visual weight often fix S and L and space hues evenly; converting the RGB of an existing chart colour gives you the S and L to keep.
Common mistakes
- Confusing Python's
colorsys.rgb_to_hls()output order (H, L, S) with CSS's H, S, L. - Expecting equal L to mean equal perceived brightness: green and blue at L = 50% look very different. Use OKLCH or a contrast checker for perceptual work.
- Mixing scales: CSS uses degrees and percentages, Office's HSL fields use 0–255, and the classic Windows colour dialog uses 0–240.
- Rounding S and L before computing variants: work from the unrounded values or the original RGB to avoid compounding drift.