All guides

How to Convert RGB to a HEX Color Code

How RGB channels become a six-digit hex code, the quick mental math, and when to prefer hex.

4 min read

Open the tool RGB to HEX Converter

To convert an RGB color to hex, turn each channel from 0 to 255 into a two-digit hexadecimal number, then join them in red, green, blue order and put a # in front. So rgb(124, 58, 237) becomes #7c3aed.

The fastest path is the RGB to HEX converter: drag the sliders or type each value and copy the hex it builds.

How each channel becomes two hex digits

Hexadecimal counts using 0 to 9 and then a to f, so a single hex digit covers 0 to 15 and a pair covers 0 to 255. That pair range lines up exactly with one RGB channel.

To convert a channel by hand, divide it by 16. The whole part is the first hex digit and the remainder is the second.

Take green at 58. Dividing by 16 gives 3 with a remainder of 10. The 3 stays as is, and 10 is written as a, so 58 becomes 3a. Do the same for red and blue, then string the three pairs together.

A worked example

Start with rgb(124, 58, 237):

  • Red 124 divides into 7 remainder 12, which is 7c
  • Green 58 divides into 3 remainder 10, which is 3a
  • Blue 237 divides into 14 remainder 13, which is ed

Join them and you get #7c3aed.

When hex is the better format

Hex is compact, so it is easy to drop into CSS, share in a message or store in a design file. A single token like #7c3aed says everything, which keeps stylesheets tidy.

RGB earns its place when you need transparency through rgba() or when you are setting channels from code, since separate numbers are easier to adjust one at a time. The RGB to HEX converter also shows HSL, HSV and CMYK for the same color, and the HEX to RGB converter goes back the other way whenever you need it.

Frequently asked questions

What is rgb(0, 0, 0) in hex?
It is
What is rgb(255, 255, 255) in hex?
It is
Do hex codes use uppercase or lowercase?
Either works.