# HEX vs RGB vs HSL: how the three colour codes differ

How HEX and RGB store the same bytes, how HSL rearranges them, why whole-number HSL changes 89% of colours on a round trip, and which format to use.

- Author: AltFTool Editorial
- Published: 2026-12-04
- Category: Tools
- Tags: colour codes, css, web design
- Canonical URL: <https://www.altftool.com/blogs/hex-vs-rgb-vs-hsl-colour-codes-explained>

---

HEX, RGB and HSL are three notations for the same sRGB colours. HEX writes red, green and blue as hexadecimal pairs, RGB as numbers from 0 to 255, and HSL rearranges them into hue, saturation and lightness. HEX and RGB convert exactly; HSL is easier to adjust by hand, but rounding it to whole numbers changes most colours slightly.

This guide shows how each format is built, converts one real brand colour through all three, and measures how much precision whole-number HSL loses across all 16.7 million colours. Every conversion was run with the conversion functions inside AltFTool's [colour converter](https://www.altftool.com/tools/all/color-conversions).

## HEX and RGB are the same numbers in different bases

A screen colour in CSS is three channels, red, green and blue, each stored as one byte from 0 to 255. RGB notation writes those bytes in decimal: `rgb(22, 99, 217)`. HEX notation writes the same bytes in base 16, two digits per channel: 22 is `16`, 99 is `63` and 217 is `D9`, giving `#1663D9`. The CSS Color Module Level 4 specification defines the 6-digit form exactly this way, with `00` as the minimum and `ff` (255) as the maximum of each channel.

Because both forms hold the same three bytes, converting between them is lossless in both directions. There are 256 × 256 × 256 = 16,777,216 possible colours either way. Which one you write is a matter of taste: HEX is compact and easy to copy from design tools, while RGB is easier to read.

| Notation | Example | Colours | Alpha |
| --- | --- | --- | --- |
| 3-digit HEX | `#ABC` = `#AABBCC` | 4,096 | No |
| 6-digit HEX | `#1663D9` | 16,777,216 | No |
| 8-digit HEX | `#1663D980` | 16,777,216 × 256 alpha steps | Yes |
| rgb() | `rgb(22, 99, 217)` | 16,777,216 | Optional |

*Colour counts follow from 16 or 256 values per channel; rgb() counted with whole-number channels.*

The short forms are just shorthand. `#ABC` doubles each digit to make `#AABBCC`, which is `rgb(170, 187, 204)`, so only 16 × 16 × 16 = 4,096 colours can be written that way. The 8-digit form adds an alpha byte: in `#1663D980` the final `80` is 128 out of 255, about 50% opacity, which the converter reports as alpha 0.5.

## How HSL rearranges the same colour

HSL keeps the sRGB colour but describes it the way people talk about colour. **Hue** is an angle on a colour wheel from 0° to 360°: 0° red, 120° green, 240° blue. **Saturation** is how vivid the colour is, from 0% (grey) to 100%. **Lightness** runs from 0% (black) through 50% (the pure hue, at full saturation) to 100% (white).

The conversion takes the largest and smallest of the three channels. Lightness is their average; saturation compares their difference with the available range; hue depends on which channel is largest and how the other two compare. For `#1663D9`, blue is largest (217) and red smallest (22), so the colour sits in the blue part of the wheel at 216°, with 82% saturation and 47% lightness.

| Colour | HEX | RGB | HSL |
| --- | --- | --- | --- |
| AltFTool Azure | `#1663D9` | `rgb(22, 99, 217)` | `hsl(216, 82%, 47%)` |
| AltFTool Sky | `#03B4F7` | `rgb(3, 180, 247)` | `hsl(196, 98%, 49%)` |
| CSS orange | `#FFA500` | `rgb(255, 165, 0)` | `hsl(39, 100%, 50%)` |
| rebeccapurple | `#663399` | `rgb(102, 51, 153)` | `hsl(270, 50%, 40%)` |
| Mid grey | `#808080` | `rgb(128, 128, 128)` | `hsl(0, 0%, 50%)` |

*Converted with the colour converter's parseHex and rgbToHsl functions on 13 November 2026.*

**Color Conversions**

Paste a HEX, RGB, HSL, HSV or CMYK value and get every other format at once.

