SpunkArt
Color, art and images

How to convert colors between HEX, RGB and HSL

HEX, RGB and HSL are three ways to write the same screen color. HEX and RGB are the same numbers in different bases. HSL rearranges them into hue, saturation and lightness, which are easier for people to adjust.

Color format converter

HEX to RGB: read two digits at a time

A six-digit HEX code is three base-16 numbers in a row: red, green, blue. Each pair runs from 00 (0) to ff (255). To convert a pair, multiply the first digit by 16 and add the second. The letters a to f stand for 10 to 15.

value = first digit × 16 + second digit

Example: #2e86c1

  • 2e: 2 × 16 + 14 = 46
  • 86: 8 × 16 + 6 = 134
  • c1: 12 × 16 + 1 = 193

So #2e86c1 is rgb(46, 134, 193).

Going back is the reverse: divide by 16. The whole number is the first digit and the remainder is the second. 193 ÷ 16 = 12 remainder 1, which gives c1. A three-digit code such as #3a9 is shorthand: double each digit to get #33aa99.

RGB to HSL

Divide each channel by 255 so it lies between 0 and 1. Then find the largest value (max), the smallest (min) and the difference d = max − min.

L = (max + min) ÷ 2
S = d ÷ (1 − |2L − 1|), or 0 when d = 0
H depends on which channel is the max:
red: 60 × ((G − B) ÷ d mod 6)
green: 60 × ((B − R) ÷ d + 2)
blue: 60 × ((R − G) ÷ d + 4)

Example: rgb(46, 134, 193). Values rounded to four decimals.

  • R = 0.1804, G = 0.5255, B = 0.7569
  • max = 0.7569 (blue), min = 0.1804, d = 0.5765
  • L = (0.7569 + 0.1804) ÷ 2 = 0.4686, so lightness is about 47%
  • S = 0.5765 ÷ (1 − |0.9373 − 1|) = 0.5765 ÷ 0.9373 = 0.6151, about 62%
  • H = 60 × ((0.1804 − 0.5255) ÷ 0.5765 + 4) = 60 × 3.4014 = 204.1°

Result: hsl(204, 62%, 47%) after rounding to whole numbers.

HSL to RGB

Use three helper values: the chroma C, a second component X, and an offset m that lifts everything to the right lightness.

C = (1 − |2L − 1|) × S
X = C × (1 − |(H ÷ 60) mod 2 − 1|)
m = L − C ÷ 2

The hue picks which channel gets C, which gets X and which gets 0. From 0° to 60° it is (C, X, 0); from 180° to 240° it is (0, X, C), and so on round the wheel. Add m to each, then multiply by 255.

Example: hsl(200, 50%, 40%).

  • C = (1 − |0.8 − 1|) × 0.5 = 0.4
  • H ÷ 60 = 3.333, mod 2 = 1.333, so X = 0.4 × (1 − 0.333) = 0.2667
  • m = 0.4 − 0.2 = 0.2
  • 200° is in the 180–240 band: (0, 0.2667, 0.4) + 0.2 = (0.2, 0.4667, 0.6)
  • × 255 = (51, 119, 153), which is #337799

Hex digit lookup

Hex89abcdef
Decimal89101112131415

Common mistakes

  • Round-trip drift. HSL rounded to whole numbers cannot hit every RGB value. Converting #2e86c1 to hsl(204, 62%, 47%) and back gives a color one or two steps away. Keep the HEX code as the source of truth.
  • Confusing HSL with HSB. HSB (also called HSV), used in many paint apps, has a different brightness axis. The same numbers give a different color.
  • Reading lightness as perceived brightness. Yellow and blue at 50% lightness look very different in brightness. Use the contrast formula for that.

Questions people ask

Which format should I use in CSS?

All three work in every current browser. HEX is compact and easy to copy. HSL is easier when you want a lighter or darker version of the same hue, because you change one number.

What about an alpha value?

An eight-digit HEX code adds a fourth pair for opacity, and rgb() and hsl() accept a fourth value after a slash. The converter here handles opaque colors only.