How HSL to RGB conversion works
HSL to RGB rebuilds the three light channels from the colour's position on the hue wheel and its saturation and lightness. Lightness sets the midpoint between the brightest and dimmest channel, saturation sets how far apart they are, and the hue places the third channel in between.
- Scale S and L to 0–1.
- C = (1 − |2L − 1|) × S; X = C × (1 − |(H ÷ 60) mod 2 − 1|); m = L − C ÷ 2.
- Choose (R′, G′, B′) by the 60° sector of H.
- R, G, B = round((value + m) × 255).
Worked example: hsl(340, 82%, 52%) to RGB
Here is the converter's own calculation for hsl(340, 82%, 52%), step by step. The result, rgb(233, 32, 99), is exactly what the tool above shows for this input.
- Chroma C = (1 − |2 × 0.52 − 1|) × 0.82 = 0.787.
- X = 0.787 × (1 − |(340 ÷ 60) mod 2 − 1|) = 0.262.
- m = 0.52 − 0.787 ÷ 2 = 0.126.
- 340° is in the 300–360° sector, so (R′, G′, B′) = (C, 0, X) = (0.787, 0, 0.262).
- Add m and multiply by 255: R = 233, G = 32, B = 99.
- Written as
rgb(233, 32, 99).
A lightness ramp in HSL and RGB
Hue 210° and 70% saturation held fixed; lightness from 10% to 90%.
| Colour | HSL | RGB |
|---|---|---|
| L 10% | hsl(210, 70%, 10%) | rgb(8, 26, 43) |
| L 20% | hsl(210, 70%, 20%) | rgb(15, 51, 87) |
| L 30% | hsl(210, 70%, 30%) | rgb(23, 77, 130) |
| L 40% | hsl(210, 70%, 40%) | rgb(31, 102, 173) |
| L 50% | hsl(210, 70%, 50%) | rgb(38, 128, 217) |
| L 60% | hsl(210, 70%, 60%) | rgb(82, 153, 224) |
| L 70% | hsl(210, 70%, 70%) | rgb(125, 179, 232) |
| L 80% | hsl(210, 70%, 80%) | rgb(168, 204, 240) |
| L 90% | hsl(210, 70%, 90%) | rgb(212, 230, 247) |
Precision test: every HSL value through the converter
The converter turns each of the 3,672,360 whole-number HSL values into one RGB colour, and converting that colour back reproduces it 100% of the time. Between them those values reach 2,037,745 distinct RGB colours, about an eighth of the RGB space.
Method: every whole-number HSL value, through the converter's own code and back, compared on the 0–255 scale (how we test).
| Measure | Result |
|---|---|
| HSL values tested | 3,672,360 |
| Distinct RGB results | 2,037,745 |
| Came back exactly | 100% |
| Largest change (0–255 levels) | 0 |
| Average change | 0 |
Last updated · Source: Pixohub exhaustive test of lib/color-math.ts
Where you'll use HSL to RGB
Canvas, WebGL and native code
Canvas fillStyle accepts hsl() strings, but pixel buffers, WebGL uniforms and most native APIs need RGB numbers. Converting once avoids parsing CSS strings at runtime.
Checking a lightness scale
The table below holds hue 210° and saturation 70% fixed and steps lightness from 10% to 90%. Watching the RGB channels shows why mid-lightness colours are the most saturated-looking.
Generating chart colours
Plotting libraries such as Matplotlib take RGB tuples between 0 and 1: divide the 0–255 values here by 255.
Common mistakes
- Using Python's
colorsys.hls_to_rgb(h, l, s)with arguments in H, S, L order: it expects lightness second, all from 0 to 1. - Passing hue in degrees to libraries that expect 0–1 (colorsys, many shader functions).
- Assuming L = 50% means medium brightness in RGB: hsl(60, 100%, 50%) is bright yellow rgb(255, 255, 0).
- Rounding each step: keep full precision until the final ×255.