Skip to content
PictureToLink logoPictureToLink
Menu

RGB to hex converter

To convert RGB to hex, write the red, green and blue values (0 to 255 each) as two base-16 digits apiece and join them after a #, so rgb(255, 87, 51) is #FF5733. Type your three values to get the hex color code, plus the 3-digit shorthand when one exists, the HSL version, the CSS rgb() string and the 24-bit decimal number.

Change any value and the result updates as you type.

0 to 255

0 to 255

0 to 255

Hex code

#FF5733

Lowercase works too: #ff5733

Shorthand hex
NoneOnly possible when every pair repeats, like #FF6600 → #F60
CSS rgb()
rgb(255, 87, 51)
HSL
hsl(11, 100%, 60%)
Decimal (24-bit)
16734003red × 65536 + green × 256 + blue
Each channel in decimal, hex and percent
ChannelDecimalHexPercent
Red255FF100%
Green875734.1%
Blue513320%

Calculated in your browser; nothing you type is sent or stored. Spotted a wrong result? Tell us.

By Ahsan Khowaja (CEO) · Updated 10 November 2026

On this page

How to use the RGB to hex converter

  1. 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.
  2. Read the hex code, which updates as you type. The example above gives #FF5733.
  3. 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:

  1. Divide by 16. The whole-number part is the first hex digit.
  2. Take the remainder as the second hex digit.
  3. 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 valuesWorkingHex 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.

ColorRGBHexShorthand
Black0, 0, 0#000000#000
White255, 255, 255#FFFFFF#FFF
Red255, 0, 0#FF0000#F00
Lime0, 255, 0#00FF00#0F0
Blue0, 0, 255#0000FF#00F
Yellow255, 255, 0#FFFF00#FF0
Cyan / aqua0, 255, 255#00FFFF#0FF
Magenta / fuchsia255, 0, 255#FF00FF#F0F
Gray128, 128, 128#808080none
Silver192, 192, 192#C0C0C0none
Maroon128, 0, 0#800000none
Olive128, 128, 0#808000none
Green0, 128, 0#008000none
Purple128, 0, 128#800080none
Teal0, 128, 128#008080none
Navy0, 0, 128#000080none

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.

Questions and answers

What is the hex code for rgb(255, 87, 51)?

It's #FF5733, a warm orange-red. In HSL it is hsl(11, 100%, 60%), and as a 24-bit decimal it is 16734003.

Are hex colors case-sensitive?

No. #ff5733 and #FF5733 are one color everywhere hex is used. Most code formatters pick lowercase and most design tools display uppercase, so use whichever your style guide prefers.

What's the difference between RGB and hex?

The color is the same. RGB writes the red, green and blue levels as decimal numbers from 0 to 255, and hex writes those three numbers in base 16, two digits each, after a #. rgb(30, 144, 255) and #1E90FF are the identical blue.

How do I convert RGB to CMYK or HSL?

HSL is a straight recalculation and the converter shows it: rgb(255, 165, 0) is hsl(39, 100%, 50%). CMYK is different. It describes ink, not light, and depends on the printer's profile, so no exact formula exists. Trust the conversion your print shop or design app makes.

All image, photo and design calculators