How do you convert RGB to HEX?
Convert each of the three values (0 to 255) to a two-digit base-16 number and write them one after another behind a #. Divide the value by 16: the whole number is the first digit and the remainder is the second, with 10 to 15 written as A to F. Values below 16 get a leading zero.
Example: rgb(46, 139, 87)
| Channel | Value | Calculation | Hex |
|---|---|---|---|
| Red | 46 | 2 × 16 + 14 | 2E |
| Green | 139 | 8 × 16 + 11 | 8B |
| Blue | 87 | 5 × 16 + 7 | 57 |
So rgb(46, 139, 87) = #2E8B57, which happens to be the CSS color seagreen.
What is the RGB to HEX formula?
first digit = floor(value ÷ 16)
second digit = value mod 16
HEX = # + RR + GG + BB
How do you convert RGBA to HEX?
Multiply the alpha (0 to 1) by 255, round it, and add it as a fourth pair. rgba(46, 139, 87, 0.5) becomes #2E8B5780 because 0.5 × 255 = 127.5, rounded to 128 = 80 in hex. Paste an rgba() value above to see it.
How do I convert RGB to HEX in JavaScript or Python?
// JavaScript
const toHex = (r, g, b) => "#" + [r, g, b].map(v => v.toString(16).padStart(2, "0")).join("").toUpperCase();
toHex(46, 139, 87); // "#2E8B57"
# Python
"#{:02X}{:02X}{:02X}".format(46, 139, 87) # '#2E8B57'
Is HEX the same as RGB?
Yes, for web colors they describe the same sRGB values in different notation. #2E8B57 and rgb(46, 139, 87) render identically; hex is shorter, rgb() is easier to read and edit. Both are defined in W3C, CSS Color Module Level 4.
Common RGB values in HEX
| HEX | RGB | CMYK | Name | |
|---|---|---|---|---|
#FFFFFF | 255, 255, 255 | 0, 0, 0, 0 | white | |
#000000 | 0, 0, 0 | 0, 0, 0, 100 | black | |
#FF0000 | 255, 0, 0 | 0, 100, 100, 0 | red | |
#00FF00 | 0, 255, 0 | 100, 0, 100, 0 | lime | |
#0000FF | 0, 0, 255 | 100, 100, 0, 0 | blue | |
#FFFF00 | 255, 255, 0 | 0, 0, 100, 0 | yellow | |
#00FFFF | 0, 255, 255 | 100, 0, 0, 0 | aqua | |
#FF00FF | 255, 0, 255 | 0, 100, 0, 0 | fuchsia | |
#808080 | 128, 128, 128 | 0, 0, 0, 50 | gray | |
#FFA500 | 255, 165, 0 | 0, 35, 100, 0 | orange | |
#800080 | 128, 0, 128 | 0, 100, 0, 50 | purple | |
#008080 | 0, 128, 128 | 100, 0, 0, 50 | teal | |
#1E90FF | 30, 144, 255 | 88, 44, 0, 0 | dodgerblue | |
#2E8B57 | 46, 139, 87 | 67, 0, 37, 45 | seagreen | |
#FF5733 | 255, 87, 51 | 0, 66, 80, 0 |
Color Code Converter · HEX to RGB Converter · CMYK to RGB Converter · RGB to CMYK Converter
Formulas checked October 2026 against W3C, CSS Color Module Level 4.