How to Convert RGB to HEX (and HEX to RGB) by Hand
Convert RGB to HEX by turning each 0 to 255 channel into two base 16 digits. Worked examples, HEX to RGB, shorthand codes and 8-digit alpha hex explained.
Updated
To convert RGB to HEX, turn each of the three channel values (0 to 255) into a two-digit base 16 number and join them after a #. For example, rgb(255, 99, 71) becomes #ff6347. To convert HEX to RGB, split the code into three pairs and turn each pair back into a decimal number.
This guide covers the arithmetic only. For what the notations mean and how HSL fits in, read HEX, RGB and HSL color codes explained.
The RGB to HEX formula
Hexadecimal has sixteen digits: 0 to 9, then a to f for the values 10 to 15. One channel of an RGB color always fits in exactly two of them.
For each channel, divide the value by 16. The whole-number quotient is the first hex digit and the remainder is the second.
Worked example with rgb(255, 99, 71):
- Red: 255 divided by 16 is 15 remainder 15. Both digits are 15, which is
f, so red isff. - Green: 99 divided by 16 is 6 remainder 3, so green is
63. - Blue: 71 divided by 16 is 4 remainder 7, so blue is
47. - Join the pairs in red, green, blue order:
#ff6347.
The one mistake people make is dropping a leading zero. A channel value below 16 still needs two digits, so 10 is 0a, not a. That makes rgb(0, 10, 5) equal to #000a05. Without the padding you would get a four-digit code that means something else entirely.
The HEX to RGB formula
Going back is multiplication instead of division. Split the code into three pairs. For each pair, multiply the first digit by 16 and add the second.
Worked example with #1e90ff:
1e: 1 times 16 is 16, pluse(14) gives 30.90: 9 times 16 is 144, plus 0 gives 144.ff: 15 times 16 is 240, plus 15 gives 255.
Result: rgb(30, 144, 255). Case does not matter, so #1E90FF converts the same way.
Quick reference: channel values
These values show up constantly, and knowing them lets you read many codes without any arithmetic.
| Decimal | Hex | Share of full intensity |
|---|---|---|
| 0 | 00 | 0% |
| 51 | 33 | 20% |
| 64 | 40 | 25% |
| 102 | 66 | 40% |
| 128 | 80 | 50% |
| 153 | 99 | 60% |
| 192 | c0 | 75% |
| 204 | cc | 80% |
| 255 | ff | 100% |
And a few complete colors:
| Color | RGB | HEX |
|---|---|---|
| Black | rgb(0, 0, 0) | #000000 |
| White | rgb(255, 255, 255) | #ffffff |
| Red | rgb(255, 0, 0) | #ff0000 |
| Lime | rgb(0, 255, 0) | #00ff00 |
| Blue | rgb(0, 0, 255) | #0000ff |
| Yellow | rgb(255, 255, 0) | #ffff00 |
| Gray | rgb(128, 128, 128) | #808080 |
| Orange | rgb(255, 165, 0) | #ffa500 |
The names in that table are CSS named colors. To look up the nearest name for any other code, use the Color Name Finder.
Shorthand hex: three digits instead of six
When both digits of every pair are the same, CSS lets you write one digit per channel. The browser doubles each digit when it reads the code:
#f60expands to#ff6600.#fffexpands to#ffffff.#09cexpands to#0099cc.
Doubling is the rule, not padding with zeros. #f60 is not #f06000. A code such as #3b82f6 has no shorthand because its pairs do not repeat. To convert shorthand to RGB, expand it first, then use the normal formula: #09c is #0099cc, which is rgb(0, 153, 204).
Alpha: 8-digit and 4-digit hex
CSS accepts a fourth pair for opacity, written #RRGGBBAA. The alpha pair runs from 00 (fully transparent) to ff (fully opaque). To get it from a percentage, multiply the opacity by 255, round, and convert to hex. For 50 percent, 0.5 times 255 is 127.5, which rounds to 128, which is 80.
| Opacity | Alpha hex |
|---|---|
| 100% | ff |
| 90% | e6 |
| 80% | cc |
| 75% | bf |
| 60% | 99 |
| 50% | 80 |
| 40% | 66 |
| 25% | 40 |
| 20% | 33 |
| 10% | 1a |
| 0% | 00 |
So tomato red at 50 percent opacity is #ff634780, the same color as rgb(255 99 71 / 50%). The shorthand works here too: a four-digit code such as #f608 expands to #ff660088.
One trap: CSS puts alpha last, but Android color resources use #AARRGGBB, with alpha first. If a color copied from an Android project looks wrong on the web, move the first pair to the end.
Converting in code
Most languages can do this in one line.
- JavaScript, RGB to HEX:
"#" + [r, g, b].map(v => v.toString(16).padStart(2, "0")).join("") - JavaScript, HEX to RGB:
parseInt("ff", 16)returns255. Run it on each pair. - Python, RGB to HEX:
"#{:02x}{:02x}{:02x}".format(r, g, b) - Python, HEX to RGB:
int("ff", 16) - Excel and Google Sheets:
=DEC2HEX(255, 2)and=HEX2DEC("FF")
The padStart(2, "0") and 02x parts are the leading-zero rule from earlier. Leave them out and dark colors break.
Converting with a tool
For HEX to RGB, paste a six-digit code into the Color Converter. It returns the rgb() and hsl() forms, the bare channel values, a CSS variable line, and the eight-digit code at 50 percent opacity, each with a copy button. The field expects six digits, so expand a shorthand code such as #f60 before pasting it.
For RGB to HEX, the converter's text field takes a HEX code rather than three channel numbers. There are two straightforward routes. Do the division above, which takes a few seconds per channel, or convert each channel in the Base Converter by entering the decimal value and reading the hexadecimal result. Pad any single digit with a zero, join the three pairs, then paste the finished code into the Color Converter to confirm the swatch matches the color you expected.
More color utilities, including contrast checking and palette generation, are in the colors category.
Try it
Paste #ff6347 into the Color Converter and check the result against the worked example above: you should see rgb(255, 99, 71). Then click the swatch to pick any other color and read its HEX and RGB values side by side.