RGB to HEX Converter
Type an rgb() value to get its hex code, ready to paste into CSS, along with HSL, OKLCH and CMYK.
How to convert RGB to HEX with this tool
- Enter an RGB value such as
rgb(30, 144, 255).rgba()input is accepted too; the HEX card shows the six-digit color, and adding the alpha pair is covered below. - If a design file gives you three loose numbers, wrap them as
rgb(r, g, b)before pasting. - The HEX card updates immediately, in this case to
#1E90FF. - Press Copy on the HEX card and paste the code into your stylesheet.
How to convert RGB to HEX by hand
Each channel from 0 to 255 becomes a two-digit base-16 number. Divide the value by 16: the whole-number result is the first digit and the remainder is the second, with 10 to 15 written as A to F.
Worked example with rgb(30, 144, 255):
- Red 30: 30 ÷ 16 = 1 remainder 14, so
1E - Green 144: 144 ÷ 16 = 9 remainder 0, so
90 - Blue 255: 255 ÷ 16 = 15 remainder 15, so
FF
Join the pairs in red, green, blue order behind a hash and you get #1E90FF, the CSS color dodgerblue.
Zero-padding: always two digits per channel
Any channel value under 16 converts to a single hex digit, so it needs a leading zero: 9 becomes 09 and 0 becomes 00. Take rgb(0, 128, 128). The 0 pads to 00, and 128 is 8 × 16 + 0, or 80, giving #008080, the CSS color teal.
Skipping the padding is a classic bug in hand-written converters. rgb(9, 99, 200) should be #0963C8; without the zero it becomes the five-digit #963C8, which is not valid CSS, so the browser drops the declaration. In JavaScript, n.toString(16).padStart(2, '0') handles it.
Converting alpha to hex
For an rgba() color, multiply the alpha (0 to 1) by 255, round, and convert the result to two hex digits. At 0.5 that is 127.5, which rounds to 128, or 80. Append it after the blue pair: rgba(30, 144, 255, 0.5) becomes #1E90FF80.
A few alpha values are worth memorizing: 1 is FF, 0.8 is CC (204), 0.5 is 80, 0.2 is 33 (51) and 0 is 00. To see the same color as HSL, OKLCH or CMYK as well, use the full color converter.
Frequently asked questions
How do I convert RGB to HEX?
Convert each channel from decimal to a two-digit base-16 number, then join them in red, green, blue order after a # sign. For example, rgb(30, 144, 255) becomes 1E, 90 and FF, or #1E90FF.
Why does my hex code have only five digits?
One channel was below 16 and lost its leading zero. Pad every channel to two digits, so 9 becomes 09 and rgb(9, 99, 200) becomes #0963C8.
How do I convert RGBA alpha to hex?
Multiply the alpha by 255, round it, and convert the result to two hex digits. An alpha of 0.5 gives 128, which is 80 in hex, so the code ends in 80.
What is rgb(0, 0, 0) in hex?
rgb(0, 0, 0) is #000000, black, and rgb(255, 255, 255) is #FFFFFF, white. Both have 3-digit short forms: #000 and #FFF.
When can an RGB color be written as 3-digit hex?
Only when both digits of every channel match, such as 00, 33, 99, AA or FF. rgb(255, 102, 0) is #FF6600, so it can also be written #F60.
Is converting RGB to HEX lossless?
Yes for the color channels, because both formats store the same 0–255 integer per channel. Alpha can lose a little precision, since hex rounds it to one of 256 steps.