A colour can be written in three common ways in CSS: HEX, RGB and HSL. They describe the same colour, so #ff8800, rgb(255, 136, 0) and hsl(32, 100%, 50%) are exactly the same orange.
The three formats
| Format | Example | How it works |
|---|---|---|
| HEX | #ff8800 | Red, green and blue as hexadecimal pairs |
| RGB | rgb(255, 136, 0) | Red, green, blue from 0 to 255 |
| HSL | hsl(32, 100%, 50%) | Hue (0 to 360), saturation and lightness |
Which one should you use?
All three work in CSS. HEX is the most common. HSL is the easiest to adjust by hand: keep the hue, then raise the lightness for a lighter shade or lower it for a darker one.
HEX to RGB in JavaScript
A HEX code is three bytes written in base 16. Short codes such as #09f expand to #0099ff.
function hexToRgb(hex) { hex = hex.replace('#', ''); if (hex.length === 3) hex = [...hex].map((c) => c + c).join(''); const n = parseInt(hex, 16); return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 }; } hexToRgb('#ff8800'); // { r: 255, g: 136, b: 0 } RGB to HSL in JavaScript
function rgbToHsl({ r, g, b }) { r /= 255; g /= 255; b /= 255; const max = Math.max(r, g, b); const min = Math.min(r, g, b); const l = (max + min) / 2; const d = max - min; let h = 0; let s = 0; if (d) { s = d / (1 - Math.abs(2 * l - 1)); if (max === r) h = ((g - b) / d) % 6; else if (max === g) h = (b - r) / d + 2; else h = (r - g) / d + 4; h = Math.round(h * 60); if (h < 0) h += 360; } return { h, s: Math.round(s * 100), l: Math.round(l * 100) }; } rgbToHsl(hexToRgb('#ff8800')); // { h: 32, s: 100, l: 50 } rgbToHsl(hexToRgb('#09f')); // { h: 204, s: 100, l: 50 } The only tricky part is the hue. It depends on which channel is largest, and the result wraps around the colour wheel, which is why negative values get 360 added.
Making a darker or lighter shade
Convert to HSL, change only the lightness (for example from 50% to 35%), and use it as hsl(32, 100%, 35%). The colour keeps its character, which is hard to do by editing RGB numbers.
Quick conversions without code
For one-off conversions I use a free browser tool: Color Converter. It runs in the browser and converts between HEX, RGB and HSL.
I also keep a plain-language version of this guide on my site: HEX, RGB and HSL colour codes explained.
Disclosure: I drafted this post with AI assistance and checked the code myself by running it in Node.

