Skip to content
Built in Lahore, PakistanHow we check the math
mathbehind

HEX, RGB and HSL: Three Ways to Write the Same Color

A HEX code is three numbers in base 16. RGB writes the same numbers in decimal. HSL describes the color by hue, saturation and lightness. Here is how they connect.

By Muhammad Ahmad, Software EngineerLinkedIn· figures checked

Published , updated . 4 min read.

Want to run your own numbers?Color Code ConverterOpen the calculator

CSS accepts colors in several formats, and design tools switch between them freely. They look very different, but HEX and RGB are exactly the same information, and HSL is a different way of describing the same color.

HEX is three numbers in base 16

A six-digit HEX code is three two-digit hexadecimal numbers in a row: one each for red, green and blue. Each pair runs from 00 (none of that color) to FF (the maximum, 255 in decimal).

Take #07804F, the green used on this site:

#07804F, one pair at a time
  1. Red

    07 in base 16equals7

  2. Green

    80 in base 16 = 8 × 16equals128

  3. Blue

    4F in base 16 = 4 × 16 + 15equals79

  4. As RGB

    #07804Fequalsrgb(7, 128, 79)

In hexadecimal, the letters A to F stand for 10 to 15, which is why F in 4F counts as 15.

Shorthand HEX

A three-digit code like #2B6 is shorthand. Each digit is doubled, so #2B6 becomes #22BB66. Shorthand can only reach 16 levels per channel instead of 256, so it only works for some colors.

HSL describes the color differently

HSL stands for hue, saturation and lightness. Hue is a position on the color wheel from 0 to 360 degrees: red at 0, green around 120, blue around 240. Saturation is how vivid the color is, and lightness runs from black at 0% to white at 100%.

The same green, #07804F, is hsl(156, 90%, 26%): a blue-leaning green, very saturated, and fairly dark. The yellow used to highlight answers on this site, #FFD43B, is hsl(47, 100%, 62%).

Which format to use

  • HEX: compact and universal. Good for storing and sharing exact colors.
  • RGB: useful when code needs to work with the channel values, or when you add transparency with rgb(7 128 79 / 50%).
  • HSL: easiest to adjust by hand. To make a lighter or darker version, change only the lightness value.

All three produce the same color on screen, so the choice is about what is easiest to read and edit in your project.

A full conversion, step by step

Take #3366CC, a medium blue. Split it into pairs: 33, 66 and CC. In base 16, 33 is 3 × 16 + 3 = 51, 66 is 102, and CC is 12 × 16 + 12 = 204. So #3366CC is rgb(51, 102, 204). In HSL the same color is about hsl(220, 60%, 50%): a hue of 220 degrees (blue), 60% saturation and 50% lightness.

The HSL form shows why designers like it. To make a lighter tint for a hover state, keep hue and saturation and raise lightness: hsl(220, 60%, 65%). To make a muted version for a disabled button, lower saturation: hsl(220, 20%, 50%). Doing the same in HEX means recalculating all three pairs.

Transparency

All three formats can carry an alpha channel for transparency. HEX adds a fourth pair, #RRGGBBAA, where 80 is about 50% opacity, so #3366CC80 is a half-transparent blue. In modern CSS, rgb() and hsl() take an alpha after a slash: rgb(51 102 204 / 50%) or hsl(220 60% 50% / 0.5). The older rgba() and hsla() names still work.

Contrast: the check that matters most

Whatever format you use, text has to be readable against its background. The Web Content Accessibility Guidelines ask for a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text at level AA. A light grey on white that looks elegant on a designer's screen can fail badly on a phone in sunlight. Check each text and background pair with a contrast checker, and remember that HSL lightness is not the same as perceived brightness: a yellow and a blue with the same lightness value can look very different.

Newer color formats

Modern browsers also support color spaces such as oklch(), which are designed so that equal steps in lightness look equally bright to the eye. That makes it easier to build color scales and themes that stay consistent. HEX, RGB and HSL are still the formats you will meet most often, and every tool understands them.

Tip: Building a palette? Keep the hue fixed and step the lightness in HSL to get a family of shades that clearly belong together.

Questions people ask

How do I make a colour lighter in CSS?
Convert it to HSL and raise the lightness value while keeping hue and saturation, for example from 50% to 65%.
What is #3366CC in HSL?
About hsl(220, 60%, 50%): a hue of 220 degrees, 60% saturation and 50% lightness.
What does #36C mean?
It is shorthand for #3366CC: each digit is doubled.
Is oklch better than HSL?
For building colour scales, often yes: equal steps in oklch lightness look equally bright, which HSL doesn't guarantee. HEX, RGB and HSL are still the most widely used.
How do I convert a HEX color to RGB?
Split the six digits into three pairs and convert each from base 16 to decimal. #07804F becomes rgb(7, 128, 79).
What does HSL stand for?
Hue, saturation and lightness. Hue is a position on the color wheel from 0 to 360 degrees; saturation and lightness are percentages.
When should I use HSL instead of HEX?
When you want to adjust colors by hand. Changing only the lightness gives lighter or darker shades of the same color.
How do I add transparency to a HEX color?
Add a fourth pair for alpha: #RRGGBBAA. #3366CC80 is #3366CC at about 50% opacity.
What contrast ratio does text need?
At least 4.5:1 for normal text and 3:1 for large text to meet WCAG level AA.