How to Convert RGB to HEX
A HEX colour code is the same three numbers as an RGB colour, written in base 16. Once you see the pattern, you can convert in your head.
Updated
Try it now: RGB to HEX Converter. It runs in your browser, with no signup.
The conversion in three steps
Each RGB channel is a whole number from 0 to 255. Each one becomes exactly two hexadecimal digits, and the three pairs are joined after a #.
- Divide the channel by 16. The whole part is the first hex digit.
- The remainder is the second hex digit.
- Digits 10 to 15 are written A to F. Repeat for red, green and blue.
rgb(59, 130, 246) 59 = 3 * 16 + 11 -> 3B 130 = 8 * 16 + 2 -> 82 246 = 15 * 16 + 6 -> F6 #3B82F6
Alpha (transparency)
An 8-digit HEX code adds a fourth pair for alpha. Multiply the alpha value (0 to 1) by 255, round, and convert like any other channel. rgba(59, 130, 246, 0.5) gives 0.5 * 255 = 127.5, which rounds to 128, or 80 in hex, so the code is #3B82F680. Modern CSS also accepts the space-separated form rgb(59 130 246 / 50%).
3-digit shorthand
A 3-digit code such as #FA0 doubles every digit, so it means #FFAA00. A colour can only be shortened when each pair has two identical digits. #3B82F6 has no shorthand.
Code for the common languages
// JavaScript
const toHex = (r, g, b) =>
"#" + [r, g, b].map(n => n.toString(16).padStart(2, "0")).join("").toUpperCase();
toHex(59, 130, 246); // "#3B82F6"
# Python
r, g, b = 59, 130, 246
print("#{:02X}{:02X}{:02X}".format(r, g, b)) # #3B82F6
// C#
int r = 59, g = 130, b = 246;
string hex = quot;#{r:X2}{g:X2}{b:X2}"; // #3B82F6Common mistakes
The RGB to HEX Converter accepts rgb(), rgba() and plain r, g, b input, and shows an error for values outside 0 to 255.
- Values above 255 or below 0 do not map to two hex digits. Check the range first.
- Forgetting the leading zero: 5 must be written 05, not 5.
- Mixing percentages and numbers: rgb(100%, 50%, 0%) is not rgb(100, 50, 0).
- Pasting a string such as rgb(59, 130, 246) into code that expects three separate numbers.
Frequently asked questions
Is HEX case sensitive?
No. #3b82f6 and #3B82F6 are the same colour. Pick one style and keep it consistent.
How do I convert HEX back to RGB?
Split the code into three pairs and convert each pair from base 16 to decimal: 3B is 59, 82 is 130, F6 is 246.
What does an 8-digit HEX code mean?
The last two digits are the alpha channel, where 00 is fully transparent and FF is fully opaque.

