ByteTips.

Guide

HEX to RGB: How the Conversion Actually Works

A hex color is just three base-16 numbers for red, green, and blue. Split it into pairs, convert each pair to decimal, and you have RGB — plus the shorthand and alpha traps.

October 17, 2026 · 7 min read · Shayan · CSS & Design

🛠️ Try it in your browser — no uploads

Skip the manual work and run this on your own input with the free Color Converter.

Open free Color Converter on Byte-Kit →

Try it now

HEX ↔ RGB converter

rgb(255, 87, 51)

Open full Color Converter on Byte-Kit →

Short answer: a hex color like #FF5733 is three numbers written in base 16 — FF red, 57 green, 33 blue. Split the hex into pairs, convert each pair from hexadecimal to decimal (0–255), and you get rgb(255, 87, 51). Same color, different notation. Everything else on this page is detail around that one idea.

Why base 16 at all

Hex exists because one byte — a value from 0 to 255 — is exactly two hexadecimal digits, no more and no less. A color channel is one byte, so a full RGB color is always exactly six hex digits, tidy and fixed-width. Decimal cannot do that: 255, 87, and 51 take different numbers of characters and need separators to read. That is the whole reason designers inherit a programmer's number format — #FF5733 is not three numbers crammed together for style, it is three bytes written in their most compact exact form. Once you see it as bytes, the conversion stops being a trick and becomes a translation: each pair of hex digits is one byte, and converting the pair to decimal is just writing the same byte in the other base.

The 30-second conversion

Take #FF5733 and split it after the #: FF | 57 | 33. Each pair is a hexadecimal number — two base-16 digits. Convert each pair with (first digit × 16) + second digit, where A=10 through F=15:

  • FF → 15 × 16 + 15 = 255 (red)
  • 57 → 5 × 16 + 7 = 87 (green)
  • 33 → 3 × 16 + 3 = 51 (blue)

So #FF5733 is rgb(255, 87, 51). In code, the whole thing is six lines:

function hexToRgb(hex) {
  hex = hex.replace("#", "");
  const r = parseInt(hex.slice(0, 2), 16);
  const g = parseInt(hex.slice(2, 4), 16);
  const b = parseInt(hex.slice(4, 6), 16);
  return { r, g, b };
}
hexToRgb("#FF5733"); // { r: 255, g: 87, b: 51 }

function rgbToHex(r, g, b) {
  return "#" + [r, g, b].map((x) => x.toString(16).padStart(2, "0")).join("");
}
rgbToHex(255, 87, 51); // "#ff5733"

Three worked examples to check your eye

  • #000000 → rgb(0, 0, 0): every pair is 00, so every channel is off — black.
  • #FFFFFF → rgb(255, 255, 255): every pair is FF = 255, every channel at full — white.
  • #808080 → rgb(128, 128, 128): 80 hex is 8 × 16 = 128, exactly halfway — mid grey. Notice 80 is not '80%'; it is 128 of 255, which is why hex midpoints confuse people who read them as percentages.
  • #1E90FF → rgb(30, 144, 255): 1E = 1 × 16 + 14 = 30, 90 = 9 × 16 = 144, FF = 255 — a strong blue with a little red and a lot of green, which matches the steel-blue you see.

Run those four through the formula until the pattern is automatic and you will never need a converter for reading colors again — only for writing them quickly.

Shorthand and alpha: the two variants

Three-digit hex like #F00 is shorthand — each digit is doubled, so #F00 means #FF0000 (pure red). Eight-digit hex like #FF573380 adds an alpha channel as the last pair: 00 is fully transparent, FF is fully opaque. To get the CSS 0–1 alpha, divide by 255 — 80 hex is 128 decimal, so #FF573380 is rgba(255, 87, 51, 0.5).

// Expand 3-digit shorthand first
function expand(hex) {
  hex = hex.replace("#", "");
  return hex.length === 3 ? hex.split("").map((c) => c + c).join("") : hex;
}
expand("F00"); // "FF0000"

