Back to Blog
Guidescolordesigncss

Color Theory for Developers: RGB, HSL, and Harmony

RGB, hex, and HSL color models, harmonious palettes, dark mode, and accessible contrast, with practical CSS examples throughout.

LoopalooPublished August 10, 20258 min read

A little color theory goes a long way when you're building interfaces that look good and stay readable for everyone. This guide covers the color models you'll use daily, how to build harmonious palettes, and the CSS patterns that put both to work.

Color Models Explained

RGB: The Language of Screens

RGB (Red, Green, Blue) is an additive color model: mixing all three at full intensity creates white. This mirrors how screens work: tiny red, green, and blue sub-pixels combine to produce every color you see.

/* RGB syntax: rgb(red, green, blue), each value 0-255 */
color: rgb(255, 0, 0);       /* Pure red */
color: rgb(0, 255, 0);       /* Pure green */
color: rgb(0, 0, 255);       /* Pure blue */
color: rgb(255, 255, 255);   /* White (all colors) */
color: rgb(0, 0, 0);         /* Black (no light) */
color: rgb(128, 128, 128);   /* Gray (equal parts) */

/* With alpha transparency */
color: rgba(255, 0, 0, 0.5); /* 50% transparent red */

When to use RGB: When you need precise control over individual color channels, or when working with color values from design tools that export RGB.

Hexadecimal: RGB in Disguise

Hex codes are just RGB values written in base-16. Each pair of characters represents one channel (00-FF = 0-255).

/* Hex breakdown: #RRGGBB */
color: #FF0000; /* Red: FF=255, Green: 00=0, Blue: 00=0 */
color: #00FF00; /* Green */
color: #0000FF; /* Blue */
color: #FFFFFF; /* White */
color: #000000; /* Black */

/* Shorthand: #RGB (doubles each digit) */
color: #F00;    /* Same as #FF0000 */
color: #0F0;    /* Same as #00FF00 */
color: #FFF;    /* Same as #FFFFFF */

/* With alpha: #RRGGBBAA or #RGBA */
color: #FF000080; /* 50% transparent red (80 = 128 = ~50%) */
color: #F008;     /* Shorthand: roughly 50% transparent red (88 = 136) */

Hex alpha values: 00 = fully transparent, FF = fully opaque. Common values: 80 ≈ 50%, CC ≈ 80%, 33 ≈ 20%.

HSL: The Developer-Friendly Choice

HSL (Hue, Saturation, Lightness) describes color the way humans think about it. This makes it far easier to create variations and build cohesive palettes.

/* HSL syntax: hsl(hue, saturation%, lightness%) */
color: hsl(0, 100%, 50%);     /* Red */
color: hsl(120, 100%, 50%);   /* Green */
color: hsl(240, 100%, 50%);   /* Blue */
color: hsl(60, 100%, 50%);    /* Yellow */
color: hsl(300, 100%, 50%);   /* Magenta */
color: hsl(180, 100%, 50%);   /* Cyan */

Understanding the components:

ComponentRangeDescription
Hue0-360°Position on color wheel (0°=red, 120°=green, 240°=blue)
Saturation0-100%Color intensity (0%=gray, 100%=vivid)
Lightness0-100%Brightness (0%=black, 50%=pure color, 100%=white)
/* With alpha transparency */
color: hsla(220, 80%, 50%, 0.5);
/* Modern syntax (no 'a' needed) */
color: hsl(220 80% 50% / 0.5);

Why HSL is Better for Development

With HSL, creating color variations is intuitive because you only adjust one value:

:root {
  /* Base brand color */
  --brand-hue: 220;
  --brand-sat: 80%;

  /* Variations that only change lightness */
  --brand-50:  hsl(var(--brand-hue), var(--brand-sat), 95%);
  --brand-100: hsl(var(--brand-hue), var(--brand-sat), 90%);
  --brand-200: hsl(var(--brand-hue), var(--brand-sat), 80%);
  --brand-300: hsl(var(--brand-hue), var(--brand-sat), 70%);
  --brand-400: hsl(var(--brand-hue), var(--brand-sat), 60%);
  --brand-500: hsl(var(--brand-hue), var(--brand-sat), 50%); /* Base */
  --brand-600: hsl(var(--brand-hue), var(--brand-sat), 40%);
  --brand-700: hsl(var(--brand-hue), var(--brand-sat), 30%);
  --brand-800: hsl(var(--brand-hue), var(--brand-sat), 20%);
  --brand-900: hsl(var(--brand-hue), var(--brand-sat), 10%);
}

