CodeCraft
← Back to Blog
Frontend2026-04-21·9 min read

Color Formats Explained: HEX, RGB, HSL, and When to Use Each

#css#color#hex#rgb#hsl#design
C
CodeCraft Team· Engineering

Colors are defined in surprisingly many ways in web development and design. A single shade of blue can be expressed as `#1da1f2`, `rgb(29, 161, 242)`, `hsl(203, 89%, 53%)`, or even a named color. Knowing the differences between HEX, RGB, HSL, OKLCH, and other color spaces isn't just academic — it directly affects how you write maintainable CSS, create accessible color palettes, and animate colors in JavaScript.

This guide explains every major CSS color format, why HSL revolutionized theme design, why OKLCH is the future, and how to convert between formats programmatically.

HEX Colors: The Web Standard

Hexadecimal (HEX) color notation is the most recognizable web color format. A HEX color encodes three bytes (red, green, blue) as three pairs of hexadecimal digits:

#RRGGBB
#ff5733   /* red=255, green=87, blue=51 */
#1da1f2   /* Twitter blue */
#000000   /* black */
#ffffff   /* white */

Shorthand HEX: When each pair uses the same digit twice, you can use the 3-digit shorthand:

#aabbcc → #abc
#112233 → #123
#ffffff → #fff
#000000 → #000

HEX with alpha channel (HEX8 and HEX4): Add a fourth byte for transparency (supported in all modern browsers):

#ff573380   /* 50% transparent red */
#1da1f2cc   /* 80% opaque Twitter blue */
#fff8       /* 53% transparent white (shorthand) */

Pros: Compact, universally understood by designers and developers, easy to copy-paste, consistent with design tool outputs (Figma, Sketch, Adobe XD).

Cons: Not intuitive — you can't mentally calculate that #3498db is a medium blue without looking it up. Hard to create variations (lighter/darker) without a tool.

RGB and RGBA: Component-Based Color

RGB specifies colors as three components: red, green, and blue, each ranging 0–255 (or 0%–100%):

rgb(255, 87, 51)       /* same as #ff5733 */
rgb(29, 161, 242)      /* Twitter blue */
rgb(0, 0, 0)           /* black */
rgb(100%, 0%, 0%)      /* red */

/* Modern syntax (CSS Color Level 4): no commas */
rgb(29 161 242)
rgb(29 161 242 / 80%)  /* 80% opacity */

RGBA adds an alpha (opacity) channel (0 = transparent, 1 = opaque):

rgba(0, 0, 0, 0.5)       /* 50% black overlay */
rgba(255, 255, 255, 0.9) /* nearly opaque white */

/* Modern equivalent: */
rgb(0 0 0 / 50%)

In JavaScript, RGB is commonly used for canvas operations and color calculations:

// Canvas 2D:
ctx.fillStyle = 'rgb(29, 161, 242)';

