Hex to RGB Color Conversion: Complete Guide
If you've ever worked with web design, CSS, or graphic software, you've encountered hex color codes โ those cryptic strings like #FF5733 or #3498DB. But what do they actually mean, and how do they relate to RGB values? Understanding this conversion is fundamental to working with digital color, whether you're building a website, designing a logo, or choosing a paint shade on screen.
This comprehensive guide covers everything you need to know about hex and RGB colors: how both systems work, the math behind conversion, a handy reference table of popular colors, CSS best practices, and the tools that make conversion effortless.
Understanding the RGB Color Model
The RGB color model is an additive color system used in screens, monitors, and digital displays. Every color you see on your screen is created by mixing three primary colors of light:
- Red (R) โ intensity from 0 to 255
- Green (G) โ intensity from 0 to 255
- Blue (B) โ intensity from 0 to 255
When all three channels are at 0, you get black (no light). When all three are at 255, you get white (full light). This gives us 256 ร 256 ร 256 = 16,777,216 possible colors.
The term "additive" means the colors add together โ unlike paint (subtractive mixing), where combining colors makes things darker, light mixing makes things brighter. This is why your monitor can produce such vivid, luminous colors.
How Hex Color Codes Work
A hexadecimal color code is simply a compact way of writing an RGB value. Instead of three separate decimal numbers, hex combines them into a single six-character string preceded by a hash (#).
The format is #RRGGBB, where:
- RR = the red channel in hexadecimal (00 to FF)
- GG = the green channel in hexadecimal (00 to FF)
- BB = the blue channel in hexadecimal (00 to FF)
Hexadecimal (base 16) uses digits 0โ9 and letters AโF. So FF in hex = 255 in decimal, 00 = 0, and 80 = 128. Each two-digit hex pair maps to one RGB channel value.
Why Hexadecimal?
Hex is used because it's more compact than writing three separate numbers. Compare: #FF5733 versus rgb(255, 87, 51). The hex version is shorter, has no spaces or parentheses, and is easy to copy-paste. It became the default in HTML and CSS for this reason.
The Conversion Math: Hex to RGB
Converting hex to RGB is straightforward once you understand hexadecimal:
Steps to convert #FF5733 to RGB:
- Split into pairs: FF, 57, 33
- Convert each pair to decimal:
- FF โ (15 ร 16) + 15 = 240 + 15 = 255
- 57 โ (5 ร 16) + 7 = 80 + 7 = 87
- 33 โ (3 ร 16) + 3 = 48 + 3 = 51
- Result: rgb(255, 87, 51)
The formula for each hex pair is: Decimal = (first digit ร 16) + second digit
Where hex digits map as: 0=0, 1=1, ... 9=9, A=10, B=11, C=12, D=13, E=14, F=15.
RGB to Hex (Reverse Conversion)
To go the other way, divide each RGB value by 16. The quotient is the first hex digit, the remainder is the second.
Example: RGB(52, 152, 219)
- 52 รท 16 = 3 remainder 4 โ 34
- 152 รท 16 = 9 remainder 8 โ 98
- 219 รท 16 = 13 remainder 11 โ DB
Result: #3498DB
Skip the math entirely with our Hex to RGB Converter.
Common Hex Color Codes Reference Table
| Color | Hex | RGB | Preview |
|---|---|---|---|
| Red | #FF0000 | 255, 0, 0 | |
| Green | #00FF00 | 0, 255, 0 | |
| Blue | #0000FF | 0, 0, 255 | |
| White | #FFFFFF | 255, 255, 255 | |
| Black | #000000 | 0, 0, 0 | |
| Coral | #FF7F50 | 255, 127, 80 | |
| Dodger Blue | #1E90FF | 30, 144, 255 | |
| Gold | #FFD700 | 255, 215, 0 | |
| Slate Gray | #708090 | 112, 128, 144 | |
| Medium Purple | #9370DB | 147, 112, 219 |
Using Hex and RGB Colors in CSS
CSS supports multiple color formats. Here's how to use them effectively:
Hex Notation
color: #FF5733;
background-color: #3498DB;
border: 2px solid #2ECC71;
3-Digit Hex Shorthand
When both digits in each pair are the same, you can use shorthand:
#FF0000 โ #F00
#336699 โ #369
#FFFFFF โ #FFF
RGB and RGBA Functions
color: rgb(255, 87, 51);
background: rgba(52, 152, 219, 0.5);
border: 1px solid rgb(46, 204, 113);
8-Digit Hex (with Alpha)
Modern browsers support 8-digit hex codes where the last two digits specify opacity:
#FF573380 /* 50% opacity */
#3498DBCC /* 80% opacity */
#00000033 /* ~20% black overlay */
When to Use Hex vs RGB
- Use hex for simple, solid colors โ it's shorter and widely recognized
- Use rgb()/rgba() when you need transparency or dynamic JavaScript values
- Use CSS named colors (like
coralordodgerblue) for prototyping โ there are 147 named colors
Hex to Decimal: The Foundation of Color Conversion
At its core, hex-to-RGB conversion is just hex-to-decimal conversion applied three times. If you understand how to convert any hexadecimal number to decimal, you can decode any color code.
The hexadecimal system uses 16 symbols: 0โ9 represent values zero through nine, and AโF represent ten through fifteen. Each position represents a power of 16, just as decimal positions represent powers of 10.
For a two-digit hex number: Value = (digit1 ร 16) + digit2
This is the same math used in our Hex to Decimal Converter, which handles any hexadecimal number โ not just color codes.
Tools for Hex to RGB Conversion
While manual conversion is educational, in practice you'll want a tool. Here's how popular options compare:
- ConvKit Hex to RGB โ instant conversion with live preview, CSS output, and color picker. No installation needed. Try it free
- Browser DevTools โ click any color in the Styles panel to toggle between hex, RGB, and HSL
- Figma/Sketch โ built-in color pickers show both formats simultaneously
- VS Code โ hover over any color in CSS to see a color picker with format conversion
- Command line โ
printf "%d %d %d\n" 0xFF 0x57 0x33gives RGB values instantly
Beyond RGB: Other Color Models
While hex and RGB dominate web development, other color models exist for different purposes:
- HSL (Hue, Saturation, Lightness) โ more intuitive for designers; CSS supports
hsl() - HSV/HSB โ common in design tools like Photoshop
- CMYK โ used in print design (subtractive color model)
- LAB/LCH โ perceptually uniform; CSS Color Level 4 introduces
lab()andlch()
Frequently Asked Questions
What is a hex color code?
A hex color code is a six-digit hexadecimal number preceded by a hash (#) that represents a color in the RGB color model. Each pair of digits represents the intensity of red, green, or blue, ranging from 00 (0) to FF (255). For example, #FF0000 is pure red.
How do I convert hex to RGB manually?
Split the six-digit hex code into three pairs (RR, GG, BB). Convert each pair from hexadecimal to decimal by multiplying the first digit by 16 and adding the second digit. For example, #FF5733 becomes R=255, G=87, B=51.
What is the difference between hex and RGB?
Both represent the same colors using the same underlying model. Hex uses a compact six-character hexadecimal string (#RRGGBB), while RGB uses three decimal numbers from 0โ255. They are completely interchangeable and represent the same 16.7 million possible colors.
Can I use hex colors in CSS?
Yes. CSS supports both hex (#FF5733) and RGB (rgb(255, 87, 51)) notation. You can also use 3-digit shorthand (#F00 for #FF0000) and 8-digit hex with alpha (#FF573380 for 50% opacity).
What does hex to decimal conversion have to do with colors?
Hex to decimal conversion is the mathematical foundation behind converting hex color codes to RGB values. Each two-character hex pair is converted to a decimal number (0โ255) to get the RGB equivalent. It's the same operation applied three times โ once for red, green, and blue.