RGB to hex converter: turn three 0-255 numbers into a hex code

By TechCompare · Updated · How we check

rgb(34, 197, 94) becomes #22C55E. Each 0 to 255 number turns into a two-digit base 16 pair, and the three pairs join behind a #.

  • HEX#22C55E
  • HSLhsl(142, 71%, 45%)
  • CMYKcmyk(83%, 0%, 52%, 23%)

A hex code is the same three RGB numbers written in base 16, with two digits per channel. Type your red, green and blue values into the converter above and the hex code appears first, with HSL and CMYK beside it. This page's example, 34, 197 and 94, becomes 22, C5 and 5E, which join into #22C55E.

Conversion
RGB to HEX
Example: rgb(34, 197, 94)
Category
Web Formats
Color knowledge base

How this is calculated

Base 16 uses the digits 0 to 9, then A to F for 10 to 15, so a pair runs from 00 to FF. The first digit of a pair counts 16 times as much as the second. Take the gray #808080. Its green channel is 128, which is 8 × 16 + 0, so the pair is 80.

Every pair needs two digits, even when the first one is zero. Take rgb(5, 10, 15): its channels are 5, 10 and 15. All three sit below 16, so each needs its leading zero. The code is #050A0F. Drop just one zero and the code has five digits, which CSS doesn't accept. Drop all three and you get #5AF, which CSS reads as #55AAFF. That's a valid code for a different color, so no error warns you.

The three-digit form works only when each pair repeats one digit, like FF, 00 or 88, which is why #FFFFFF can be written #FFF. Case doesn't matter either. The RGB to hex step is exact, since each 0 to 255 number maps to one pair and back, but the HSL and CMYK rows round their numbers. Going through HSL returns #21C45D for this color, not #22C55E, so a round trip through HSL can land on a slightly different code.

Verdict

Keep the RGB numbers when you plan to do math on the color, since you can always write the hex from them. Use the hex code when a stylesheet or design tool asks for one, and copy it with the copy button so no digit gets retyped by hand.

More Color scenarios

Hex to RGB
A hex color code is three RGB numbers written in base 16.
View details
RGB vs CMYK
Screens add light and print adds ink. See one color in both, and why a plain CMYK formula can't promise a print match.
View details
HSL color model
Hue, saturation and lightness, with three live sliders and a table of common colors in hex, RGB and HSL.
View details

Frequently asked questions

Why is my hex code only five characters long?
A hex color has six digits after the #, or three when each pair repeats one digit. CSS also accepts four and eight digits, which add an alpha value. Five digits never works, and the converter reads only three or six, so five shows an error. The likely cause is a channel that lost its leading zero, like 5 instead of 05. CSS Color 4 lists the accepted lengths.
Can I use percentages in rgb()?
Not in this converter. It reads three whole numbers from 0 to 255, so percent signs and decimals show an error under the field. CSS does accept percentages in rgb(), so convert them first: divide the percentage by 100, multiply by 255 and round to a whole number.
How do I get a hex code from rgba()?
Use the three color numbers for a six-digit code. If you need the alpha too, add its pair after the other three, using an eight-digit code: alpha 1 is FF, 0 is 00 and 0.8 is CC, because 0.8 × 255 is 204. The converter reads only the three channels, so type the numbers without the alpha value.
How do I convert RGB to hex in JavaScript or Python?
For one channel, JavaScript uses n.toString(16).padStart(2, "0") and Python uses f"{n:02X}". Each call gives one pair, so join the three results behind a #. JavaScript gives lowercase digits, Python gives uppercase, and CSS reads both. For this page's red channel, 34 gives 22.