HEX to RGB: The Complete Conversion Guide

Understand hexadecimal encoding, RGB color model, and when to use each format in CSS and design workflows.

7 min read Color · CSS · Web 6 sections + FAQ

Every color you see on a screen is ultimately three numbers: red, green, and blue. The question is only how those numbers are written. HEX notation packs them into a six-character string familiar to designers and developers alike. RGB notation unpacks them into three explicit values used in CSS functions, design tokens, and data pipelines.

This guide covers the complete picture — the math behind hexadecimal encoding, the RGB model, a step-by-step manual conversion, and the practical rules for choosing between HEX and RGB in real CSS and design workflows.

How hexadecimal encoding works

Hexadecimal is a base-16 numbering system using digits 0–9 and letters A–F, where A=10, B=11, C=12, D=13, E=14, F=15. A two-character hex pair can represent values from 00 (0 in decimal) to FF (255 in decimal). This is exactly the range needed for a single color channel. So a six-character HEX code like #3A7BD5 is three two-character pairs: 3A (red), 7B (green), D5 (blue). The # prefix is a CSS convention, not part of the numeric value. Shorthand codes like #FFF expand to #FFFFFF — each digit is doubled.

Free Tool HEX to RGB Converter Instantly convert any HEX code to RGB and HSL values

The RGB color model

RGB stands for Red, Green, Blue — the three primary channels of additive color mixing used by every digital display. Each channel ranges from 0 (none) to 255 (maximum). rgb(0, 0, 0) is pure black; rgb(255, 255, 255) is pure white. The model is additive: mixing full red and full green produces yellow (rgb(255, 255, 0)). This is opposite to the subtractive mixing used in paint or print (where mixing pigments produces darker results). CSS also accepts the modern space-separated syntax: color: rgb(58 123 213) without commas, which is equivalent to rgb(58, 123, 213).

Manual conversion step by step

To convert #3A7BD5 to RGB: split into pairs 3A, 7B, D5. Convert each pair from hex to decimal. For 3A: 3 × 16 + 10 = 58. For 7B: 7 × 16 + 11 = 123. For D5: 13 × 16 + 5 = 213. Result: rgb(58, 123, 213). For the reverse, divide each decimal value by 16, take the quotient as the first hex digit and the remainder as the second.

#3A7BD5

R = 3A → 3×16 + 10 = 58
G = 7B → 7×16 + 11 = 123
B = D5 → 13×16 + 5 = 213

Result: rgb(58, 123, 213)
Free Tool Color Palette Generator Generate harmonious palettes from any HEX color

HEX vs RGB in CSS: when to use each

HEX is compact and universally supported. Use it for static color values in CSS, design systems, and anywhere brevity matters. RGB is preferable when you need to manipulate the color in JavaScript or CSS calc(). The modern CSS color() and oklch() functions are increasingly common for gamut-wide color, but HEX and RGB remain dominant for everyday design work. CSS custom properties work equally well with both: --brand: #3A7BD5 or --brand: rgb(58 123 213).

Alpha channel: HEX8 vs rgba()

Transparency is added differently in each format. In HEX, append two more hex digits for alpha: #3A7BD5FF is fully opaque, #3A7BD580 is 50% transparent (128/255 ≈ 0.5). In CSS, rgba() has been the traditional approach: rgba(58, 123, 213, 0.5). Modern CSS allows rgb() to accept an optional alpha too: rgb(58 123 213 / 0.5). HEX8 is supported in all modern browsers but can be less readable. rgba() is more explicit and easier to adjust dynamically with CSS variables or JavaScript.

Practical use cases

Design tokens typically store colors in HEX for portability across tools (Figma, Storybook, Style Dictionary). CSS variables can hold either format. When passing colors to Canvas API or WebGL shaders, you need RGB values — the HEX string must be parsed first. JavaScript's parseInt("3A", 16) handles this. For CSS animations with color-mix() or @property transitions, the rgb() or hsl() format gives browsers more interpolation flexibility than opaque hex strings.

Free Tool Tint & Shade Generator Create light and dark variants of any color

Frequently Asked Questions

What is a HEX color code? +
A HEX color code is a six-character hexadecimal string (prefixed with #) that encodes red, green, and blue channel values. Each pair of characters represents one channel as a value from 00 (0) to FF (255). For example, #FF0000 is pure red: red=255, green=0, blue=0.
How do I convert HEX to RGB manually? +
Split the HEX code into three two-character pairs (RR, GG, BB). Convert each pair from base-16 to base-10 using the formula: first_digit × 16 + second_digit. For #1A2B3C: R = 1×16+10 = 26, G = 2×16+11 = 43, B = 3×16+12 = 60. Result: rgb(26, 43, 60).
What is the difference between HEX and RGB in CSS? +
Both represent the same sRGB color space but differ in syntax. HEX (#RRGGBB) is more compact and common in static stylesheets. RGB (rgb(R, G, B)) is more explicit and easier to manipulate programmatically. Modern CSS also supports rgb() with space-separated values and an optional alpha: rgb(58 123 213 / 0.5).
How many colors can HEX represent? +
Six hex digits give 256 × 256 × 256 = 16,777,216 possible colors. This covers the full sRGB gamut, which is the standard color space for web displays. Wide-gamut displays (P3, Rec2020) require the CSS color() function for extended range beyond sRGB.
Is HEX always accurate for print or physical colors? +
No. HEX defines a color in the sRGB screen color space. When you need to reproduce a color in print (CMYK) or industrial coating (RAL, Pantone), HEX is only an approximation. Color management software converts between spaces, but metamerism means screen colors can look different under different lighting when printed.
When should I use rgba() instead of HEX? +
Use rgba() when you need transparency that can be changed dynamically — for example, with CSS custom properties or JavaScript. HEX8 (#RRGGBBAA) also supports transparency but is less readable. For static opaque colors, HEX is simpler and equally valid.
Does the HEX to RGB converter on PixCode support all CSS color formats? +
The PixCode HEX to RGB tool accepts any valid 3-digit or 6-digit HEX code and outputs decimal RGB values. It also shows HSL equivalents. For wider CSS color format support (oklch, color(), P3), additional CSS unit tools are available on the platform.