Hover and active states become simple arithmetic:

.button {
  --btn-hue: 220;
  --btn-sat: 80%;
  --btn-light: 50%;

  background: hsl(var(--btn-hue), var(--btn-sat), var(--btn-light));
}

.button:hover {
  /* Darken by 10 points of lightness */
  background: hsl(var(--btn-hue), var(--btn-sat), calc(var(--btn-light) - 10%));
}

.button:active {
  /* Darken more and reduce saturation */
  background: hsl(var(--btn-hue), calc(var(--btn-sat) - 10%), calc(var(--btn-light) - 15%));
}

Color Harmony: Building Palettes That Work

Color harmony is based on relationships around the color wheel. Here are the main schemes with practical HSL examples:

Complementary Colors

Colors opposite each other on the wheel (180° apart). High contrast, bold and energetic.

:root {
  --primary: hsl(220, 80%, 50%);        /* Blue */
  --complement: hsl(40, 80%, 50%);      /* Orange (220 + 180 = 400 → 40) */
}

Use for: Call-to-action buttons, highlighting important elements, creating visual tension.

Analogous Colors

Adjacent colors on the wheel (within 30-60°). Harmonious and pleasing.

:root {
  --primary: hsl(220, 70%, 50%);   /* Blue */
  --analog-1: hsl(190, 70%, 50%);  /* Cyan-blue */
  --analog-2: hsl(250, 70%, 50%);  /* Purple-blue */
}

Use for: Gradients, related UI sections, cohesive themes.

Triadic Colors

Three colors evenly spaced (120° apart). Balanced and vibrant.

:root {
  --primary: hsl(220, 70%, 50%);   /* Blue */
  --triad-1: hsl(340, 70%, 50%);   /* Red-pink (220 + 120) */
  --triad-2: hsl(100, 70%, 50%);   /* Green (220 + 240 → 100) */
}

Use for: Playful designs, dashboards with distinct sections, infographics.

Split-Complementary

The two colors adjacent to the complement (150° and 210°). Less tension than complementary.

:root {
  --primary: hsl(220, 70%, 50%);   /* Blue */
  --split-1: hsl(10, 70%, 50%);    /* Red-orange */
  --split-2: hsl(70, 70%, 50%);    /* Yellow-green */
}

Use for: When complementary feels too harsh, but you still want contrast.

Practical Palette Building

The 60-30-10 Rule

A rule of thumb borrowed from interior design that keeps interfaces balanced:

  • 60% dominant color (backgrounds, large areas)
  • 30% secondary color (cards, sections, navigation)
  • 10% accent color (CTAs, highlights, links)
:root {
  /* 60%: neutral backgrounds */
  --surface: hsl(220, 15%, 97%);
  --surface-alt: hsl(220, 15%, 93%);

  /* 30%: secondary elements */
  --secondary: hsl(220, 20%, 85%);
  --border: hsl(220, 15%, 80%);

  /* 10%: brand accent */
  --accent: hsl(220, 80%, 50%);
  --accent-hover: hsl(220, 80%, 40%);
}

Semantic Color System

Map colors to their purpose, not their appearance:

:root {
  /* Status colors */
  --success: hsl(142, 70%, 40%);
  --warning: hsl(38, 95%, 50%);
  --error: hsl(0, 85%, 55%);
  --info: hsl(210, 80%, 55%);

  /* Text hierarchy */
  --text-primary: hsl(220, 15%, 15%);
  --text-secondary: hsl(220, 10%, 45%);
  --text-muted: hsl(220, 10%, 60%);

  /* Interactive states */
  --focus-ring: hsl(220, 80%, 50%);
  --selection: hsl(220, 80%, 90%);
}

Dark Mode with HSL

HSL makes dark mode implementation straightforward:

