RGB to HEX and HEX to RGB: The Complete Conversion Guide 2026

By IWantFreeColorTools.com Editorial Team · 8 min read

Quick Answer

RGB to HEX conversion turns three decimal channels from 0 to 255 into a six-character hexadecimal code. Convert each channel to a two-digit hex value from 00 to FF and join them behind a hash. So rgb(255, 87, 51) becomes #FF5733. Going the other way, split a HEX code into three pairs and read each as a decimal number, so #FF5733 is rgb(255, 87, 51). Both notations describe the exact same sRGB color, just written differently.

Converting RGB to HEX means taking a color written as three decimal numbers, like rgb(255, 87, 51), and rewriting it as a six-character hexadecimal code, like #FF5733. Converting HEX to RGB is the same steps in reverse. Both formats describe the identical color, so nothing is lost in the swap when your channels are whole numbers. This guide covers both directions by hand and in code, the valid HEX lengths, where alpha goes on different platforms, when to use each format, and a reference table.

What Are RGB and HEX?

RGB stands for red, green, blue. It describes a color by the intensity of each of those three channels, written as a decimal number from 0 to 255, like rgb(255, 87, 51).

A HEX code is the same three channels written in base 16. Each channel becomes two characters from 00 (off) to FF (maximum), and the three pairs sit together behind a hash, like #FF5733. Hexadecimal uses the digits 0 to 9 and then the letters A to F for ten to fifteen. Two hex digits hold exactly 256 values, the same 256 levels as an 8-bit channel, which is why one RGB channel always maps to exactly two hex characters with no rounding. The W3C CSS Color Module Level 4 says the hex notation "allows an sRGB color to be specified by giving the components as hexadecimal numbers," with the first pair the red component, then green, then blue. Our HEX color codes explained guide breaks the format down further.

You'll see RGB written two ways, and both are correct. The same spec defines a modern form with spaces between the channels and alpha after a slash, so rgb(255 87 51) equals rgb(255, 87, 51), and half opacity is rgb(255 87 51 / 0.5). It calls rgba() an alias of rgb(), so the older comma form still works. None of this changes the conversion: the three channel numbers are the same either way.

How Do You Convert RGB to HEX?

The conversion is a simple three-step process. Take rgb(255, 87, 51):

  1. Take each channel on its own. Red is 255, green is 87, blue is 51.
  2. Convert each to two hex digits. Divide the value by 16 for the first digit and take the remainder for the second, mapping 10 to 15 onto A to F. So 87 divided by 16 is 5 remainder 7, which is 57. And 255 is FF, 51 is 33.
  3. Join them behind a hash. #FF5733.
rgb(255, 87, 51) to #FF5733

One rule to remember: pad any single-digit result with a leading zero so every channel is exactly two characters. A blue value of 5 converts to 5 in hex, but you write it as 05, not 5, or the code breaks. A converter pads automatically.

How Do You Convert HEX to RGB?

Run the same steps backwards. Take #FF5733:

  1. Split the code into pairs. FF, 57 and 33 for red, green and blue.
  2. Convert each pair to decimal. The first digit is worth 16 times its value and the second is worth its value, with A to F standing for 10 to 15. So 57 is 5 times 16 plus 7, which is 87, FF is 15 times 16 plus 15, which is 255, and 33 is 51.
  3. Write the RGB value. rgb(255, 87, 51).
#FF5733 to rgb(255, 87, 51)

Shorter codes expand first. MDN documents four lengths: in the 3-digit and 4-digit forms each digit is doubled, so #F53 is #FF5533, and the 4-digit and 8-digit forms carry a fourth value for alpha. To read alpha, convert that pair to decimal and divide by 255: an alpha pair of 80 is 128 out of 255, or about 50% opacity.

How Do You Convert Between RGB and HEX in Code?

In JavaScript, RGB to HEX takes two built-in methods:

const toHex = (r, g, b) =>
  '#' + [r, g, b]
    .map(n => n.toString(16).padStart(2, '0'))
    .join('');

