HEX to RGB Guide
Understand exactly how HEX color codes convert to RGB values, with worked examples and common pitfalls explained.
What Is HEX?
A HEX color code is a six-digit hexadecimal number prefixed with #. Each pair of digits represents one of the three RGB channels:
- First pair: Red (00–FF)
- Second pair: Green (00–FF)
- Third pair: Blue (00–FF)
Hexadecimal means base-16. The digits are 0–9 plus A–F, where A=10, B=11, C=12, D=13, E=14, F=15.
What Is RGB?
RGB is a color model where each color is described by how much red, green and blue light it contains. Each channel has 256 possible values (0–255), producing 16,777,216 total colors.
The Conversion Formula
To convert HEX to RGB:
- Strip the leading
# - If the code is 3 digits, expand each digit by doubling it:
#0AFbecomes#00AAFF - Split into three 2-digit pairs
- Convert each pair from base-16 to base-10
The Math for One Pair
Each 2-digit hex value has the form XY where X and Y are hex digits. The decimal value is:
value = (X × 16) + Y
For example, the pair EF:
E = 14, F = 15
value = (14 × 16) + 15
= 224 + 15
= 239
Worked Example: #155EEF
Let's convert #155EEF step by step:
HEX: #155EEF
Red pair: "15"
1 × 16 = 16
16 + 5 = 21
Red = 21
Green pair: "5E"
5 × 16 = 80
80 + 14 (E) = 94
Green = 94
Blue pair: "EF"
14 (E) × 16 = 224
224 + 15 (F) = 239
Blue = 239
Result: rgb(21, 94, 239)
More Examples
#FFFFFF (White)
Red: FF = 15 × 16 + 15 = 255
Green: FF = 15 × 16 + 15 = 255
Blue: FF = 15 × 16 + 15 = 255
Result: rgb(255, 255, 255)
#000000 (Black)
Red: 00 = 0
Green: 00 = 0
Blue: 00 = 0
Result: rgb(0, 0, 0)
#FF7F50 (Coral)
Red: FF = 255
Green: 7F = 7 × 16 + 15 = 127
Blue: 50 = 5 × 16 + 0 = 80
Result: rgb(255, 127, 80)
3-Digit HEX Shortcuts
CSS allows a 3-digit HEX shortcut where each digit is doubled. For example:
#F00=#FF0000= red#0F0=#00FF00= green#0AF=#00AAFF= cyan-blue#FFF=#FFFFFF= white
The shortcut only works when each pair of digits is identical. You cannot shorten #155EEF to #15E.
4-Digit and 8-Digit HEX (Alpha)
CSS also supports 4-digit and 8-digit HEX codes where the last digit or last two digits represent alpha (transparency):
#155EEF— fully opaque (alpha = FF implicitly)#155EEFCC— 80% opacity#155EEF80— 50% opacity#15EC— 4-digit shorthand, equivalent to#1155EEwith alpha CC
OmniColor's converter shows the alpha value when present.
Common Mistakes
Forgetting the #
CSS allows HEX without the leading # in some contexts, but most parsers require it. Always include the #.
Using wrong digit count
A HEX code must be 3, 4, 6 or 8 digits. Codes like #155EE (5 digits) are invalid.
Assuming 3-digit works everywhere
Some tools (older image editors, certain APIs) do not expand 3-digit HEX. Use 6-digit form to be safe.
Confusing HEX with web-safe
Web-safe colors are a legacy concept from 8-bit displays. Modern screens show all 16.7 million colors, so web-safe is irrelevant today.
When to Use HEX vs RGB
Use HEX for
- CSS stylesheets
- Design tool exports
- Brand guidelines
- Email templates
- Documentation and specs
Use RGB for
- JavaScript canvas drawing
- WebGL and Three.js
- Animation and interpolation
- Dynamic color manipulation
- APIs that expect channel integers