:root {
  --hue: 220;

  /* Light mode */
  --bg: hsl(var(--hue), 15%, 98%);
  --surface: hsl(var(--hue), 15%, 100%);
  --text: hsl(var(--hue), 15%, 10%);
  --text-muted: hsl(var(--hue), 10%, 40%);
  --border: hsl(var(--hue), 15%, 85%);
}

[data-theme="dark"] {
  /* Dark mode: flip the lightness values */
  --bg: hsl(var(--hue), 15%, 8%);
  --surface: hsl(var(--hue), 15%, 12%);
  --text: hsl(var(--hue), 15%, 95%);
  --text-muted: hsl(var(--hue), 10%, 60%);
  --border: hsl(var(--hue), 15%, 20%);
}

Tip: In dark mode, bring the saturation of vibrant colors down a little so they don't glare against the dark background:

:root {
  --accent: hsl(220, 80%, 50%);
}

[data-theme="dark"] {
  --accent: hsl(220, 70%, 60%); /* Less saturated, lighter */
}

Accessibility and Contrast

Sufficient contrast ensures readability for everyone.

WCAG Contrast Requirements

LevelRatioUse Case
AA4.5:1Normal text
AA3:1Large text (≥ 24px, or ≥ about 18.7px bold)
AAA7:1Normal text, enhanced contrast
AA3:1UI components and graphics

Quick Contrast Checks

/* High-contrast text for light and dark surfaces */
--text-on-light: hsl(220, 15%, 15%);  /* Dark gray on white: ~15:1 */
--text-on-dark: hsl(220, 15%, 95%);   /* Near-white on dark: ~14:1 */

/* Avoid pure black and white: slightly softer values are easier on the eyes */
--bg-light: hsl(220, 15%, 98%);  /* Not #FFFFFF */
--bg-dark: hsl(220, 15%, 8%);    /* Not #000000 */

Testing Contrast Programmatically

function getLuminance(r, g, b) {
  const [rs, gs, bs] = [r, g, 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 * rs + 0.7152 * gs + 0.0722 * bs;
}

function getContrastRatio(rgb1, rgb2) {
  const l1 = getLuminance(...rgb1);
  const l2 = getLuminance(...rgb2);
  const lighter = Math.max(l1, l2);
  const darker = Math.min(l1, l2);
  return (lighter + 0.05) / (darker + 0.05);
}

// Usage
const ratio = getContrastRatio([0, 0, 0], [255, 255, 255]); // 21:1

Color Psychology Quick Reference

Colors carry emotional associations. Use them intentionally:

ColorAssociationsCommon UI Uses
BlueTrust, calm, professionalLinks, primary actions, tech brands
GreenSuccess, growth, natureSuccess states, confirmations, eco themes
RedUrgency, error, passionErrors, destructive actions, sales
YellowWarning, optimism, energyWarnings, highlights, attention
PurpleLuxury, creativity, wisdomPremium features, creative tools
OrangeFriendly, energetic, affordableCTAs, notifications, budget options
GrayNeutral, professional, balancedBackgrounds, disabled states, borders

Modern CSS Color Features

Relative Color Syntax (CSS Color Level 5)

Create color variations without variables:

.button {
  --color: hsl(220 80% 50%);
  background: var(--color);
}

.button:hover {
  /* Darken by adjusting lightness relative to original */
  background: hsl(from var(--color) h s calc(l - 10));
}

color-mix()

Blend colors directly in CSS:

.element {
  /* Mix 70% blue with 30% white */
  background: color-mix(in srgb, hsl(220 80% 50%) 70%, white);

  /* Create a semi-transparent version */
  border-color: color-mix(in srgb, currentColor 50%, transparent);
}

Key Takeaways

  1. Use HSL for development: it's intuitive and makes variations easy
  2. Build systematic palettes with CSS custom properties
  3. Follow the 60-30-10 rule for balanced interfaces
  4. Test contrast to ensure accessibility (4.5:1 minimum for text)
  5. Consider color meaning and cultural associations
  6. Plan for dark mode from the start

To experiment with these ideas, the Loopaloo Color Picker reads a color back in every common notation with its contrast ratios, and the Color Palette Generator builds harmonies with the contrast of every pair already worked out.

Related Tools

Related Articles