Whether you’re building a web app, tuning CSS variables for a dark mode theme, or tweaking an SVG icon, you’ve probably seen colors expressed as #3b82f6, rgb(59, 130, 246), or hsl(217, 91%, 60%) They all represent the same blue shade, but each format was built with a specific purpose in mind.

1. RGB: How Screens Actually Emit Light
Computer monitors, phone screens, and TVs use additive color Each physical pixel on your screen contains three tiny subpixels: Red, Green, and Blue. By varying the brightness of each subpixel from 0 (completely off) to 255 (full blast), your display can mix over 16.7 million distinct colors (256 × 256 × 256).
rgb(0, 0, 0): All subpixels off → Pure Blackrgb(255, 255, 255): All subpixels at maximum brightness → Pure Whitergb(255, 0, 0): Only red subpixels lit → Pure Redrgb(255, 255, 0): Red + Green at full power → Bright Yellow
2. HEX Codes: Base-16 Shorthand for RGB
Hexadecimal color codes are simply RGB values written in base-16 notation. A byte (0 to 255 in decimal) translates cleanly into two hexadecimal digits (00 to FF):
# R R G G B B [A A]
# 3 b 8 2 f 6
# │ │ └── Blue channel: 0xf6 = 246
# │ └────── Green channel: 0x82 = 130
# └────────── Red channel: 0x3b = 59
Adding two extra digits at the end sets the alpha (transparency) channel. For example, #3b82f680 renders that same blue with 50% opacity (0x80 = 128 / 255 ≈ 50%).
3. HSL: The Color Model Built for Human Brains
While HEX and RGB make sense to computer GPUs, they are notoriously difficult for humans to adjust manually. If someone hands you #4a7c59 and asks you to make it slightly lighter and a bit warmer, guessing the new HEX numbers is nearly impossible.
HSL (Hue, Saturation, Lightness) solves this elegantly:
- Hue (0° to 360°): The angle on the color wheel. 0° is Red, 60° is Yellow, 120° is Green, 180° is Cyan, 240° is Blue, and 300° is Magenta.
- Saturation (0% to 100%): The intensity of the pigment. 0% is completely grayscale; 100% is vibrant, pure color.
- Lightness (0% to 100%): The illumination level. 0% is pitch black, 50% is normal color, and 100% is pure white.
Why HSL is a Game-Changer for CSS Design Systems
Using HSL in CSS custom properties makes dynamic themes effortless. You can define a single primary hue and generate hover states, borders, and light/dark mode variations simply by tweaking the Lightness percentage:
:root {
--primary-h: 217;
--primary-s: 91%;
--primary-base: hsl(var(--primary-h), var(--primary-s), 60%);
--primary-hover: hsl(var(--primary-h), var(--primary-s), 50%); /* 10% darker */
--primary-light: hsl(var(--primary-h), var(--primary-s), 95%); /* subtle tint */
}
Contrast Ratios and Accessibility (WCAG)
When styling text over colored backgrounds, always test against Web Content Accessibility Guidelines (WCAG):
- WCAG AA (Standard): Requires at least a 4.5:1 contrast ratio for normal body text and 3:1 for large text (18pt+ or bold 14pt+).
- WCAG AAA (Enhanced): Requires at least a 7:1 contrast ratio for normal text.
Key Takeaways for Digital Color Models Explained
Use HEX for concise color definitions in icons and design files, RGB when doing canvas pixel math in JavaScript, and HSL whenever you are architecting a flexible, maintainable CSS theme.
