Boneyard Tools

How an RGB triple becomes a hex code

The math that turns red, green and blue values from 0 to 255 into a six-digit hex color, digit by digit, with worked examples.

Two notations, one color

A screen color is built from three light channels: red, green and blue. RGB writes each channel as a base-10 number from 0 to 255, which is easy for people to read. Hex writes the exact same three numbers in base 16, joined into one string that starts with a hash. Neither notation is more precise than the other in the usual 8-bit range; hex is just a compact way to pack the three channels into six characters, which is why it is so common in CSS and design files.

Why 255 and why two hex digits

Each channel uses eight bits, and eight bits can represent 256 distinct values, from 0 up to 255. In hexadecimal, one digit covers 16 values, so it takes exactly two hex digits to span the full 0 to 255 range: 00 at the bottom and ff at the top. That is why a full color needs six hex digits total, two for red, two for green and two for blue, in that order after the hash.

Converting a channel by hand

To turn one channel into hex, divide the value by 16. The whole-number part is the first hex digit and the remainder is the second, using the letters a through f for ten through fifteen. Take 130 as an example: 130 divided by 16 is 8 with a remainder of 2, so 130 becomes 82. Do this for all three channels of rgb(59, 130, 246) and you get 3b, 82 and f6, which join into #3b82f6. Values 10 through 15 map to the letters a through f, so 246 splits into 15 and 6, written f6.

Edge cases and rounding

A channel of 0 always becomes 00 and a channel of 255 always becomes ff, so pure red rgb(255, 0, 0) is #ff0000. Because hex has no room for fractions, a decimal channel is rounded to the nearest whole number first, and any value is kept within 0 to 255. Casing does not matter when reading a hex code, but writing it in lowercase is the common convention, which is what this converter produces.

Frequently asked questions

What does the leading hash mean in a hex color?

The hash is just a marker that tells CSS and design tools the characters after it are a hexadecimal color. It is not part of the number itself, but you should include it wherever a color is expected.

Is #fff the same as #ffffff?

Yes. The three-digit shorthand expands each digit to a pair, so #fff means #ffffff, which is white. This tool always writes the full six-digit form for consistency.

Does hex support transparency?

Standard six-digit hex does not. An eight-digit form adds an alpha pair for opacity, but this converter focuses on the solid six-digit RGB color without an alpha channel.