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.
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):
| Input | Slice-and-parseInt snippet | Browser reads it as |
|---|---|---|
#2C3E50 | 44, 62, 80 | rgb(44, 62, 80) |
#F00 | 240, 0, NaN | rgb(255, 0, 0) |
#f00a | 240, 10, NaN | rgba(255, 0, 0, 0.667) |
#FF6B6B80 | 255, 107, 107 (alpha dropped) | rgba(255, 107, 107, 0.5) |
FF6B6B (no #) | 246, 182, 11 | invalid in standards mode |
#GG0000 | NaN, 0, 0 | invalid |
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).