RGB to HEX Converter
Convert HEX colors to RGB or RGB colors back to HEX.
Written by Anil Gautam · Updated
How do you convert HEX to RGB by hand?
A six-digit HEX color is three pairs of hexadecimal digits for red, green and blue, each running from 00 (0) to ff (255). To convert a pair, multiply the first digit by 16 and add the second, with a to f standing for 10 to 15. For #e85d2a, e8 is 14 × 16 + 8 = 232, 5d is 5 × 16 + 13 = 93 and 2a is 2 × 16 + 10 = 42, which is why the tool answers rgb(232, 93, 42). Three-digit shorthand doubles each digit, so #f80 means #ff8800, or rgb(255, 136, 0).
Going the other way, each channel becomes a two-digit pair, so rgb(0, 128, 255) gives #0080ff. Values above 255 are capped, and rgb(300, 0, 0) returns #ff0000. The HEX result is in lower case, which CSS treats the same as upper case.
Which color formats can you paste?
Input that starts with # is read as HEX. Anything else is scanned for whole numbers, and the first three become red, green and blue. That is why rgb(232, 93, 42), plain 232 93 42, rgba(232, 93, 42, 0.5) and the newer rgb(232 93 42 / 50%) all return #e85d2a. Any alpha value is dropped, since six-digit HEX has no transparency.
The same number scan explains some wrong answers. A HEX code pasted without its #, such as 1a2b3c, is read as the numbers 1, 2 and 3 and becomes #010203. Percentages are read as plain numbers and decimals are split at the point, so rgb(100%, 0%, 0%) gives #640000 instead of red, and hsl(16, 80%, 54%) is treated as if it were RGB. A minus sign is ignored, so -20 counts as 20.
Why is my 8-digit HEX code converted wrongly?
The HEX reader expects three or six digits. An eight-digit code with alpha, such as #e85d2a80, shifts the channels and comes out as rgb(93, 42, 128), and four-digit #rgba shorthand fails in a similar way. Remove the alpha digits first, so #e85d2a80 becomes #e85d2a. A typo inside a code is not always caught either: #e85g2a stops reading at the g and returns rgb(0, 14, 133). Only a code whose first character is not a hex digit, like #zzz, shows "Invalid HEX".
To find the HEX code of a color in a photo or screenshot, use the Image Color Picker (Eyedropper). The Base Converter handles hex numbers that are not colors.