Apps & Software

HEX to RGB Color Code Conversion: Math Formulas, CSS RGBA & Contrast Guide

Master color conversions between Hexadecimal (#RRGGBB) and RGB/RGBA channels. Understand base-16 math, shorthand 3-digit hex codes, CSS alpha transparency, and WCAG accessibility contrast.

Quick Summary & Key Takeaways

  • Objective: Master color conversions between Hexadecimal (#RRGGBB) and RGB/RGBA channels. Understand base-16 math, shorthand 3-digit hex codes, CSS alpha transparency, and WCAG accessibility contrast.
  • Estimated Time: Approximately 6 minutes following our step-by-step instructions.
  • Safety Recommendation: Ensure your device battery is above 30% and preserve any important files or passwords before modifying system settings.
Creative web design color swatches palette with digital code overlays
Converting hexadecimal color strings to 8-bit RGB and RGBA channels.

In web development, graphic design, and UI styling, color is represented primarily through two color models: Hexadecimal (HEX) and Red-Green-Blue (RGB). While HEX codes like #2563EB are compact and standard in CSS and HTML, RGB format allows for direct manipulation of red, green, blue color channels and alpha transparency (rgba(37, 99, 235, 0.85)).

Understanding the mathematical relationship between base-16 and base-10 numbers allows you to convert colors manually, debug CSS stylesheets, and build accessible user interfaces.

In this guide, we explore the exact conversion formulas, demonstrate alpha channel expansion, and explain WCAG contrast accessibility ratios.

🚀 Try the Live Utility: Convert color formats instantly with live swatches and WCAG contrast scoring using our free HEX ↔ RGB Converter.


1. How Hexadecimal Color Codes Work

A standard HEX color code consists of a hash symbol (#) followed by 6 characters:

$\text{#} \mathbf{RR} \mathbf{GG} \mathbf{BB}$

  • RR: Red intensity, ranging from 00 (none) to FF (maximum).
  • GG: Green intensity, ranging from 00 to FF.
  • BB: Blue intensity, ranging from 00 to FF.

Hexadecimal is a base-16 numbering system using sixteen distinct symbols:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A (10), B (11), C (12), D (13), E (14), F (15).

Because each channel is allocated 2 hex digits, the maximum value is:

$\text{FF}_{16} = (15 \times 16^1) + (15 \times 16^0) = 240 + 15 = 255_{10}$

This matches the 8-bit color depth of standard optical displays, where each color channel can have 256 discrete levels (0 to 255). Combining 3 channels yields $256 \times 256 \times 256 = \mathbf{16,777,216 \text{ possible colors}}$.


2. Converting HEX to RGB: The Math Formula

To convert a 2-digit hex pair into an RGB integer from 0 to 255:

$\text{Value} = (\text{First Digit} \times 16) + \text{Second Digit}$

Practical Example: Converting #2563EB

  • Red Pair (25):

$(2 \times 16) + 5 = 32 + 5 = \mathbf{37}$

  • Green Pair (63):

$(6 \times 16) + 3 = 96 + 3 = \mathbf{99}$

  • Blue Pair (EB):

$(14 \times 16) + 11 = 224 + 11 = \mathbf{235}$

Result: #2563EB converts to rgb(37, 99, 235).


3. Converting RGB to HEX: The Division Method

To convert an RGB channel value (0–255) back into hexadecimal:

  1. Divide the number by 16 to find the whole integer (first digit).
  2. Take the remainder to find the second digit.

Practical Example: Converting rgb(239, 68, 68)

  • Red (239):

$239 \div 16 = 14$ remainder $15 \rightarrow \text{14 is E, 15 is F} \rightarrow \mathbf{EF}$

  • Green (68):

$68 \div 16 = 4$ remainder $4 \rightarrow \mathbf{44}$

  • Blue (68):

$68 \div 16 = 4$ remainder $4 \rightarrow \mathbf{44}$

Result: rgb(239, 68, 68) converts to #EF4444 (Tailwind Red 500).


4. Shorthand 3-Digit and 8-Digit Alpha Hex Codes

3-Digit Shorthand HEX:

When both digits of each channel are identical, CSS allows 3-digit shorthand:

  • #F00 expands to #FF0000 (Pure Red)
  • #0F0 expands to #00FF00 (Pure Green)
  • #38B expands to #3388BB

8-Digit HEX with Alpha Transparency (#RRGGBBAA):

Modern browsers and CSS Color Module Level 4 support an optional fourth pair representing the alpha channel:

  • #2563EB80: The 80 represents $128 / 255 \approx 50\%$ opacity.
  • In CSS RGBA syntax: rgba(37, 99, 235, 0.5).

5. Web Accessibility: WCAG Contrast Ratios

When selecting colors for text and backgrounds, the Web Content Accessibility Guidelines (WCAG 2.1) specify minimum relative luminance contrast ratios:

  • WCAG Level AA (Normal Text): Requires a contrast ratio of at least 4.5:1 against the background.
  • WCAG Level AA (Large Text 18pt+ / Bold 14pt+): Requires at least 3.0:1.
  • WCAG Level AAA (Enhanced Contrast): Requires at least 7.0:1 for body text.

Always test your foreground text color against its background to ensure readable, accessible digital experiences for users with low vision or color vision deficiencies.

Use our interactive HEX ↔ RGB Converter to test color codes, tweak sliders, and verify WCAG compliance instantly.

Frequently Asked Questions

What does the '#' sign in a HEX color code mean?

The hash symbol (#) informs web browsers, design tools, and parsers that the following string of characters should be interpreted as a base-16 hexadecimal color value rather than a plain string or variable.

How do I convert a 3-digit HEX code to 6 digits?

Duplicate each character: #1A3 becomes #11AA33. The first character repeats for Red, the second for Green, and the third for Blue.

Can HEX codes have transparency like RGBA?

Yes. By appending two extra hexadecimal characters to make an 8-digit HEX code (#RRGGBBAA), the final pair defines alpha transparency from 00 (0% opacity) to FF (100% opacity).

What is the difference between RGB and HSL?

RGB defines colors by optical red, green, and blue hardware channels (0-255). HSL defines colors by human perceptual qualities: Hue (angle on color wheel 0-360°), Saturation (vibrancy percentage), and Lightness (brightness percentage).

Elena Rostova

About the Author: Elena Rostova

Cloud & Google Workspace Specialist

Elena researches cloud storage architectures, Google Workspace workflows, browser privacy, and account security solutions.