How to Convert HEX to RGB (and RGB to HEX) by Hand

Short answer

Split the 6-digit HEX code into three pairs (red, green, blue) and convert each pair from base 16 to base 10. For #1E90FF: 1E = 30, 90 = 144, FF = 255, so the colour is rgb(30, 144, 255). To go back, convert each RGB number (0–255) to a two-digit hexadecimal value and join them.

Designers often work with HEX codes such as #1E90FF, while some software, spreadsheets and code expect RGB values like rgb(30, 144, 255). Both describe exactly the same colour: the amount of red, green and blue light on a scale from 0 to 255. Converting between them only needs a little base-16 arithmetic, and once you understand it, colour codes stop looking like random strings.

How HEX colour codes work

A HEX code is three numbers written in hexadecimal (base 16), which uses the digits 0–9 and the letters A–F for the values 10 to 15:

#RRGGBB: two digits for red, two for green and two for blue.

Each pair goes from 00 (none of that colour) to FF (the maximum, 255). So #000000 is black, #FFFFFF is white, #FF0000 is pure red and #808080 is a mid grey. Letters can be uppercase or lowercase.

HEX to RGB: step by step

Convert #1E90FF:

  1. Split into pairs: 1E, 90, FF.
  2. Convert each pair: multiply the first digit by 16 and add the second digit.
    • 1E: 1 Γ— 16 + 14 (E) = 30
    • 90: 9 Γ— 16 + 0 = 144
    • FF: 15 Γ— 16 + 15 = 255
  3. Write the result: rgb(30, 144, 255).

Letter values to remember: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.

RGB to HEX: step by step

Convert rgb(255, 99, 71):

  1. Divide each number by 16. The whole-number part is the first hex digit, the remainder is the second.
    • 255 Γ· 16 = 15 remainder 15 β†’ F, F β†’ FF
    • 99 Γ· 16 = 6 remainder 3 β†’ 63
    • 71 Γ· 16 = 4 remainder 7 β†’ 47
  2. Join them with a #: #FF6347.

Always use two digits per channel: 5 becomes 05, not 5. The same base-conversion idea is explained for base 2 in how to convert binary to decimal, and the number base converter converts any value between binary, octal, decimal, hexadecimal and even base 36. It is a quick way to check your working when you convert colour codes by hand for the first few times.

Shorthand 3-digit HEX codes

CSS allows a 3-digit shorthand when each pair has two identical digits. Each digit is simply doubled: #FA0 means #FFAA00, which is rgb(255, 170, 0). #FFF is white and #000 is black. Codes like #1E90FF cannot be shortened because the digits in each pair differ.

8-digit HEX codes with transparency

Modern CSS also accepts #RRGGBBAA, where the last pair is the alpha (opacity) channel from 00 (fully transparent) to FF (fully opaque). #1E90FF80 is our blue at an alpha of 128 Γ· 255 β‰ˆ 0.5, which you could also write as rgb(30 144 255 / 50%) or rgba(30, 144, 255, 0.5). A 4-digit shorthand such as #FA08 works the same way as the 3-digit form.

Quick reference table

ColourHEXRGB
Black#0000000, 0, 0
White#FFFFFF255, 255, 255
Red#FF0000255, 0, 0
Lime (pure green)#00FF000, 255, 0
Blue#0000FF0, 0, 255
Grey#808080128, 128, 128
Dodger blue#1E90FF30, 144, 255
Tomato#FF6347255, 99, 71

Convert instantly

The colour picker and converter converts between HEX, RGB and HSL as you type or pick a colour, and gives CSS values you can copy. HSL (hue, saturation, lightness) is often easier for creating lighter or darker shades of the same colour. To build a set of matching colours, try the colour palette generator.

Check contrast before you use a colour

A colour that looks good may still be hard to read as text. The Web Content Accessibility Guidelines (WCAG) require a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text at level AA. Our dodger blue #1E90FF has a contrast ratio of only about 3.2:1 against white, so white body text on it fails AA, while black text on it reaches about 6.5:1. A darker blue such as #0066CC reaches about 5.6:1 with white text. Test your pairs with the colour contrast checker, which also suggests a passing colour.

Converting colours in code

Common mistakes

Colours also matter when you save images; see PNG vs JPG vs WebP for which format keeps flat colours crisp.

Frequently asked questions

How do I convert HEX to RGB?

Split the code into three pairs and convert each from base 16 to base 10 by multiplying the first digit by 16 and adding the second. #1E90FF becomes rgb(30, 144, 255).

How do I convert RGB to HEX?

Convert each value from 0 to 255 into a two-digit hexadecimal number and join them with a # in front. rgb(255, 99, 71) becomes #FF6347.

What does a 3-digit HEX code mean?

It is shorthand where each digit is doubled. #FA0 is the same as #FFAA00.

What are the last two digits in an 8-digit HEX code?

They set the opacity (alpha), from 00 for fully transparent to FF for fully opaque.

Are HEX colour codes case-sensitive?

No. #1e90ff and #1E90FF are the same colour.

Sources