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 - HSL
hsl(142, 71%, 45%) - CMYK
cmyk(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.
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
Related guides
Frequently asked questions
Why is my hex code only five characters long?
Can I use percentages in rgb()?
How do I get a hex code from rgba()?
How do I convert RGB to hex in JavaScript or Python?
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.Related tools
Contrast Checker
Check color contrast ratios for WCAG AA/AAA accessibility compliance.
Use toolJSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use toolAspect Ratio Visualizer
Visually compare width differences between 16:9, 21:9, and 32:9 monitors.
Use tool