HEX vs RGB Color Codes Explained
Last updated 2026-09-12
HEX and RGB are two ways of writing down the exact same color information — one is more compact, the other more human-readable for each color channel.
Read a HEX code
A HEX code like #2851FF has three pairs of hexadecimal digits: 28 (red), 51 (green), FF (blue) — each pair ranges from 00 to FF (0-255 in decimal).
Read an RGB code
RGB writes the same three values in decimal: rgb(40, 81, 255) — easier to read individual channel values at a glance than hex.
Convert between them
Convert each hex pair from base-16 to base-10 to get RGB, or convert each RGB decimal value (0-255) to a 2-digit hex pair to get HEX.
Example
#2851FF and rgb(40, 81, 255) describe the exact same shade of blue.
Important Considerations
- Shorthand HEX codes like #f00 expand to #ff0000 — each digit is duplicated.
- Neither format includes transparency by default — HEX needs an extra pair (#2851FFAA) and RGB needs a fourth value (rgba(40,81,255,0.67)) to represent opacity.
- CSS accepts both formats interchangeably — the choice is usually just a matter of which is easier to read or edit for the task at hand.
Frequently Asked Questions
- Which format should I use in my code?
- Either works identically in CSS — HEX is more compact, RGB is easier to read and adjust individual channels quickly. Many developers use HEX for static colors and RGB/RGBA when transparency matters.
- Is there a third common format?
- Yes — HSL (hue, saturation, lightness) is popular for design work because it's more intuitive for creating color variations (lighter/darker/more saturated) than either HEX or RGB.