How to Convert Hex to RGB

A hex colour is three numbers written in base 16, one for red, one for green and one for blue. Converting to RGB is a matter of reading each pair.

The structure

Six hexadecimal digits, two per channel: #RRGGBB. Each pair ranges from 00 to FF, which is 0 to 255 in decimal.

That gives 256 levels per channel and about 16.7 million possible colours, which is more than the eye can distinguish in isolation.

Convert between HEX, RGB and HSL.

Open the Color Converter →

A worked example

#2B5FD9 splits into 2B, 5F and D9. 2B is 2 x 16 + 11 = 43. 5F is 5 x 16 + 15 = 95. D9 is 13 x 16 + 9 = 217.

So the colour is rgb(43, 95, 217), a strong blue. Checking one channel by hand is usually enough to confirm the method.

The shorthand

A three-digit code such as #f0c is shorthand for #ff00cc, where each digit is doubled. It only works when both digits in each pair are the same.

This catches people out: #123 means #112233, not #102030. If a code has three digits, double each one before converting.

Alpha and four or eight digits

An eight-digit hex code adds an alpha channel: #RRGGBBAA, where AA is opacity from 00 transparent to FF opaque. A four-digit shorthand #RGBA works the same way as the three-digit form.

CSS also accepts rgb(43 95 217 / 0.5) for the same result. Both are supported in current browsers.

Converting back

Divide each decimal value by 16 to get the first digit and take the remainder for the second. 43 divided by 16 is 2 remainder 11, so 2B.

Going the other way by hand is slower, which is why most people use a converter or an eyedropper.

Why hex is used at all

It is compact and it maps cleanly onto bytes: two hex digits are exactly one byte, so a colour is three bytes written in six characters.

That correspondence is also why hex appears in so many other places, from memory addresses to hash values.

HSL for humans

Hex and RGB are hard to adjust by hand. HSL describes a colour as hue, saturation and lightness, which matches how people think about colour: make it lighter, make it less saturated.

Converting hex to HSL is a formula rather than a lookup, which is why a converter is worth using when you are building a palette.

Contrast and accessibility

Contrast between text and background is measured as a ratio, not a difference in hex values. The WCAG guidelines set 4.5:1 for normal text and 3:1 for large text as the usual minimum.

Two colours that look clearly different can still fail the ratio. Always check with a contrast tool rather than by eye.

Common mistakes

Doing it in code

In JavaScript, parseInt(pair, 16) converts a pair. In Python, int(pair, 16). Most colour libraries expose a parse and a format function, which is safer than hand-parsing.

Validate the input length first, because a five-digit code will otherwise produce a plausible but wrong colour.

A practical habit

When you pick a colour from a design, note both the hex value and its contrast against the text colour you will use. That two-second check prevents most readability problems before they reach a review.

Picking colours that work together

A palette built from one hue at different lightness levels is easier to keep consistent than one built from unrelated hex values. Convert to HSL, adjust lightness, convert back.

Check each text and background pair against the contrast ratio rather than by eye. Two colours can look distinct and still fail the minimum.

Reading a hex code by hand

Split the six characters into three pairs. Each pair is one channel: red, green, then blue. Convert each pair from base 16 to a number from 0 to 255.

For example, FF is 255 and 00 is 0, so #FF0000 is pure red. A pair like 80 is 128, roughly half intensity.

Three digit codes such as #0F8 are shorthand where each digit repeats, so #0F8 means #00FF88.

Frequently asked questions

What is #FFF in RGB?

rgb(255, 255, 255), which is white.

Is #123 the same as #102030?

No. It is #112233.

How do I get a lighter version of a hex colour?

Convert to HSL, raise the lightness, and convert back.

Sources

More free tools

Related guides