toHex(255, 87, 51);  // "#ff5733"

MDN's Number.prototype.toString reference documents the radix argument as an integer from 2 to 36, and notes that for bases above 10 "the letters of the alphabet indicate digits greater than 9." String.prototype.padStart pads a string to a given length, so "5".padStart(2, "0") gives "05". Drop it and rgb(5, 10, 20) produces "#5a14" instead of "#050a14". Four hex digits is a legal CSS form with alpha, so the browser renders #55aa1144, a translucent green, with no error.

HEX to RGB is the reverse, using parseInt with a radix of 16:

const toRgb = hex => {
  let h = hex.replace(/^#/, '');
  if (h.length === 3) h = h.split('').map(c => c + c).join('');
  return [0, 2, 4].map(i => parseInt(h.slice(i, i + 2), 16));
};

toRgb('#FF5733');  // [255, 87, 51]

In Python, the format specification does both jobs of the RGB to HEX direction at once, and int() with base 16 handles the reverse:

def to_hex(r, g, b):
    return '#{:02x}{:02x}{:02x}'.format(r, g, b)

def to_rgb(h):
    h = h.lstrip('#')
    return tuple(int(h[i:i + 2], 16) for i in (0, 2, 4))

to_hex(255, 87, 51)  # '#ff5733'
to_rgb('#FF5733')   # (255, 87, 51)

Read {:02x} as three instructions: x means hexadecimal, 2 means two characters wide, and the leading 0 means pad with zeros. Swap x for X if you want uppercase.

Two guards are worth adding in production. Round first, because toString(16) on a non-integer gives a fractional string like "7f.8". And clamp to 0 through 255, because 300 becomes "12c", three characters, and corrupts the rest of the code:

const clamp = n => Math.min(255, Math.max(0, Math.round(n)));

Does the Conversion Ever Lose Anything?

For whole numbers from 0 to 255, no. The edge cases come from inputs that don't fit that shape.

Which HEX Formats Are Actually Valid?

Six digits is what you'll write most days, but CSS allows four lengths, per MDN's hex-color reference.

The shorthand is not an approximation: #FC0 and #FFCC00 are the identical color. And MDN notes the syntax is case-insensitive, so #00ff00 and #00FF00 are the same value.

Does an 8-Digit HEX Code Mean the Same Thing Everywhere?

No. In CSS, MDN lists #RGB, #RGBA, #RRGGBB and #RRGGBBAA, with alpha last. Android's documentation on color resources says a value "always begins with a pound (#) character, which is followed by the Alpha-Red-Green-Blue information," and lists #RGB, #ARGB, #RRGGBB and #AARRGGBB. Alpha first.

Android's own example defines #80ff0000 as translucent red: alpha 80, then ff0000. Paste that string into CSS and a browser reads it as red 80, green ff, blue 00, alpha 00, a chartreuse at zero opacity, so you see nothing at all. Six-digit codes move between the platforms unchanged; only the transparent ones break.

When Should You Use RGB vs HEX?

Both are fully supported in CSS and describe the same colors, so the choice is about what's convenient for the job in front of you.

Neither HEX nor RGB tells you how light a color looks, so check contrast with our contrast checker rather than by eye. For two values per color in light and dark themes, see our dark mode color guide, and for HSL and the newer formats, CSS color formats explained.

What Are Common RGB to HEX Values?

A handful of colors come up again and again. Here's a quick reference with both notations side by side.

ColorRGBHEX
Blackrgb(0, 0, 0)#000000
Whitergb(255, 255, 255)#FFFFFF
Redrgb(255, 0, 0)#FF0000
Green (lime)rgb(0, 255, 0)#00FF00
Bluergb(0, 0, 255)#0000FF
Yellowrgb(255, 255, 0)#FFFF00
Orangergb(255, 165, 0)#FFA500
Greyrgb(128, 128, 128)#808080
Off-whitergb(245, 245, 245)#F5F5F5
Navyrgb(30, 58, 95)#1E3A5F

A channel that's fully on becomes FF, and one that's off becomes 00. Red is 255 red and nothing else, so #FF0000. Grey at 128 lands on 80 in hex, which is why balanced greys read as #808080.

How Do You Use a Free RGB to HEX Converter?

Our converter runs in your browser and converts both ways between HEX, RGB, HSL and CMYK:

  1. Open the tool. Go to the free color converter.
  2. Type into any field. Enter RGB as three numbers from 0 to 255, such as 255, 87, 51, or a 3-digit or 6-digit HEX code with or without the hash.
  3. Read the other formats. The remaining fields update, with a preview swatch so you can confirm the color.
  4. Copy and paste. Each field has a copy button.

It has no alpha field, so convert 4-digit and 8-digit codes and rgba() values by hand using the steps above. To grab a color off the screen first, the color picker gives you RGB and HEX values.

What Else Do People Ask?

How do you convert RGB to HEX?

Take each of the three RGB channels, a decimal number from 0 to 255, and convert it to a two-digit hexadecimal value from 00 to FF, then join them behind a hash. For example, rgb(255, 87, 51) becomes 255 which is FF, 87 which is 57, and 51 which is 33, giving #FF5733. Pad any single-digit result with a leading zero so each channel is two characters.

How do you convert HEX to RGB?

Split the six-digit HEX code into three pairs and convert each from base 16 to a decimal number from 0 to 255. For #FF5733, FF is 255, 57 is 87 and 33 is 51, giving rgb(255, 87, 51). A 3-digit code such as #F53 expands to #FF5533 first.

What is the difference between RGB and HEX?

They describe the same colors in different notations. RGB writes each of the red, green, and blue channels as a decimal number from 0 to 255. HEX writes each channel as a two-digit hexadecimal value from 00 to FF. RGB is easier to read and supports an alpha channel for transparency, while HEX is compact and standard in design tools.

Is RGB or HEX better for CSS?

Neither is better, since both are fully supported in CSS and describe the same sRGB colors. HEX is convenient for pasting exact brand colors from design tools and keeps stylesheets tidy. RGB is handy when you need transparency through rgba or when generating colors in code. Many developers use HSL for adjusting a color's lightness and saturation.

Can every RGB color be written as HEX?

Yes, every standard RGB color converts to a HEX code with no loss, because both use the same 8-bits-per-channel sRGB values. The only exception is the alpha channel: an rgba color with partial transparency needs an 8-digit HEX code to carry the opacity, which some older tools support less reliably than rgba.

What is rgba and how does it become HEX?

rgba is RGB plus an alpha value between 0 and 1 that sets opacity, like rgba(255, 87, 51, 0.5) for 50 percent. To write it as HEX you convert the three color channels normally, then add a fourth two-digit pair for the alpha, making an 8-digit code. Half opacity is roughly 80 in hex, so the example becomes #FF573380.

Sources: W3C CSS Color Module Level 4 (w3.org) for hex notation as an sRGB color with red, green and blue pairs in order, rgba() as an alias of rgb(), the space and slash syntax, numbers and percentages that "may be freely mixed", the rounding rule for components and the clamping of alpha to 0 to 1; MDN Web Docs (developer.mozilla.org) on the CSS hex-color type listing #RGB, #RGBA, #RRGGBB and #RRGGBBAA with doubled shorthand digits, alpha last and case-insensitive syntax, on the color() function and the implicit sRGB space of other color functions, on Number.prototype.toString and its radix from 2 to 36, on String.prototype.padStart, and on parseInt with a radix; Python documentation (docs.python.org) on the format specification mini-language (x, X, width and zero padding) and on int() with a base; Android developer documentation on color resources (developer.android.com), which lists #RGB, #ARGB, #RRGGBB and #AARRGGBB as "Alpha-Red-Green-Blue" information and gives #80ff0000 as translucent red. All linked above.

Get fresh reads straight to your inbox

Get notified when we publish new articles. Unsubscribe anytime.