// Parsing hex to RGB:
function hexToRgb(hex) {
  const result = /^#?([a-fd]{2})([a-fd]{2})([a-fd]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

HSL: The Designer's Color Model

HSL (Hue, Saturation, Lightness) revolutionized CSS color work by mapping colors to how humans think about them:

hsl(hue, saturation%, lightness%)
hsl(203, 89%, 53%)    /* Twitter blue */
hsl(0, 100%, 50%)     /* pure red */
hsl(120, 100%, 50%)   /* pure green */
hsl(240, 100%, 50%)   /* pure blue */
hsl(0, 0%, 0%)        /* black */
hsl(0, 0%, 100%)      /* white */
hsl(0, 0%, 50%)       /* 50% gray */

Hue: The color angle on the color wheel (0–360°):

- 0° / 360° = Red

- 60° = Yellow

- 120° = Green

- 180° = Cyan

- 240° = Blue

- 300° = Magenta

Saturation: Color intensity (0% = gray, 100% = fully saturated)

Lightness: 0% = black, 50% = normal, 100% = white

Why HSL is powerful for themes: Creating color variations is intuitive:

:root {
  --brand-hue: 203;
  
  --brand-light: hsl(var(--brand-hue), 89%, 90%);   /* light background */
  --brand-base: hsl(var(--brand-hue), 89%, 53%);    /* primary button */
  --brand-dark: hsl(var(--brand-hue), 89%, 30%);    /* hover state */
  --brand-text: hsl(var(--brand-hue), 89%, 15%);    /* dark text */
}

Changing --brand-hue instantly recolors your entire theme!

HSLA: Same as HSL but with an alpha channel for transparency.

OKLCH: The Future of CSS Colors

OKLCH is the newest color format, available in CSS Color Level 4 and supported in all modern browsers. It solves a fundamental problem with HSL and RGB: perceptual uniformity.

With HSL, colors at the same lightness value (e.g., hsl(60, 100%, 50%) yellow and hsl(240, 100%, 50%) blue) look dramatically different in perceived brightness — yellow looks much brighter. This makes creating accessible color palettes and smooth gradients with HSL unreliable.

OKLCH uses a perceptually uniform color space where equal numerical changes produce equal perceived changes:

/* oklch(lightness chroma hue) */
oklch(60% 0.15 203)       /* a medium blue */
oklch(60% 0.15 60)        /* a medium yellow — same perceived brightness! */

/* Practical usage: */
oklch(0.51 0.19 264)      /* blue */
oklch(0.78 0.13 126)      /* green */
oklch(0.70 0.15 38)       /* orange */

Benefits of OKLCH:

- Predictable lightness — great for accessible text contrast

- Vibrant, wide-gamut colors (P3 display support)

- Better gradients that pass through natural-looking intermediate colors

- Supported natively in CSS with color(display-p3 ...) fallback

Using OKLCH in design systems:

:root {
  --primary-050: oklch(97% 0.04 264);
  --primary-100: oklch(93% 0.07 264);
  --primary-500: oklch(55% 0.19 264);
  --primary-900: oklch(25% 0.09 264);
}

Named Colors and System Colors

CSS defines 140+ named colors that are handy for prototyping:

color: red;
color: cornflowerblue;
color: chocolate;
color: tomato;
color: rebeccapurple; /* named after Eric Meyer's daughter Rebecca */

Special keywords:

color: transparent;      /* rgba(0,0,0,0) */
color: currentColor;     /* inherits the current text color */
color: inherit;          /* inherits from parent */
background: Canvas;      /* OS/browser system background color */
color: CanvasText;       /* OS/browser system text color */

currentColor is especially powerful for icon components:

.icon svg { fill: currentColor; } /* icon changes color with text */

System color keywords (CSS Color Level 4) let you use the user's OS theme colors, supporting light/dark mode automatically:

button {
  background: ButtonFace;
  color: ButtonText;
  border-color: ButtonBorder;
}

Color Conversion: The Math Behind the Formats

HEX to RGB (trivial — it's just base-16):

const hex = "#1da1f2";
const r = parseInt(hex.slice(1, 3), 16); // → 29
const g = parseInt(hex.slice(3, 5), 16); // → 161
const b = parseInt(hex.slice(5, 7), 16); // → 242

RGB to HEX:

const toHex = (n) => n.toString(16).padStart(2, '0');
const hex = `#${toHex(r)}${toHex(g)}${toHex(b)}`;

RGB to HSL (requires normalization):

function rgbToHsl(r, g, b) {
  r /= 255; g /= 255; b /= 255;
  const max = Math.max(r, g, b), min = Math.min(r, g, b);
  let h, s, l = (max + min) / 2;

  if (max === min) {
    h = s = 0; // achromatic
  } else {
    const d = max - min;
    s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
    switch (max) {
      case r: h = ((g - b) / d + (g < b ? 6 : 0)) / 6; break;
      case g: h = ((b - r) / d + 2) / 6; break;
      case b: h = ((r - g) / d + 4) / 6; break;
    }
  }
  return [Math.round(h * 360), Math.round(s * 100), Math.round(l * 100)];
}

Using the `color-convert` npm package for easy programmatic conversion:

import convert from 'color-convert';
convert.hex.hsl('1da1f2'); // → [203, 89, 53]
convert.rgb.hex(29, 161, 242); // → "1da1f2"

Choosing the Right Color Format

Use HEX when:

- Working with design tokens from Figma/Sketch/Adobe XD

- Copy-pasting brand colors from a style guide

- Storing colors in a database or config file

- Communicating with designers (HEX is the universal designer language)

Use RGB/RGBA when:

- Doing color math in JavaScript (canvas, WebGL, color interpolation)

- You need transparency that works in all browsers including IE (now legacy)

- Generating colors programmatically from component values

Use HSL/HSLA when:

- Building CSS custom property theme systems

- Creating color variations (lighter/darker) via CSS calc()

- You want color-manipulation logic in pure CSS

Use OKLCH when:

- Building a design system with precise perceptual uniformity

- Targeting modern browsers and need P3 wide-gamut colors

- Creating accessible color palettes with guaranteed contrast ratios

- Building smooth CSS gradients

Use named colors when:

- Quick prototyping

- Test styles that will be replaced later

- Using semantic color keywords like currentColor or system colors

Color formats might seem like a minor CSS detail, but choosing the right format for the right context makes your code more maintainable, your themes more flexible, and your designs more accessible. HEX for design tokens, HSL for theme systems, OKLCH for the design system of the future. Use our free Color Converter tool to instantly convert between HEX, RGB, and HSL formats with a real-time color preview — no installation, no accounts, no data sent to any server.