Need to Convert, Inspect, or Test Hex Codes Live?
Use our free, client-side Hex Color Converter: real-time RGB, HSL, OKLCH, 8-digit alpha opacity slider, and WCAG 2.1 contrast checking.
The Fundamentals of Hexadecimal Notation
Computers do not think in base-10 decimals or human alphabets. They process voltage differentials in binary ones and zeros. Reading raw binary strings like 1101101011111110 quickly overwhelms human comprehension. Base-16 hexadecimal notation (hex) solves this cognitive bottleneck by compressing four binary bits (one nibble) into a single alphanumeric glyph.
Hexadecimal uses sixteen distinct symbols: digits 0 through 9 represent values zero to nine, and letters A through F represent values ten through fifteen. Because exactly 24 = 16 states exist in four bits, two hexadecimal characters map cleanly to one standard 8-bit byte (28 = 256 states, spanning 0x00 to 0xFF). This direct correspondence makes hex notation foundational across memory addressing, packet sniffing, cryptography, character encoding, and web color systems.
Table 1: The 4-Bit Nibble Lookup Matrix
Every single byte consists of two 4-bit nibbles: a high nibble and a low nibble. Below is the fundamental translation matrix connecting decimal integers, hexadecimal characters, and their raw 4-bit binary equivalents:
| Decimal Value | Hexadecimal Character | Binary Nibble (4-bit) | Bit Weight (8-4-2-1) |
|---|---|---|---|
| 0 | 0 | 0000 | 0 + 0 + 0 + 0 |
| 1 | 1 | 0001 | 0 + 0 + 0 + 1 |
| 2 | 2 | 0010 | 0 + 0 + 2 + 0 |
| 3 | 3 | 0011 | 0 + 0 + 2 + 1 |
| 4 | 4 | 0100 | 0 + 4 + 0 + 0 |
| 5 | 5 | 0101 | 0 + 4 + 0 + 1 |
| 6 | 6 | 0110 | 0 + 4 + 2 + 0 |
| 7 | 7 | 0111 | 0 + 4 + 2 + 1 |
| 8 | 8 | 1000 | 8 + 0 + 0 + 0 |
| 9 | 9 | 1001 | 8 + 0 + 0 + 1 |
| 10 | A | 1010 | 8 + 0 + 2 + 0 |
| 11 | B | 1011 | 8 + 0 + 2 + 1 |
| 12 | C | 1100 | 8 + 4 + 0 + 0 |
| 13 | D | 1101 | 8 + 4 + 0 + 1 |
| 14 | E | 1110 | 8 + 4 + 2 + 0 |
| 15 | F | 1111 | 8 + 4 + 2 + 1 |
Table 2: Hexadecimal Byte Mapping (0 to 255)
In computer architecture and network protocols, data is addressed in bytes. A single byte ranges from decimal 0 (0x00) to 255 (0xFF). Here is how decimal increments translate across common engineering byte thresholds:
| Decimal Byte | Hex Notation | 8-Bit Binary String | System Architecture Context |
|---|---|---|---|
| 0 | 0x00 | 00000000 | Null byte / String terminator (\0) |
| 16 | 0x10 | 00010000 | First two-digit decimal milestone |
| 32 | 0x20 | 00100000 | ASCII Space character |
| 64 | 0x40 | 01000000 | ASCII '@' symbol / 64-byte memory boundary |
| 127 | 0x7F | 01111111 | Maximum positive signed 8-bit integer |
| 128 | 0x80 | 10000000 | High bit set / Extended ASCII entry |
| 192 | 0xC0 | 11000000 | Common subnet octet (192.168.x.x) |
| 255 | 0xFF | 11111111 | Maximum unsigned 8-bit byte / Color channel full saturation |
Table 3: Printable ASCII Character to Hex Lookup
The American Standard Code for Information Interchange (ASCII) maps numerical byte values to text glyphs. Programmers inspecting hex dumps or debugging network protocols rely on this exact byte-to-glyph mapping:
| Hex Byte | Decimal | Glyph | Description | Hex Byte | Decimal | Glyph | Description |
|---|---|---|---|---|---|---|---|
0x20 | 32 | [SP] | Space | 0x41 | 65 | A | Uppercase Latin A |
0x21 | 33 | ! | Exclamation point | 0x42 | 66 | B | Uppercase Latin B |
0x22 | 34 | " | Double quotation | 0x43 | 67 | C | Uppercase Latin C |
0x23 | 35 | # | Number sign (Hash) | 0x4D | 77 | M | Uppercase Latin M |
0x24 | 36 | $ | Dollar sign | 0x5A | 90 | Z | Uppercase Latin Z |
0x28 | 40 | ( | Left parenthesis | 0x61 | 97 | a | Lowercase Latin a |
0x29 | 41 | ) | Right parenthesis | 0x62 | 98 | b | Lowercase Latin b |
0x2A | 42 | * | Asterisk | 0x63 | 99 | c | Lowercase Latin c |
0x2B | 43 | + | Plus sign | 0x7A | 122 | z | Lowercase Latin z |
0x2D | 45 | - | Hyphen / Minus | 0x7B | 123 | { | Left curly brace |
0x2F | 47 | / | Forward slash | 0x7D | 125 | } | Right curly brace |
0x30 | 48 | 0 | Digit zero | 0x7E | 126 | ~ | Tilde |
0x39 | 57 | 9 | Digit nine | 0x0A | 10 | \n | Line Feed |
0x3D | 61 | = | Equals sign | 0x0D | 13 | \r | Carriage Return |
Table 4: Hex Opacity & Alpha Transparency Matrix (0% to 100%)
Modern CSS Color Module Level 4 supports 8-digit hexadecimal colors (#RRGGBBAA), where the final two characters specify alpha channel opacity. In 8-digit notation, 00 corresponds to 0% opacity (completely transparent) and FF corresponds to 100% opacity (fully opaque).
The mathematical conversion from an integer percentage P (0 to 100) to its 2-digit hexadecimal suffix is:
Alpha_Hex = round( (P / 100) * 255 ).toString(16).padStart(2, '0').toUpperCase()
Below is the reference cheat sheet for standard opacity steps, illustrated using brand blue (#2563EB):
| Opacity Percentage | Alpha Hex Suffix (AA) | Decimal Value (0-255) | 8-Digit CSS Example | CSS RGBA Equivalent |
|---|---|---|---|---|
| 100% | FF | 255 | #2563EBFF | rgba(37, 99, 235, 1.0) |
| 95% | F2 | 242 | #2563EBF2 | rgba(37, 99, 235, 0.95) |
| 90% | E6 | 230 | #2563EBE6 | rgba(37, 99, 235, 0.90) |
| 85% | D9 | 217 | #2563EBD9 | rgba(37, 99, 235, 0.85) |
| 80% | CC | 204 | #2563EBCC | rgba(37, 99, 235, 0.80) |
| 75% | BF | 191 | #2563EBBF | rgba(37, 99, 235, 0.75) |
| 70% | B3 | 179 | #2563EBB3 | rgba(37, 99, 235, 0.70) |
| 65% | A6 | 166 | #2563EBA6 | rgba(37, 99, 235, 0.65) |
| 60% | 99 | 153 | #2563EB99 | rgba(37, 99, 235, 0.60) |
| 55% | 8C | 140 | #2563EB8C | rgba(37, 99, 235, 0.55) |
| 50% | 80 | 128 | #2563EB80 | rgba(37, 99, 235, 0.50) |
| 45% | 73 | 115 | #2563EB73 | rgba(37, 99, 235, 0.45) |
| 40% | 66 | 102 | #2563EB66 | rgba(37, 99, 235, 0.40) |
| 35% | 59 | 89 | #2563EB59 | rgba(37, 99, 235, 0.35) |
| 30% | 4D | 77 | #2563EB4D | rgba(37, 99, 235, 0.30) |
| 25% | 40 | 64 | #2563EB40 | rgba(37, 99, 235, 0.25) |
| 20% | 33 | 51 | #2563EB33 | rgba(37, 99, 235, 0.20) |
| 15% | 26 | 38 | #2563EB26 | rgba(37, 99, 235, 0.15) |
| 10% | 1A | 26 | #2563EB1A | rgba(37, 99, 235, 0.10) |
| 5% | 0D | 13 | #2563EB0D | rgba(37, 99, 235, 0.05) |
| 0% | 00 | 0 | #2563EB00 | rgba(37, 99, 235, 0.0) |
In standard web CSS, the alpha channel is placed at the end:
#RRGGBBAA. In contrast, native Android XML color resources historically placed the alpha channel at the front: #AARRGGBB. A developer copying #802563EB into CSS will render an opaque color with a dark red channel, rather than a 50% translucent blue. Always verify the byte order specification of your runtime platform.
The 3-Digit and 4-Digit Shorthand Duplication Rules
CSS allows shorthand 3-digit hex notation (#RGB) and 4-digit notation with alpha (#RGBA). A widespread misconception among beginners is that shorthand appends trailing zeros (for example, assuming #F00 becomes #F00000). In reality, the CSS specification dictates that each character duplicates into a pair:
#RGB ──► #RRGGBB (e.g., #FA0 ──► #FFAA00)
#RGBA ──► #RRGGBBAA (e.g., #38CF ──► #3388CCFF)
Because each character is cloned, shorthand can only represent colors where both nibbles of every channel are identical. For example, #123456 cannot be compressed into shorthand because 1 != 2, 3 != 4, and 5 != 6. Out of 16,777,216 standard TrueColor values, exactly 4,096 colors (163) can be expressed in 3-digit shorthand.
Table 5: Standard CSS Named Colors & The 'DarkGray' Paradox
The W3C CSS specification standardizes 140 named color keywords originating from the X11 Unix windowing system and HTML 3.2. Below is an engineering reference for popular named colors across primary hues:
| Color Name | Hex Notation | RGB Value | Perceived Luminance | Color Family |
|---|---|---|---|---|
crimson | #DC143C | rgb(220, 20, 60) | 0.160 | Red |
red | #FF0000 | rgb(255, 0, 0) | 0.213 | Red |
coral | #FF7F50 | rgb(255, 127, 80) | 0.370 | Orange |
darkorange | #FF8C00 | rgb(255, 140, 0) | 0.395 | Orange |
gold | #FFD700 | rgb(255, 215, 0) | 0.699 | Yellow |
yellow | #FFFF00 | rgb(255, 255, 0) | 0.928 | Yellow |
lime | #00FF00 | rgb(0, 255, 0) | 0.715 | Green |
forestgreen | #228B22 | rgb(34, 139, 34) | 0.189 | Green |
cyan / aqua | #00FFFF | rgb(0, 255, 255) | 0.787 | Cyan |
dodgerblue | #1E90FF | rgb(30, 144, 255) | 0.274 | Blue |
blue | #0000FF | rgb(0, 0, 255) | 0.072 | Blue |
rebeccapurple | #663399 | rgb(102, 51, 153) | 0.076 | Purple |
magenta / fuchsia | #FF00FF | rgb(255, 0, 255) | 0.285 | Purple |
silver | #C0C0C0 | rgb(192, 192, 192) | 0.527 | Gray |
darkgray | #A9A9A9 | rgb(169, 169, 169) | 0.396 | Gray |
gray | #808080 | rgb(128, 128, 128) | 0.216 | Gray |
darkslategray | #2F4F4F | rgb(47, 79, 79) | 0.067 | Gray |
black | #000000 | rgb(0, 0, 0) | 0.000 | Neutral |
The Historical 'DarkGray' Paradox: Notice in the table above that darkgray (#A9A9A9, luminance 0.396) is objectively lighter than standard gray (#808080, luminance 0.216). This occurred when the W3C combined HTML 3.2 with the X11 color registry. HTML 3.2 had specified gray as exact middle 50% gray (#808080). X11 had independently defined darkgray as #A9A9A9 (roughly 66% gray). When both catalogs merged into modern CSS, darkgray was preserved as lighter than gray.
Bitwise Manipulation: Unpacking Hex Colors Across Languages
In low-level graphics engines, canvas pipelines, and high-performance game loops, colors are packed into single 32-bit integers. Software engineers unpack individual 8-bit color channels using bitwise right shifts (>>) and bitwise AND masks (& 0xFF):
1. TypeScript / JavaScript
export interface RGBColor {
r: number;
g: number;
b: number;
}
export function unpackHex(hexString: string): RGBColor {
const cleanHex = hexString.replace(/^#/, '');
const colorInt = parseInt(cleanHex, 16);
if (isNaN(colorInt)) {
throw new TypeError(`Invalid hexadecimal color format: ${hexString}`);
}
// Bitwise masking and shifting operations
const r = (colorInt >> 16) & 0xff; // Shift 16 bits, mask low byte
const g = (colorInt >> 8) & 0xff; // Shift 8 bits, mask low byte
const b = colorInt & 0xff; // Mask low byte directly
return { r, g, b };
}
2. Rust
#[derive(Debug, PartialEq, Eq)]
pub struct Rgb {
pub r: u8,
pub g: u8,
pub b: u8,
}
pub fn unpack_hex(hex: u32) -> Rgb {
Rgb {
r: ((hex >> 16) & 0xFF) as u8,
g: ((hex >> 8) & 0xFF) as u8,
b: (hex & 0xFF) as u8,
}
}
3. C / C++
#include <stdint.h>
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} RGBColor;
RGBColor unpack_hex(uint32_t colorInt) {
RGBColor c;
c.r = (colorInt >> 16) & 0xFF;
c.g = (colorInt >> 8) & 0xFF;
c.b = colorInt & 0xFF;
return c;
}
Color Science: From CRT Monitors to Relative Luminance and OKLCH
For three decades, web styling relied on hexadecimal color notations: #ffffff, #000000, and #3b82f6. Hex notation represented red, green, and blue photon intensities as three pairs of base-16 integers (00 to FF). It mirrored how 1990s cathode-ray tube (CRT) displays mapped electron guns to physical phosphors.
In modern web engineering, hex codes introduce significant architectural constraints. They confine rendering engines to the narrow, legacy sRGB color space, discarding roughly 30% of the richer saturation visible on Display P3 screens, iPhone OLED displays, and MacBook liquid retina panels.
Furthermore, neither hex nor HSL possesses perceptual uniformity. In HSL, pure blue (hsl(240, 100%, 50%)) and pure yellow (hsl(60, 100%, 50%)) share an identical lightness value of 50%. Human vision registers yellow as bright and blue as dark. CSS Color Module Level 4 resolved this by introducing OKLCH.
The Modern Color Space Comparison
| Color Format | CSS Syntax Example | Supported Gamut | Perceptually Uniform? | Recommended Engineering Use |
|---|---|---|---|---|
| Hexadecimal | #3b82f6 |
sRGB only | No | Static legacy fallbacks, build configs |
| RGB / RGBA | rgb(59 130 246 / 0.8) |
sRGB only | No | Canvas 2D pixel manipulation |
| HSL | hsl(217 91% 60%) |
sRGB only | No (Distorts luminance) | Legacy CSS preprocessors |
| OKLCH | oklch(0.62 0.19 250) |
Wide Gamut (P3, Rec.2020) | Yes (Calibrated to human vision) | Design systems, dark modes, token scales |
The Architecture of OKLCH
OKLCH models light through mathematical coordinates based directly on human retinal response:
oklch( Lightness Chroma Hue )
│ │ │
│ │ └── Hue Angle (0 to 360 degrees)
│ │ 0 = Red, 90 = Yellow, 140 = Green, 240 = Blue
│ │
│ └── Chroma / Saturation (0.0 to ~0.37+)
│ 0 = Pure Grayscale, 0.3+ = Ultra-vivid P3 Display Color
│
└── Perceived Lightness (0% = Pure Black, 100% = Pure White)
Identical number = Identical perceived brightness across ALL hues!
Because lightness is perceptually calibrated, oklch(0.7 0.15 60) (yellow) and oklch(0.7 0.15 250) (blue) have identical perceived luminance. If dark text satisfies contrast ratios on the yellow variant, it is mathematically guaranteed to satisfy contrast ratios on the blue variant.
Automated Palette Generation in TypeScript
Here is an automated palette generator that converts a single brand hue into an accessible, 11-step CSS variable scale:
// src/styles/theme-engine.ts
export interface PaletteStep {
token: string;
lightness: number;
chroma: number;
}
const PALETTE_CURVE: PaletteStep[] = [
{ token: '50', lightness: 0.97, chroma: 0.02 },
{ token: '100', lightness: 0.93, chroma: 0.04 },
{ token: '200', lightness: 0.86, chroma: 0.08 },
{ token: '300', lightness: 0.77, chroma: 0.12 },
{ token: '400', lightness: 0.68, chroma: 0.16 },
{ token: '500', lightness: 0.58, chroma: 0.20 }, // Primary Brand Shade
{ token: '600', lightness: 0.50, chroma: 0.18 },
{ token: '700', lightness: 0.42, chroma: 0.15 },
{ token: '800', lightness: 0.33, chroma: 0.12 },
{ token: '900', lightness: 0.24, chroma: 0.08 },
{ token: '950', lightness: 0.15, chroma: 0.04 },
];
export function buildOklchScale(hueDegrees: number, prefix = 'brand'): Record {
const tokens: Record = {};
for (const step of PALETTE_CURVE) {
tokens[`--color-${prefix}-${step.token}`] = `oklch(${step.lightness} ${step.chroma} ${hueDegrees})`;
}
return tokens;
}
Progressive Enhancement in Modern CSS
Deploy wide-gamut OKLCH colors while providing reliable sRGB hex fallbacks for older browsers:
/* styles/buttons.css */
:root {
/* sRGB fallback */
--accent: #2563eb;
}
@supports (color: oklch(0.6 0.2 250)) {
:root {
/* Wide-gamut P3 color on modern displays */
--accent: oklch(0.58 0.22 255);
}
}
.btn-primary {
background-color: var(--accent);
color: #ffffff;
padding: 0.75rem 1.5rem;
border-radius: 6px;
font-weight: 600;
}
Looking for Structured Frontend Training?
Take your frontend engineering skills to the next level with hands-on projects, component architecture, and production best practices. Build real-world applications with our free structured courses:
Engineering Conclusion
Hexadecimal notation remains the universal bridge between raw binary machine data and human developers. Whether you are parsing network packets, executing bitwise bit shifts, or defining modern wide-gamut CSS variables, understanding hex fundamentals is an irreplaceable computer science skill.
