Skip to main content
Toolscolor-conversions8 min read

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.

Published

Cover banner: HEX vs RGB vs HSL: how the three colour codes differ, with the free Color Conversions

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.

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.

NotationExampleColoursAlpha
3-digit HEX#ABC = #AABBCC4,096No
6-digit HEX#1663D916,777,216No
8-digit HEX#1663D98016,777,216 × 256 alpha stepsYes
rgb()rgb(22, 99, 217)16,777,216Optional
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.

ColourHEXRGBHSL
AltFTool Azure#1663D9rgb(22, 99, 217)hsl(216, 82%, 47%)
AltFTool Sky#03B4F7rgb(3, 180, 247)hsl(196, 98%, 49%)
CSS orange#FFA500rgb(255, 165, 0)hsl(39, 100%, 50%)
rebeccapurple#663399rgb(102, 51, 153)hsl(270, 50%, 40%)
Mid grey#808080rgb(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

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%)HEXRelative luminance
Yellow (60°)#FFFF000.9278
Green (120°)#00FF000.7152
Red (0°)#FF00000.2126
Blue (240°)#0000FF0.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 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

TaskBest formatWhy
Storing brand coloursHEX or RGBExact, lossless, universal
Making tints and shadesHSL (or OKLCH)Change one number, keep the hue
Transparency8-digit HEX or rgb() with alphaAlpha travels with the colour
Even-looking palettesOKLCHLightness matches perception
PrintConvert with a print profileScreen RGB to CMYK is not one formula
Which notation to reach for.

To sample a colour from an image or screen, the colour picker returns HEX and RGB values you can paste straight into the converter.

Frequently asked questions

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.

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.

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.

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.

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

Links the author listed for this post.

  1. 1
  2. 2
  3. 3
  4. 4

Reader feedback

Was this guide helpful?

Your signal helps the editorial team improve stale or unclear guides faster.

Share this article

Written by

Every conversion, luminance value and round-trip count in this article was computed on 13 November 2026 with the conversion functions in AltFTool's colour converter (parseHex, toHex, rgbToHsl, hslToRgb, luminance), run over all 16,777,216 RGB colours.

Your inbox, your topics

What do you want more of?

Pick the topics you'd like in your inbox. We've ticked one that matches this guide — change them freely.

Topics you want more of

Up to 200 characters.

Privacy policy

Discussion

0 comments

Loading discussion