HEX vs RGB Colors: Which Format Should You Use?
Category: Color Theory ยท Published: 2026-08-07 ยท Updated: 2026-10-18
If you have ever built a website, designed an interface in Figma, or tweaked a CSS stylesheet, you have encountered both hexadecimal codes (like #7C5CFF) and RGB strings (like rgb(124, 92, 255)). Both formats describe identical colors on your monitor. Yet developers frequently debate which syntax is superior, when to convert between them, and how modern CSS standards impact their usage.
1. How HEX and RGB Represent Color
Digital computer displays create images by emitting red, green, and blue light across millions of microscopic pixels. This is the additive color model. By mixing varying intensities of Red, Green, and Blue from 0 (off) to 255 (maximum brightness), a display can render 16,777,216 unique colors (256 ร 256 ร 256 = 16.7 million, or 24-bit TrueColor).
- RGB (Red, Green, Blue): Represents colors using decimal numbers (base-10). The syntax is written as
rgb(red, green, blue), where each channel accepts an integer from 0 to 255. Example:rgb(255, 0, 0)represents pure bright red. - HEX (Hexadecimal): Represents the exact same 0-255 channels using base-16 notation. A hexadecimal code consists of a hash symbol followed by six characters: two for Red, two for Green, and two for Blue (
#RRGGBB). Base-16 uses digits 0-9 and letters A-F (where A=10, B=11, C=12, D=13, E=14, F=15). Pure red in HEX is#FF0000.
2. Side-by-Side Comparison Table
| Feature | HEX (#RRGGBB) | RGB / RGBA |
|---|---|---|
| Number System | Base-16 (0-9, A-F) | Base-10 (0-255) |
| String Length | Compact (4 or 7 chars) | Longer (14-22 chars) |
| Human Readability | Requires hex decoding | Intuitive channel numbers |
| Transparency Support | 8-digit hex (#RRGGBBAA) | rgba(R, G, B, Alpha) |
| JavaScript Compatibility | Requires string parsing | Native canvas & math manipulation |
| Browser Support | 100% since HTML 2.0 | 100% since CSS1 |
3. The Mathematical Relationship: Converting HEX to RGB
Converting between HEX and RGB is straightforward arithmetic. Each two-character hex pair maps to a decimal value using the formula:
Let's convert our brand accent color #7C5CFF into RGB step-by-step:
- Red Channel (7C):
7 ร 16 + 12 (C) = 112 + 12 = 124 - Green Channel (5C):
5 ร 16 + 12 (C) = 80 + 12 = 92 - Blue Channel (FF):
15 (F) ร 16 + 15 (F) = 240 + 15 = 255
Result: #7C5CFF = rgb(124, 92, 255). You can perform these conversions automatically using our dedicated HEX to RGB Converter.
4. Transparency Handling: RGBA vs 8-Digit HEX
When you need semi-transparent overlays (like modal backdrops, subtle glass borders, or shadow tinting), both formats support alpha channels:
- RGBA Syntax:
rgba(124, 92, 255, 0.5)sets the alpha transparency to 50% using an intuitive decimal from 0.0 to 1.0. Modern CSS also supports space syntax:rgb(124 92 255 / 50%). - 8-Digit HEX Syntax:
#7C5CFF80appends two extra hex digits representing alpha from 00 (0%) to FF (100%). For 50% opacity, 255 ร 0.5 = 127.5 โ 128, which converts to hex80.
While 8-digit hex is supported by all modern browsers, RGBA is far more intuitive for developers because specifying 0.15 is much easier to read and maintain than calculating #7C5CFF26.
5. When to Use HEX vs RGB in Production
In real-world web architecture, you will leverage both formats strategically:
- Use HEX when:
- Declaring static brand colors in design tokens, Figma styles, and documentation.
- Minimizing stylesheet file sizes (
#ffftakes 4 bytes vsrgb(255,255,255)which takes 17 bytes). - Sharing quick color codes in Slack, Jira, or code review comments.
- Use RGB when:
- Working with CSS Custom Properties where alpha channels must be dynamically applied:
background: rgba(var(--brand-rgb), 0.2);. - Writing JavaScript canvas animations, WebGL shaders, or pixel manipulation scripts.
- Gradually interpolating between two colors in interactive sliders.
- Working with CSS Custom Properties where alpha channels must be dynamically applied:
6. Frequently Asked Questions
What is 3-digit HEX shorthand?
When both digits in each RGB pair are identical, CSS allows a 3-digit shorthand. For example, #FF0000 can be shortened to #F00, and #FFFFFF simplifies to #FFF. The browser expands each single digit to two identical digits.
Can HEX and RGB represent colors outside the sRGB color space?
No. Standard HEX and RGB syntax are strictly bounded to the 24-bit sRGB color gamut. To access wider display gamuts like Display P3, you must use modern CSS color functions like color(display-p3 1 0 0) or oklch(). Learn more in our Guide to OKLCH in Web Design.
How do I test if my HEX color has accessible contrast?
Enter your HEX code and background color into our WCAG Contrast Checker. It will compute the exact mathematical contrast ratio according to WCAG 2.2 standards instantly.