By Ahsan Khowaja (CEO) · Updated 10 November 2026
On this page
- How to use the RGB to hex converter
- The RGB to hex formula
- Worked examples
- Why colors are written in hex
- When an RGB to hex code can be shortened
- Hex color code chart: the 16 basic colors
- RGB to HSL
- Convert RGB to hex in JavaScript, Python and Excel
- What the hex code doesn't tell you
- Questions and answers
- Related calculators
How to use the RGB to hex converter
- Type the red, green and blue values. Each is a whole number from 0 (none of that light) to 255 (as much as possible). Design apps show them as "R 255 G 87 B 51" or in CSS form.
- Read the hex code, which updates as you type. The example above gives
#FF5733. - Copy the form you need. You also get the 3-digit shorthand (only when the color has one), the CSS
rgb()string, the HSL version and the 24-bit decimal that some apps and APIs want.
Got percentages, as in rgb(100%, 34%, 20%)? Multiply each one by 2.55 and round before you type it in. 34% × 2.55 = 86.7, so 87. If you have the hex code and want the numbers, the hex to RGB converter goes the other way.
The RGB to hex formula
Each channel covers 256 levels, and two hex digits cover that range exactly, because 16 × 16 is 256. To convert one channel:
- Divide by 16. The whole-number part is the first hex digit.
- Take the remainder as the second hex digit.
- Write any digit above 9 as a letter, so 10 = A up to 15 = F.
For red 255, 255 ÷ 16 = 15 remainder 15, so FF. Green 87 gives 5 remainder 7, so 57. Blue 51 gives 3 remainder 3, so 33. Join the three pairs after a # in the order #RRGGBB and you have #FF5733.
Order matters, and the W3C CSS Color Module Level 4 fixes it. "The first pair of digits, interpreted as a hexadecimal number, specifies the red component of the color", the next pair specifies green and the last pair blue (W3C CSS Color Module Level 4). Swap the pairs and the color changes. #5733FF has the same digits as #FF5733 but reads as red 57, green 33 and blue FF, a blue-violet.
Worked examples
| RGB color values | Working | Hex code |
|---|---|---|
| rgb(255, 87, 51) | 255 → FF, 87 = 5 × 16 + 7 → 57, 51 = 3 × 16 + 3 → 33 | #FF5733 |
| rgb(30, 144, 255) | 30 = 1 × 16 + 14 → 1E, 144 = 9 × 16 → 90, 255 → FF | #1E90FF (dodgerblue) |
| rgb(255, 165, 0) | 165 = 10 × 16 + 5 → A5, 0 → 00 | #FFA500 (orange) |
| rgb(70, 130, 180) | 70 → 46, 130 = 8 × 16 + 2 → 82, 180 = 11 × 16 + 4 → B4 | #4682B4 (steelblue) |
| rgb(0, 128, 0) | 128 = 8 × 16 → 80 | #008000 (green) |
| rgb(128, 0, 128) | 128 → 80, 0 → 00 | #800080 (purple) |
Names in brackets are CSS named colors. The CSS Color specification defines them with exactly these values, so you can check the converter against them.
Why colors are written in hex
Screens mix three colors of light. In the standard 8-bit sRGB system each channel has 256 levels, which gives 256 × 256 × 256 = 16,777,216 colors (the "16.7 million colors" of monitor specs). Hex packs all three channels into six characters, which is why HTML and CSS adopted it. color: #FF5733; is shorter than color: rgb(255, 87, 51); and means exactly the same thing. Browsers treat the two forms identically (MDN: hex color).
That 24-bit decimal in the result is the same six hex digits read as a single number, red × 65,536 + green × 256 + blue. For #FF5733 that is 16,734,003. A few places ask for it, such as bot and chat-embed APIs that store a color as an integer.
When an RGB to hex code can be shortened
CSS allows a 3-digit shorthand, #RGB, when each of the three pairs is a doubled digit. #FFFFFF becomes #FFF, #000000 becomes #000 and #FF6600 becomes #F60. Browsers expand each digit by repeating it, so they read #F60 as #FF6600.
Most colors can't be shortened. #FF5733 has no shorthand because 57 and 33 aren't doubled digits, and the converter says "None" for it. Don't drop digits by hand: #F53 would be #FF5533, a slightly different color.
Hex color code chart: the 16 basic colors
These sixteen named colors go back to the original HTML 4 palette and are now part of the CSS named colors. They help you recognize a code at a glance.
| Color | RGB | Hex | Shorthand |
|---|---|---|---|
| Black | 0, 0, 0 | #000000 | #000 |
| White | 255, 255, 255 | #FFFFFF | #FFF |
| Red | 255, 0, 0 | #FF0000 | #F00 |
| Lime | 0, 255, 0 | #00FF00 | #0F0 |
| Blue | 0, 0, 255 | #0000FF | #00F |
| Yellow | 255, 255, 0 | #FFFF00 | #FF0 |
| Cyan / aqua | 0, 255, 255 | #00FFFF | #0FF |
| Magenta / fuchsia | 255, 0, 255 | #FF00FF | #F0F |
| Gray | 128, 128, 128 | #808080 | none |
| Silver | 192, 192, 192 | #C0C0C0 | none |
| Maroon | 128, 0, 0 | #800000 | none |
| Olive | 128, 128, 0 | #808000 | none |
| Green | 0, 128, 0 | #008000 | none |
| Purple | 128, 0, 128 | #800080 | none |
| Teal | 0, 128, 128 | #008080 | none |
| Navy | 0, 0, 128 | #000080 | none |
One row catches a lot of people out. CSS green is the half-strength #008000, and the full-strength #00FF00 is called lime.
RGB to HSL
HSL describes the same color as a hue angle (0° to 360° around the color wheel), a saturation percentage and a lightness percentage. Designers like it because "make it a bit lighter" is a bigger lightness number, and that is hard to do by editing hex digits.
For the orange example, first scale the channels to 0–1, which gives 1, 0.341 and 0.2. Then:
- Lightness is the average of the highest and lowest channels: (1 + 0.2) ÷ 2 = 0.6, so 60%.
- Saturation is the range divided by 1 − |2L − 1|: 0.8 ÷ 0.8 = 1, so 100%.
- Hue, with red as the highest channel, is 60° × (G − B) ÷ range = 60 × 0.141 ÷ 0.8 ≈ 11°.
So the result is hsl(11, 100%, 60%). We round to whole degrees and whole percents, which is how CSS tools usually show it.
Convert RGB to hex in JavaScript, Python and Excel
For one color, the converter is quickest. For a list of them, these one-liners use the same formula. They don't check their input, though, so a value like 300 or 12.5 gives a wrong code without any warning.
In JavaScript, convert each channel to base 16 with toString(16), pad to two digits, and join.
const rgbToHex = (r, g, b) => '#' + [r, g, b].map((n) => n.toString(16).padStart(2, '0')).join('');
rgbToHex(255, 87, 51); // "#ff5733"
Python's X format code prints a number in uppercase hex, and 02 pads it to two digits.
'#{:02X}{:02X}{:02X}'.format(255, 87, 51) # '#FF5733'
In Excel or Google Sheets, DEC2HEX(number, places) converts one channel. With red in A2, green in B2 and blue in C2, RGB to hex in Excel is:
="#"&DEC2HEX(A2,2)&DEC2HEX(B2,2)&DEC2HEX(C2,2)
All three assume whole numbers. Round first if your values are decimals.
What the hex code doesn't tell you
A hex code only has meaning inside a color space, and that is where most "my color looks wrong" problems start. Almost everything on the web assumes sRGB, so #FF5733 looks the same on any calibrated screen. But the same three numbers taken from a Display P3 or Adobe RGB photo describe a more saturated color, so a color sampled in a photo editor can look different once it's pasted into CSS. Our guide to sRGB, Adobe RGB and Display P3 explains how profiles change what the numbers mean.
Six-digit hex also has no transparency. For a semi-transparent color, use eight digits (#FF573380 is 50% opaque) or rgb(255 87 51 / 50%). Our hex converter reads eight-digit codes and shows the alpha as a decimal.
Once you have the color, the color contrast checker tells you whether text in it is readable on your background.