[Open Color Conversions](https://www.altftool.com/tools/all/color-conversions)

## Whole-number HSL is a lossy round trip

HEX to RGB and back never changes a colour. HSL is different when it is rounded to whole degrees and whole percentages, which is how many tools display it. Converting `#1663D9` to `hsl(216, 82%, 47%)` and back gives `#1664DA`: green and blue have each moved by one step. `#FFA500` comes back as `#FFA600`, and `#1E90FF` as `#1F8FFF`.

To see how common this is, we ran every one of the 16,777,216 RGB colours through the converter's own HSL functions and back:

- **89.42%** of colours came back different (15,001,987 of them).
- The largest change to any single channel was **5** steps out of 255, so the shift is small but sometimes visible in flat brand areas.
- All 16.7 million RGB colours collapse onto just **2,428,455** distinct whole-number HSL values, compared with 360 × 101 × 101 = 3,672,360 possible combinations (the count includes values the converter rounds to hue 360, the same angle as 0).

Pure primaries and secondaries, black and white survive, as do some other colours such as `#808080` and `#663399`, which is why the problem often goes unnoticed. Most greys do shift slightly: 155 of the 256 greys change. The practical rule: keep the HEX or RGB value as the source of truth for brand colours, and use HSL for exploring variations. CSS itself accepts decimal values for hue, saturation and lightness, so you can keep a decimal place or two when you need more precision.

## HSL lightness is not how bright a colour looks

HSL's biggest weakness is that its lightness is simply the average of the largest and smallest channel, not a measure of perceived brightness. The CSS Color 4 specification gives the example directly: blue, `hsl(240deg 100% 50%)`, and yellow, `hsl(60deg 100% 50%)`, both have an HSL lightness of 50%, yet the yellow clearly looks much lighter.

| Colour at hsl(h, 100%, 50%) | HEX | Relative luminance |
| --- | --- | --- |
| Yellow (60°) | `#FFFF00` | 0.9278 |
| Green (120°) | `#00FF00` | 0.7152 |
| Red (0°) | `#FF0000` | 0.2126 |
| Blue (240°) | `#0000FF` | 0.0722 |

*Relative luminance computed with the colour converter's luminance function.*

Same lightness, a nearly thirteen-fold difference in luminance between yellow and blue. That is why a palette built by fixing HSL lightness and rotating the hue produces uneven text contrast. When legibility matters, test the actual pair in a [colour contrast checker](https://www.altftool.com/tools/all/color-contrast-checker) rather than trusting HSL lightness. The specification points to OKLCH as the alternative: its own worked example gives sRGB blue an OKLCH lightness of 0.452 and yellow 0.968.

## Which format to use where

| Task | Best format | Why |
| --- | --- | --- |
| Storing brand colours | HEX or RGB | Exact, lossless, universal |
| Making tints and shades | HSL (or OKLCH) | Change one number, keep the hue |
| Transparency | 8-digit HEX or rgb() with alpha | Alpha travels with the colour |
| Even-looking palettes | OKLCH | Lightness matches perception |
| Print | Convert with a print profile | Screen RGB to CMYK is not one formula |

*Which notation to reach for.*

To sample a colour from an image or screen, the [colour picker](https://www.altftool.com/tools/all/color-picker) returns HEX and RGB values you can paste straight into the converter.

## Frequently asked questions

### Is HEX more accurate than RGB?

No. A 6-digit HEX code and an rgb() value with whole numbers hold exactly the same three bytes, so they describe identical colours and convert back and forth without loss.

### Why does my colour change slightly after converting to HSL and back?

Most tools round HSL to whole degrees and percentages. In our test of all 16,777,216 colours, 89.42% changed after a whole-number HSL round trip, by up to 5 steps in one channel.

### What does the fourth pair in an 8-digit HEX code mean?

It is the alpha (opacity) channel, from 00 (fully transparent) to FF (fully opaque). For example, 80 is 128 out of 255, roughly 50% opacity.

### Is HSL lightness the same as brightness?

No. HSL lightness is the average of the largest and smallest channel. Yellow and blue both have 50% lightness at full saturation, but their relative luminance is 0.9278 and 0.0722.

### Can I write HSL with decimals in CSS?

Yes. CSS Color 4 accepts numbers rather than only whole values for hue, saturation and lightness, so decimal HSL avoids most of the rounding loss.

## Sources and notes

1. [CSS Color Module Level 4](https://www.w3.org/TR/css-color-4/) — W3C
2. [hex-color CSS data type](https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color) — MDN Web Docs
3. [hsl() CSS function](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl) — MDN Web Docs
4. [rgb() CSS function](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb) — MDN Web Docs
