A comprehensive guide to hexadecimal color notation, how it works, and its applications in web development and digital design.
Hexadecimal (HEX) color codes are a fundamental way of specifying colors in web development and digital design. This six-digit alphanumeric system provides a precise, standardized method for defining colors that works consistently across browsers, design software, and digital platforms, making it the most widely used color notation system in web development.
A HEX color code consists of a hash symbol (#) followed by six hexadecimal digits (0-9, A-F). These six digits are divided into three pairs, each representing the intensity of red, green, and blue components:
Hexadecimal uses base-16 numbering with digits 0-9 and letters A-F:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 (decimal)Each hexadecimal pair represents a value from 0 to 255 in decimal:
// Convert HEX pair to decimal
function hexToDecimal(hex) {
return parseInt(hex, 16);
}
// Convert decimal to HEX pair
function decimalToHex(decimal) {
return decimal.toString(16).padStart(2, '0').toUpperCase();
}
// Examples
console.log(hexToDecimal('FF')); // 255
console.log(hexToDecimal('80')); // 128
console.log(decimalToHex(255)); // 'FF'
console.log(decimalToHex(128)); // '80'// Convert full HEX color to RGB
function hexToRgb(hex) {
// Remove # if present
hex = hex.replace('#', '');
// Handle 3-digit shorthand
if (hex.length === 3) {
hex = hex.split('').map(char => char + char).join('');
}
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
return { r, g, b };
}
// Convert RGB to HEX
function rgbToHex(r, g, b) {
const toHex = (n) => {
const hex = Math.round(Math.max(0, Math.min(255, n))).toString(16);
return hex.length === 1 ? '0' + hex : hex;
};
return '#' + toHex(r) + toHex(g) + toHex(b);
}
// Examples
console.log(hexToRgb('#FF5733')); // {r: 255, g: 87, b: 51}
console.log(rgbToHex(255, 87, 51)); // '#FF5733'When both digits in each pair are identical, you can use shorthand notation:
// Expand 3-digit HEX to 6-digit
function expandHex(hex) {
if (hex.length === 4) { // includes #
return '#' + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
}
return hex;
}
// Examples
console.log(expandHex('#F00')); // '#FF0000'
console.log(expandHex('#ABC')); // '#AABBCC'HEX colors are widely supported in CSS and HTML:
/* CSS Color Properties */
color: #FF5733; /* Text color */
background-color: #2E86AB; /* Background */
border: 2px solid #F23557; /* Border color */
box-shadow: 0 2px 4px #00000040; /* Shadow with alpha */
/* CSS Variables */
:root {
--primary-color: #FF5733;
--secondary-color: #2E86AB;
--accent-color: #F23557;
}
.button {
background-color: var(--primary-color);
border: 1px solid var(--accent-color);
}<!-- HTML Examples -->
<div style="color: #FF5733;">Red text</div>
<span style="background: #2E86AB; color: #FFFFFF;">Blue background</span>
<p style="border-left: 4px solid #F23557;">Accent border</p>Modern browsers support 8-digit HEX codes with alpha transparency:
/* 8-digit HEX with alpha channel */
#FF573380 /* 50% transparent red-orange */
#2E86ABFF /* Fully opaque blue */
#F2355700 /* Fully transparent red */
/* CSS usage */
background-color: #FF573380;
border: 1px solid #2E86AB80;// Convert RGBA to 8-digit HEX
function rgbaToHex(r, g, b, a) {
const toHex = (n) => Math.round(n).toString(16).padStart(2, '0');
const alpha = Math.round(a * 255);
return '#' + toHex(r) + toHex(g) + toHex(b) + toHex(alpha);
}
// Convert 8-digit HEX to RGBA
function hexToRgba(hex) {
hex = hex.replace('#', '');
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
const a = hex.length === 8 ? parseInt(hex.substring(6, 8), 16) / 255 : 1;
return { r, g, b, a };
}
// Examples
console.log(rgbaToHex(255, 87, 51, 0.5)); // '#FF573380'
console.log(hexToRgba('#FF573380')); // {r: 255, g: 87, b: 51, a: 0.5}// Generate complementary color
function getComplementary(hex) {
const rgb = hexToRgb(hex);
return rgbToHex(255 - rgb.r, 255 - rgb.g, 255 - rgb.b);
}
// Generate analogous colors
function getAnalogous(hex, degrees = 30) {
const hsl = hexToHsl(hex);
const analogous1 = hslToHex((hsl.h + degrees) % 360, hsl.s, hsl.l);
const analogous2 = hslToHex((hsl.h - degrees + 360) % 360, hsl.s, hsl.l);
return [analogous1, analogous2];
}
// Generate triadic colors
function getTriadic(hex) {
const hsl = hexToHsl(hex);
const triadic1 = hslToHex((hsl.h + 120) % 360, hsl.s, hsl.l);
const triadic2 = hslToHex((hsl.h + 240) % 360, hsl.s, hsl.l);
return [triadic1, triadic2];
}// Lighten/darken HEX colors
function adjustBrightness(hex, percent) {
const rgb = hexToRgb(hex);
const factor = 1 + (percent / 100);
const r = Math.min(255, Math.max(0, Math.round(rgb.r * factor)));
const g = Math.min(255, Math.max(0, Math.round(rgb.g * factor)));
const b = Math.min(255, Math.max(0, Math.round(rgb.b * factor)));
return rgbToHex(r, g, b);
}
// Saturate/desaturate colors
function adjustSaturation(hex, percent) {
const hsl = hexToHsl(hex);
const newSaturation = Math.min(100, Math.max(0, hsl.s + percent));
return hslToHex(hsl.h, newSaturation, hsl.l);
}
// Examples
console.log(adjustBrightness('#FF5733', 20)); // Lighter version
console.log(adjustBrightness('#FF5733', -20)); // Darker version
console.log(adjustSaturation('#FF5733', -30)); // Less saturated// Calculate relative luminance
function getRelativeLuminance(hex) {
const rgb = hexToRgb(hex);
const sRGB = [rgb.r, rgb.g, rgb.b].map(c => {
c = c / 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
});
return 0.2126 * sRGB[0] + 0.7152 * sRGB[1] + 0.0722 * sRGB[2];
}
// Calculate contrast ratio between two colors
function getContrastRatio(hex1, hex2) {
const lum1 = getRelativeLuminance(hex1);
const lum2 = getRelativeLuminance(hex2);
const lighter = Math.max(lum1, lum2);
const darker = Math.min(lum1, lum2);
return (lighter + 0.05) / (darker + 0.05);
}
// Check WCAG compliance
function checkWCAGCompliance(foreground, background) {
const ratio = getContrastRatio(foreground, background);
return {
ratio: ratio,
AA: ratio >= 4.5,
AAA: ratio >= 7,
AALarge: ratio >= 3,
AAALarge: ratio >= 4.5
};
}
// Example
const compliance = checkWCAGCompliance('#000000', '#FFFFFF');
console.log(compliance); // {ratio: 21, AA: true, AAA: true, ...}// Generate monochromatic palette
function generateMonochromatic(hex, steps = 5) {
const hsl = hexToHsl(hex);
const palette = [];
for (let i = 0; i < steps; i++) {
const lightness = (100 / (steps - 1)) * i;
palette.push(hslToHex(hsl.h, hsl.s, lightness));
}
return palette;
}
// Generate split-complementary palette
function generateSplitComplementary(hex) {
const hsl = hexToHsl(hex);
const comp1 = hslToHex((hsl.h + 150) % 360, hsl.s, hsl.l);
const comp2 = hslToHex((hsl.h + 210) % 360, hsl.s, hsl.l);
return [hex, comp1, comp2];
}
// Generate tetradic palette
function generateTetradic(hex) {
const hsl = hexToHsl(hex);
const colors = [];
for (let i = 0; i < 4; i++) {
const hue = (hsl.h + (i * 90)) % 360;
colors.push(hslToHex(hue, hsl.s, hsl.l));
}
return colors;
}HEX colors have universal browser support:
// Efficient HEX validation
function isValidHex(hex) {
return /^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/.test(hex);
}
// Batch color processing
function processColorBatch(hexColors) {
return hexColors.map(hex => ({
original: hex,
rgb: hexToRgb(hex),
hsl: hexToHsl(hex),
luminance: getRelativeLuminance(hex)
}));
}
// Memoized color calculations
const colorCache = new Map();
function memoizedHexToRgb(hex) {
if (colorCache.has(hex)) {
return colorCache.get(hex);
}
const rgb = hexToRgb(hex);
colorCache.set(hex, rgb);
return rgb;
}/* Design system color tokens */
:root {
/* Primary colors */
--color-primary-50: #FFF7ED;
--color-primary-100: #FFEDD5;
--color-primary-500: #F97316;
--color-primary-900: #9A3412;
/* Semantic colors */
--color-success: #10B981;
--color-warning: #F59E0B;
--color-error: #EF4444;
--color-info: #3B82F6;
/* Neutral colors */
--color-gray-50: #F9FAFB;
--color-gray-100: #F3F4F6;
--color-gray-500: #6B7280;
--color-gray-900: #111827;
}
/* Usage in components */
.button {
background-color: var(--color-primary-500);
color: var(--color-gray-50);
}
.alert-error {
background-color: var(--color-error);
border-color: var(--color-error);
}// Design system color manager
class ColorSystem {
constructor() {
this.colors = {
primary: {
50: '#FFF7ED',
100: '#FFEDD5',
500: '#F97316',
900: '#9A3412'
},
semantic: {
success: '#10B981',
warning: '#F59E0B',
error: '#EF4444',
info: '#3B82F6'
}
};
}
getColor(path) {
return path.split('.').reduce((obj, key) => obj?.[key], this.colors);
}
generateShades(baseHex, steps = 9) {
const hsl = hexToHsl(baseHex);
const shades = {};
for (let i = 0; i < steps; i++) {
const lightness = 95 - (i * 10);
const shade = (i + 1) * 100;
shades[shade] = hslToHex(hsl.h, hsl.s, lightness);
}
return shades;
}
validateAccessibility(foreground, background) {
return checkWCAGCompliance(foreground, background);
}
}/* Brand color system */
:root {
/* Brand primary */
--brand-primary: #FF5733;
--brand-primary-light: #FF8A65;
--brand-primary-dark: #D84315;
/* Brand secondary */
--brand-secondary: #2E86AB;
--brand-secondary-light: #42A5F5;
--brand-secondary-dark: #1565C0;
/* Brand accent */
--brand-accent: #F23557;
--brand-accent-light: #FF6B8A;
--brand-accent-dark: #C62828;
}
/* Component usage */
.header {
background: linear-gradient(135deg, var(--brand-primary), var(--brand-secondary));
}
.cta-button {
background-color: var(--brand-accent);
border: 2px solid var(--brand-accent-dark);
}
.cta-button:hover {
background-color: var(--brand-accent-dark);
}/* Light mode colors */
:root {
--bg-primary: #FFFFFF;
--bg-secondary: #F8F9FA;
--text-primary: #212529;
--text-secondary: #6C757D;
--border-color: #DEE2E6;
}
/* Dark mode colors */
[data-theme="dark"] {
--bg-primary: #1A1A1A;
--bg-secondary: #2D2D2D;
--text-primary: #FFFFFF;
--text-secondary: #B0B0B0;
--border-color: #404040;
}
/* Automatic dark mode */
@media (prefers-color-scheme: dark) {
:root {
--bg-primary: #1A1A1A;
--bg-secondary: #2D2D2D;
--text-primary: #FFFFFF;
--text-secondary: #B0B0B0;
}
}HEX color codes remain the cornerstone of web color specification, offering precision, compatibility, and ease of use. Understanding their structure, conversion methods, and practical applications is essential for modern web development and digital design. As web technologies evolve, HEX colors continue to provide a reliable foundation for color management across all digital platforms.
Explore these comprehensive pillar articles related to HEX Color Codes
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
Comprehensive guide to triadic color schemes - three colors evenly spaced around the color wheel that create balanced, vibrant, and dynamic visual compositions with mathematical precision and artistic appeal.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
Discover more about HEX Color Codes and related color theory concepts
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
Comprehensive guide to triadic color schemes - three colors evenly spaced around the color wheel that create balanced, vibrant, and dynamic visual compositions with mathematical precision and artistic appeal.
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
A comprehensive guide to split-complementary color schemes - sophisticated three-color harmonies that combine a base color with two colors adjacent to its complement, creating balanced visual contrast with reduced tension compared to direct complementary pairs, essential for refined design compositions, brand identity systems, and advanced color theory applications.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
Discover more color terminology and concepts in our comprehensive glossary.
Browse Full Glossary