ColorMar 12, 2026 ยท Updated Oct 13, 2026 ยท 3 min read

How to Convert HEX to RGB (and Back)

How HEX maps to RGB, including 3-, 4- and 8-digit codes. We tested the common slice-and-parseInt snippet against Chromium's CSS parser: it misreads short codes, drops alpha and returns NaN instead of an error.

๐ŸŽจ
Try the Color Converter
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01What a HEX Color Code Is02The Manual Conversion03Converting in JavaScript, and Where Snippets Break04HEX With Alpha: 256 Steps05Which Format to Use in CSS06Common Conversions

What a HEX Color Code Is

A HEX color is a hash sign followed by hexadecimal digits for red, green and blue, and optionally alpha. CSS Color 4 allows four lengths: #RGB, #RGBA, #RRGGBB and #RRGGBBAA. In the short forms each digit is doubled, so #F09 is #FF0099, and the notation is case-insensitive. Each pair runs from 00 (0) to FF (255).

The Manual Conversion

Split the code into pairs and read each as base 16: first digit ร— 16 + second digit, with A-F meaning 10-15. For #2C3E50: 2C = 2 ร— 16 + 12 = 44, 3E = 3 ร— 16 + 14 = 62, 50 = 5 ร— 16 + 0 = 80, so rgb(44 62 80). Going back, write each channel as two hex digits: 44 โ†’ 2C, 62 โ†’ 3E, 80 โ†’ 50.

Converting in JavaScript, and Where Snippets Break

The snippet that circulates (and that this guide used to show) slices characters 1-2, 3-4 and 5-6 and runs parseInt(..., 16). We ran it against Chromium's own CSS parser on eight inputs (October 13, 2026):

InputSlice-and-parseInt snippetBrowser reads it as
#2C3E5044, 62, 80rgb(44, 62, 80)
#F00240, 0, NaNrgb(255, 0, 0)
#f00a240, 10, NaNrgba(255, 0, 0, 0.667)
#FF6B6B80255, 107, 107 (alpha dropped)rgba(255, 107, 107, 0.5)
FF6B6B (no #)246, 182, 11invalid in standards mode
#GG0000NaN, 0, 0invalid

So a converter needs to expand short forms, keep alpha and reject bad input instead of returning NaN. A version that does all three:

function hexToRgb(hex) {
  let h = hex.trim().replace(/^#/, '');
  if (!/^([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(h)) return null;
  if (h.length <= 4) h = [...h].map(c => c + c).join('');
  const n = h.match(/../g).map(x => parseInt(x, 16));
  return { r: n[0], g: n[1], b: n[2], a: n.length === 4 ? +(n[3] / 255).toFixed(3) : 1 };
}
const toHex = (r, g, b) => '#' + [r, g, b].map(v => v.toString(16).padStart(2, '0')).join('');

If the code runs in a browser, you can also let CSS do the parsing: set the value on an element's style.color, and an empty result means the browser rejected it.

HEX With Alpha: 256 Steps

The last pair of an eight-digit code is alpha in 256 steps, so it cannot hit every percentage exactly. Chromium reads 80 (128/255 = 0.502) back as 0.5, 7F (127/255) as 0.498, CC as 0.8 and 33 as 0.2. 50% is 80, 20% is 33, 80% is CC. MDN's compatibility data has eight-digit hex in Chrome 62, Firefox 49, Safari 10 and Edge 79, so every current browser reads it.

Which Format to Use in CSS

There is no visual difference: #FF6B6B and rgb(255 107 107) are the same sRGB color. In CSS Color 4, rgba() is just an alias of rgb(), and the modern syntax takes alpha after a slash: rgb(255 107 107 / 50%). Use hex where you copy from design tools, the function form where you want to read or edit channels, and oklch() when you need lightness steps that look even. The Color Converter reads every CSS Color 4 syntax, including named colors as input, and returns HEX, RGB, HSL, OKLCH and other forms; its CMYK row uses the simple formula without an ink profile, so treat it as a starting point for print.

Common Conversions

#FFFFFF = rgb(255 255 255), #000000 = rgb(0 0 0), #FF0000 = rgb(255 0 0), #00FF00 = rgb(0 255 0), #0000FF = rgb(0 0 255), #808080 = rgb(128 128 128).

Frequently Asked Questions

How do I convert HEX to RGB?+
Expand a 3- or 4-digit code by doubling each digit, split into pairs, and read each pair as base 16 (first digit ร— 16 + second). #FF6B6B becomes rgb(255 107 107). An eighth and ninth digit pair is alpha out of 255.
Is there a difference between HEX and RGB in CSS?+
No visual difference: both describe the same sRGB color. Both can carry alpha too, as #RRGGBBAA or rgb(r g b / a); in CSS Color 4 rgba() is an alias of rgb().
What does the # mean in a HEX code?+
The hash symbol (#) is a prefix that tells the browser the following characters are a hexadecimal color value. It's required in CSS but optional in some design tools.
Try it yourself

Use the Color Converter โ€” free, no signup required.

โšก Open Color Converter
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.