๐จ HEX, RGB, and HSL: Three Ways to See Color
Understanding color formats every frontend developer should know
Three Formats at a Glance
| Format | Example | Meaning | Best For |
|---|---|---|---|
| HEX | #3b82f6 | RGB in hexadecimal | CSS, design handoff |
| RGB | rgb(59,130,246) | Red, Green, Blue (0-255 each) | JavaScript, Canvas |
| HSL | hsl(217,91%,60%) | Hue (0-360ยฐ) + Saturation + Lightness | Theme systems, accessibility |
Key insight: HEX is just RGB in base-16. HSL is a human-friendly perspective on the same color โ easier to adjust and reason about.
When to Use Each Format
- ๐จ CSS from design files โ HEX (designers usually give HEX values)
- โ๏ธ JS dynamic colors โ RGB (
Math.random()*255per channel) - ๐ Theme systems โ HSL (fix H, vary S and L for light/dark variants)
- ๐๏ธ Canvas pixel ops โ RGB (ImageData is RGBA array)
HSL's Superpower: Effortless Color Variations
Want a darker hover state? Drop L from 60% to 50%. Lighter variant? Raise L to 80%. With RGB you'd have to guess three numbers.
/* HSL theme palette in one line */
--primary: hsl(217, 91%, 60%); /* main */
--primary-light: hsl(217, 91%, 80%); /* lighter */
--primary-dark: hsl(217, 91%, 40%); /* darker */
Modern CSS Color Formats
- HWB:
hwb(217 23% 4%)โ hue + white + black, even more intuitive - LCH / OKLCH: Perceptually uniform color spaces, CSS Color Level 4
- 8-digit HEX:
#3b82f680โ last two digits are alpha (80 = 50% opacity)
๐ Convert colors now
HEX โ RGB โ HSL real-time, color picker, one-click copy
๐จ Open Color ConverterFAQ
Q: Are #3b82f6 and rgb(59,130,246) the same?
Yes. HEX is base-16: 3b=59, 82=130, f6=246. Same color, different notation.
Q: When should I use HSL over HEX?
When you need to programmatically generate color variants (themes, gradients, hover states). HSL makes "lighter/darker/saturated" trivial.
๐ Related: Regex Tester โ ยท JSON Formatter โ