// 8-digit hex → rgba()
const alpha = parseInt("80", 16) / 255; // 0.5019…
"rgba(255, 87, 51, " + alpha.toFixed(2) + ")"; // "rgba(255, 87, 51, 0.50)"

When you actually need RGB

For plain CSS, hex is fine and shorter. RGB earns its keep in three places: canvas and WebGL APIs, which hand you decimal channel arrays (getImageData returns 0–255 values); color math like blending or darkening, which is arithmetic on channels; and transparency — rgba(255, 87, 51, 0.5) works everywhere, while 8-digit hex still trips up older browsers and some design tools.

There is a fourth place that matters more every year: design systems. The moment a color needs variations — a hover state 10% darker, a background at 8% opacity over white — someone has to do arithmetic, and arithmetic happens in decimal channels. That is also exactly the conversion the glassmorphism recipes need: How to Create the Glassmorphism Effect with CSS builds its tints as rgba() values, and every one of them starts life as a hex code from a palette that has to be converted pair by pair, alpha included.

Common mistakes and how to fix them

  • Reading hex pairs as percentages. Cause: 80 looks like 80%. Fix: a pair is a byte — 80 hex is 128/255, almost exactly 50%. If you need a percentage of full intensity, divide the decimal value by 255.
  • Dropping the leading zero on the way back. Cause: converting 5 to hex gives '5', and joining naively produces a five-digit color. Fix: padStart(2, '0') on every channel, as in the rgbToHex function above — a channel under 16 is where this bites.
  • Converting a 4-digit hex as if it were shorthand RGB. Cause: #RGBA exists too — four digits are red, green, blue, alpha, each doubled. Fix: check the length before converting: 3 = RGB shorthand, 4 = RGBA shorthand, 6 = RGB, 8 = RGBA.
  • Assuming uppercase matters. Cause: worrying that #ff5733 differs from #FF5733. Fix: it does not — hex digits are case-insensitive everywhere in CSS and code. Pick one style for your codebase and stop thinking about it.

The traps

  • parseInt without a radix: parseInt("FF") returns NaN, because without the second argument it parses as decimal. Always write parseInt(pair, 16).
  • Forgetting the #: split "#FF5733" into pairs without removing it first and every channel shifts — all three values come out wrong.
  • Using shorthand raw: #F00 is not red=15, green=0, blue=0 — it is #FF0000. Double each digit before converting.
  • Reading alpha as 0–100: in 8-digit hex the alpha pair is 00–FF like any other channel. FF means 255/255 = 1, fully opaque. Divide by 255 for the CSS value.
  • Assuming the color changed: conversion only renames the value. If the swatch looks different after converting, the bug is in your code, not the math.

Frequently asked questions

Is #FFF the same as #FFFFFF? Yes — shorthand doubles each digit, so F becomes FF in all three channels. The shorthand only exists for colors whose pairs are doubled digits (#FF0000 → #F00); a color like #FF5733 has no shorthand, because 57 and 33 cannot be written as one doubled digit.

Why do my converted colors look identical but the numbers seem off by one? Rounding in the alpha channel, almost always: 0.5 × 255 is 127.5, which rounds to 128 (80 hex) going one way and reads back as 0.502 going the other. The RGB channels themselves never round — pairs convert exactly — so a one-step difference lives in alpha, and it is far below anything an eye can detect.

What about HSL — should I convert to that instead? Different tool for a different job. HSL describes a color the way humans talk about it (hue, saturation, lightness), which makes it the right format for generating variations of a color in code. But screens are driven in RGB, design tools hand you hex, and the conversion on this page is the bridge between the two worlds you actually live in.

Does converting change how the color prints or displays? No. Hex and RGB are two spellings of one value; every spelling reaches the screen as the same three channel intensities. If a converted color genuinely looks different somewhere, the cause is a color-profile or a bug — never the arithmetic.

Try it

Paste any hex into the Color Converter — it shows the RGB, HSL, and a live preview instantly, all in your browser with nothing uploaded.

Written by Shayan — Creator of Byte-Kit & ByteTips. Building private developer tools.

More CSS & Design guides