HEX vs RGB vs HSL: color codes explained, with conversions

By Hammad Rasool · November 14, 2026

Quick answer: HEX, RGB and HSL are three ways of writing the same sRGB color. #3366ff, rgb(51, 102, 255) and hsl(225, 100%, 60%) are the same blue. HEX and RGB both give the red, green and blue amounts (HEX in base 16, RGB in base 10), so converting between them is exact. HSL describes the color as hue, saturation and lightness, which is easier for people to adjust. Convert any value instantly with the color converter.

About this guide: every conversion below was worked out by hand and checked against the color converter's code, and the round-trip figures come from running that code over 140,608 colors. CSS syntax follows MDN and the W3C CSS Color specification (links at the end).

The same colors in all three formats

HEXRGBHSL
#3366ffrgb(51, 102, 255)hsl(225, 100%, 60%)
#ff8800rgb(255, 136, 0)hsl(32, 100%, 50%)
#4f46e5rgb(79, 70, 229)hsl(243, 75%, 59%)
#1e293brgb(30, 41, 59)hsl(217, 33%, 17%)

What each format means

RGB

rgb(r, g, b) gives the amount of red, green and blue light, each from 0 to 255. rgb(0, 0, 0) is black, rgb(255, 255, 255) is white, and equal values in between are greys. That gives 256 × 256 × 256 = 16,777,216 possible colors.

HEX

A hex code is the same three numbers written in base 16, two digits each: #RRGGBB. ff is 255 and 00 is 0. CSS also accepts a 3-digit short form when each pair repeats (#36f = #3366ff) and 8-digit codes whose last pair is the opacity (#3366ff80 is about 50% opaque).

HSL

hsl(h, s%, l%) describes a color the way people talk about it. Hue is the angle on a color wheel from 0 to 360 (0 red, 120 green, 240 blue). Saturation runs from 0% (grey) to 100% (full color). Lightness runs from 0% (black) through 50% (the pure color) to 100% (white).

How to convert HEX to RGB

Split the code into pairs and convert each pair from base 16: first digit × 16 + second digit.

#ff8800
ff = 15 × 16 + 15 = 255   (red)
88 =  8 × 16 +  8 = 136   (green)
00 =  0 × 16 +  0 =   0   (blue)
→ rgb(255, 136, 0)

Hex digits a to f stand for 10 to 15. To go the other way, divide each RGB value by 16: the whole part is the first digit and the remainder the second. 51 ÷ 16 = 3 remainder 3, so 51 is 33. Shortcut tools: HEX to RGB and RGB to HEX.

How to convert RGB to HSL, step by step

Example: rgb(51, 102, 255).

  1. Scale to 0–1: divide by 255. R = 0.2, G = 0.4, B = 1.0.
  2. Find max and min: max = 1.0 (blue), min = 0.2. Their difference d = 0.8.
  3. Lightness = (max + min) ÷ 2 = (1.0 + 0.2) ÷ 2 = 0.6 → 60%.
  4. Saturation = d ÷ (1 − |2L − 1|) = 0.8 ÷ (1 − 0.2) = 1.0 → 100%. (If max equals min, the color is grey: saturation and hue are 0.)
  5. Hue depends on which channel is the max. Blue is the max, so hue = 60 × ((R − G) ÷ d + 4) = 60 × (−0.25 + 4) = 225. If red is the max, use 60 × ((G − B) ÷ d), adding 360 if negative; if green, 60 × ((B − R) ÷ d + 2).

Result: hsl(225, 100%, 60%). The RGB to HSL converter does this in one step.

How to convert HSL back to RGB

Example: hsl(225, 100%, 60%), with S and L written as 1.0 and 0.6.

  1. Chroma C = (1 − |2L − 1|) × S = (1 − 0.2) × 1.0 = 0.8.
  2. X = C × (1 − |(H ÷ 60) mod 2 − 1|). H ÷ 60 = 3.75; mod 2 = 1.75; X = 0.8 × (1 − 0.75) = 0.2.
  3. m = L − C ÷ 2 = 0.6 − 0.4 = 0.2.
  4. Pick the sector: hue 180–240 uses (R, G, B) = (0, X, C) = (0, 0.2, 0.8). The other sectors are 0–60: (C, X, 0); 60–120: (X, C, 0); 120–180: (0, C, X); 240–300: (X, 0, C); 300–360: (C, 0, X).
  5. Add m and scale: (0.2, 0.4, 1.0) × 255 = rgb(51, 102, 255).

Why HSL doesn't always convert back exactly

HSL written with whole numbers has 360 × 101 × 101 = 3,672,360 possible values, fewer than the 16,777,216 RGB colors, so it cannot name every RGB color exactly. When we converted 140,608 colors (every fifth value of each RGB channel) to whole-number HSL and back, 125,180 (89%) came back slightly different, usually by 1 in one channel. Example: rgb(0, 0, 25) becomes hsl(240, 100%, 5%), which converts back to rgb(0, 0, 26).

The difference is invisible, but it matters when you need an exact match, such as a brand color or a value that is compared in a test. Keep the HEX or RGB value as the source of truth, or use decimals in HSL (CSS accepts hsl(240 100% 4.9%)).

Which format to use in CSS

  • HEX is the most compact and what design tools such as Figma show and copy by default. Good for fixed brand colors.
  • RGB is handy when you compute colors in code, or need opacity: rgb(51 102 255 / 50%).
  • HSL is best for making related colors by hand: keep the hue and change lightness for a hover state or a lighter tint, e.g. hsl(225 100% 60%) → hsl(225 100% 70%).

Modern CSS accepts the space-separated syntax shown above, with opacity after a slash; the older rgba() and hsla() are now aliases of rgb() and hsl(). One limitation: equal HSL lightness does not mean equal perceived brightness (yellow at 50% looks much lighter than blue at 50%). If that matters, CSS Color 4 adds oklch(), designed to be perceptually uniform.

Check contrast after you pick a color

A color can look fine and still be hard to read. WCAG asks for a contrast ratio of at least 4.5:1 for normal text. White text on #3366ff measures 4.68:1, which just passes; on #4f46e5 it is 6.29:1. The color converter shows the contrast against white and black for any color you enter.

FAQ

Is HEX the same as RGB?

Yes, they hold the same three numbers. HEX writes them in base 16 (#3366ff), RGB in base 10 (rgb(51, 102, 255)). Converting between them never loses anything.

What does the 4th pair in an 8-digit hex code mean?

It is the alpha (opacity) channel: 00 is fully transparent and ff fully opaque. 80 is 128 of 255, about 50%.

Why do two converters give a slightly different HSL for the same color?

Rounding. Some round hue, saturation and lightness to whole numbers, others keep decimals. A difference of 1 is normal and not visible.

What is the difference between HSL and HSV (HSB)?

Both use the same hue. In HSL, 100% lightness is always white; in HSV, 100% value is the brightest version of the color. #3366ff is hsl(225, 100%, 60%) but hsv(225, 80%, 100%). Photoshop's picker uses HSB; CSS uses HSL.

Sources