Color Codes Explained: HEX, RGB, HSL Complete Guide for Designers & Developers

Whether you are designing a website, building a mobile app, or creating digital art, you work with color codes every single day. Yet many designers and developers treat color values as a black box—copying hex strings from color pickers without truly understanding what those characters represent or how the different color models relate to each other.

In this comprehensive guide, we will demystify the three most important color formats used on the web: HEX, RGB, and HSL. You will learn exactly how each model works, how to convert between them by hand or in code, and how to choose the right format for every situation. We will also cover CSS color functions, accessibility contrast requirements, and fundamental color theory that every creative professional should know.

What Are Color Codes and Why Do They Matter?

A color code is a standardized way to describe a specific color numerically so that computers, browsers, and design tools can reproduce it exactly. Without color codes, there would be no way to guarantee that the blue on your monitor matches the blue on your colleague's screen or in a user's browser halfway across the world.

Color codes matter for several important reasons:

  • Consistency: They ensure the exact same hue appears across devices, operating systems, and applications.
  • Communication: Designers can share a precise value like #2563EB instead of saying "a medium-dark blue."
  • Automation: Build tools, preprocessors, and JavaScript libraries can programmatically generate, lighten, darken, and blend colors when they are represented numerically.
  • Accessibility: Contrast-checking algorithms rely on numeric color values to determine whether text meets WCAG guidelines.

The web primarily uses three color notation systems—HEX, RGB, and HSL—each offering a different lens through which to understand and manipulate color. Let us explore each one in detail.

HEX Colors Explained

🎨 HEX Color Format

HEX (hexadecimal) is the most widely recognized color format on the web. A HEX color begins with a hash symbol (#) followed by six hexadecimal digits that represent the red, green, and blue channels of the color.

The #RRGGBB Structure

Each pair of characters in a six-digit HEX code maps to one color channel on a scale from 00 (0 in decimal, meaning none of that color) to FF (255 in decimal, meaning full intensity):

/* Anatomy of a HEX color */
#FF5733
  ││││││
  FF → Red   = 255
    57 → Green = 87
      33 → Blue  = 51

Because hexadecimal is base-16, each digit can be one of sixteen values: 0–9 and A–F. Two hex digits together give 16 × 16 = 256 possible values per channel, and three channels yield 256³ = 16,777,216 possible colors.

Shorthand HEX Notation

When both digits in every pair are the same, CSS allows you to shorten the code to just three characters. The browser doubles each digit automatically:

/* Shorthand HEX examples */
#FF0000#F00   /* pure red */
#33CC99#3C9   /* teal-green */
#FFFFFF#FFF   /* white */
#000000#000   /* black */

8-Digit HEX with Alpha Transparency

Modern browsers also support an eight-digit HEX format that adds a two-character alpha (opacity) value at the end. The alpha channel follows the same 00–FF scale, where 00 is fully transparent and FF is fully opaque:

/* 8-digit HEX with alpha */
#FF573380/* ~50% opacity (80 hex ≈ 128 decimal ≈ 50.2%) */
#2563EBCC/* ~80% opacity */
#00000000/* fully transparent black */

You can also use a four-digit shorthand (#RGBA) where each digit is doubled, just like three-digit shorthand. For instance, #F008 expands to #FF000088.

💡 Pro Tip

HEX codes are case-insensitive: #ff5733 and #FF5733 produce the same color. Most style guides prefer lowercase for readability, but uppercase is equally valid.

RGB Color Model

🔴🟢🔵 RGB Color Format

RGB stands for Red, Green, Blue—the three primary colors of light. The RGB model is an additive color system, meaning you create colors by adding light together. When all three channels are at full intensity you get white; when all are zero you get black.

The 0–255 Range

In CSS, each RGB channel is specified as an integer from 0 to 255 (or as a percentage from 0% to 100%). The syntax uses the rgb() function:

/* RGB syntax in CSS */
.element {
  color: rgb(255, 87, 51);   /* decimal values */
  color: rgb(100%, 34%, 20%); /* percentage values */
}

RGBA — Adding Transparency

The rgba() function adds a fourth parameter—the alpha channel—as a value from 0 (completely transparent) to 1 (completely opaque):

/* RGBA with transparency */
.overlay {
  background: rgba(0, 0, 0, 0.5)/* 50% transparent black */
}
.highlight {
  background: rgba(37, 99, 235, 0.15); /* very faint blue */
}

In modern CSS (Level 4), you can also write the alpha value right inside the standard rgb() function using a forward slash: rgb(255 87 51 / 0.8). This newer syntax works in all current browsers.

Additive Color Mixing

Unlike paint or ink, where mixing colors together produces darker shades (subtractive mixing), light-based RGB works in the opposite way:

  • Red (255, 0, 0) + Green (0, 255, 0) = Yellow (255, 255, 0)
  • Red (255, 0, 0) + Blue (0, 0, 255) = Magenta (255, 0, 255)
  • Green (0, 255, 0) + Blue (0, 0, 255) = Cyan (0, 255, 255)
  • All three at max = White (255, 255, 255)

This additive behavior is why RGB is the native color model for screens, monitors, and projectors—all devices that emit light.

HSL Color Model

🌈 HSL Color Format

HSL stands for Hue, Saturation, Lightness. It was designed to be more intuitive for humans because it separates the concepts of which color, how vivid, and how bright into independent axes.

Hue (0°–360°)

Hue represents the type of color on a circular wheel. Think of a rainbow bent into a circle, where the degrees correspond to positions on that wheel:

  • 0° / 360° — Red
  • 60° — Yellow
  • 120° — Green
  • 180° — Cyan
  • 240° — Blue
  • 300° — Magenta

Saturation (0%–100%)

Saturation controls the vividness of the color. At 100% the color is fully vivid; at 0% it becomes a shade of gray regardless of the hue value. You can think of saturation as the amount of "color" in the color.

Lightness (0%–100%)

Lightness determines how bright or dark the color is. At 0% you always get black, at 100% you always get white, and at 50% you get the purest, most saturated version of the hue.

/* HSL syntax in CSS */
.primary {
  color: hsl(220, 90%, 56%);   /* vibrant blue */
}
.muted {
  color: hsl(220, 15%, 56%);   /* grayish blue */
}

/* HSLA with transparency */
.transparent-bg {
  background: hsla(220, 90%, 56%, 0.2); /* faint blue bg */
}

✅ Why Designers Love HSL

HSL makes it trivial to create consistent color palettes. To darken a color, lower the lightness. To make a pastel, increase lightness and lower saturation. To find a complementary color, add 180° to the hue. This kind of manipulation is far harder to reason about in HEX or RGB.

Converting Between Color Formats

Understanding how to convert between HEX, RGB, and HSL gives you complete flexibility to work in whichever system a project requires. Below are the key conversion methods.

HEX to RGB Conversion

Converting HEX to RGB is straightforward—split the hex string into three pairs and convert each from base-16 to base-10:

/* Example: #3B82F6 → RGB */
R = hex("3B") = 3×16 + 11 = 59
G = hex("82") = 8×16 + 2  = 130
B = hex("F6") = 15×16 + 6 = 246

Result: rgb(59, 130, 246)

RGB to HEX Conversion

The reverse is also simple—convert each decimal channel value to a two-digit hexadecimal string:

/* Example: rgb(16, 185, 129) → HEX */
R = 16  → hex = 10
G = 185 → hex = B9
B = 129 → hex = 81

Result: #10B981

RGB to HSL Conversion

The RGB-to-HSL conversion requires more math. Here is the algorithm in simplified steps:

  1. Normalize the R, G, B values by dividing each by 255 to get values between 0 and 1.
  2. Find the maximum (Cmax) and minimum (Cmin) of the three normalized values, and calculate the delta (Δ = Cmax − Cmin).
  3. Lightness: L = (Cmax + Cmin) / 2.
  4. Saturation: If Δ = 0, S = 0. Otherwise, S = Δ / (1 − |2L − 1|).
  5. Hue: If Δ = 0, H = 0°. Otherwise, the formula depends on which channel is the max:
    • If Cmax is R: H = 60° × ((G − B) / Δ mod 6)
    • If Cmax is G: H = 60° × ((B − R) / Δ + 2)
    • If Cmax is B: H = 60° × ((R − G) / Δ + 4)
// JavaScript helper: RGB → HSL
function rgbToHsl(r, g, b) {
  r /= 255; g /= 255; b /= 255;
  const max = Math.max(r, g, b);
  const min = Math.min(r, g, b);
  let h, s, l = (max + min) / 2;
  const d = max - min;
  if (d === 0) { h = s = 0; }
  else {
    s = d / (1 - Math.abs(2 * l - 1));
    switch (max) {
      case r: h = ((g - b) / d) % 6; break;
      case g: h = (b - r) / d + 2; break;
      case b: h = (r - g) / d + 4; break;
    }
    h = Math.round(h * 60);
    if (h < 0) h += 360;
  }
  return [h, Math.round(s*100), Math.round(l*100)];
}

⚠️ Rounding Differences

Due to rounding in the conversion process, you may notice very small differences when converting back and forth between formats. For example, an HSL value converted to HEX and then back to HSL might be off by 1° in hue or 1% in saturation. This is normal and visually imperceptible.

CSS Color Formats and Named Colors

CSS gives developers a rich set of ways to specify color. Beyond HEX, RGB, and HSL, there are several additional options worth knowing.

CSS Named Colors

CSS defines 148 named colors that you can use by keyword. Some common ones include:

  • coral — equivalent to #FF7F50
  • steelblue — equivalent to #4682B4
  • tomato — equivalent to #FF6347
  • rebeccapurple — equivalent to #663399
  • transparent — fully transparent (equivalent to rgba(0,0,0,0))

Named colors are great for quick prototyping, but for production work, explicit HEX, RGB, or HSL values give you precise control. For a complete reference, see MDN's CSS named-color documentation.

The Modern color() Function and Wider Gamuts

CSS Color Level 4 introduces the color() function, which supports color spaces beyond sRGB—such as Display P3 and Rec. 2020—for richer, more saturated colors on compatible displays. The oklch() and oklab() functions provide perceptually uniform color manipulation. While these are the future, HEX, RGB, and HSL remain the workhorses for the vast majority of web projects today.

Color Accessibility and WCAG Contrast Ratios

Choosing visually appealing colors is only half the job. If your text does not have sufficient contrast against its background, a significant portion of your audience may struggle to read it. The Web Content Accessibility Guidelines (WCAG) 2.1 define minimum contrast ratios to ensure readability.

WCAG Contrast Ratio Requirements

Level Normal Text Large Text
AA (minimum) 4.5 : 1 3 : 1
AAA (enhanced) 7 : 1 4.5 : 1

"Large text" is defined as 18pt (24px) regular weight or 14pt (18.66px) bold. The contrast ratio is calculated using the relative luminance of the foreground and background colors, which is derived from their RGB values using a formula that accounts for human perception of brightness.

Calculating Relative Luminance

The formula for relative luminance (L) from linear RGB values is:

L = 0.2126 × R + 0.7152 × G + 0.0722 × B

Where R, G, B are first linearized from their sRGB values. The contrast ratio between two luminances L1 (lighter) and L2 (darker) is: (L1 + 0.05) / (L2 + 0.05).

Practical Accessibility Tips

  • Never rely on color alone to convey information—use icons, labels, or patterns as well.
  • Test with color-blindness simulators; roughly 8% of men and 0.5% of women have some form of color vision deficiency.
  • Use dark gray instead of pure black for body text (e.g., #1f2937 instead of #000000)—it reduces eye strain while still passing contrast requirements.
  • Check every color pair in your design, including links, buttons, form labels, and placeholder text.

💡 Quick Contrast Check

A simple rule of thumb: if you can comfortably read the text while squinting, the contrast is probably adequate. But always verify with a proper contrast-checking tool—human judgment is unreliable for borderline cases.

Color Theory Basics for Digital Design

Understanding core color theory helps you create palettes that are harmonious, visually balanced, and meaningful. Here are the most important relationships on the color wheel.

Complementary Colors

Complementary colors sit directly opposite each other on the color wheel (180° apart in HSL terms). They create maximum contrast and visual tension, making them perfect for call-to-action buttons against a background. Examples:

  • Blue (220°) ↔ Orange (40°)
  • Red (0°) ↔ Cyan (180°)

Analogous Colors

Analogous colors are adjacent on the wheel (within about 30–60° of each other). They produce smooth, harmonious palettes often seen in nature. Because the hues are close, these schemes feel cohesive and are easy on the eyes. For example, a palette of teal (180°), blue (210°), and indigo (240°) creates a cool, professional tone.

Triadic Colors

A triadic scheme uses three colors evenly spaced at 120° intervals—for example, red (0°), green (120°), and blue (240°). Triadic palettes are vibrant and balanced but can be overwhelming if all three are used at full saturation. A common approach is to pick one dominant color and use the other two as accents with reduced saturation or lightness.

Split-Complementary and Tetradic

A split-complementary scheme takes one base color and pairs it with the two colors adjacent to its complement, providing contrast with less tension than a pure complementary pair. Tetradic (or double-complementary) schemes use four colors forming a rectangle on the wheel—they offer the most variety but require careful balance.

Color Format Comparison Table

This table summarizes the key characteristics of each format to help you choose the right one for your project:

Feature HEX RGB HSL
Syntax Example #3B82F6 rgb(59,130,246) hsl(217,91%,60%)
Transparency 8-digit (#RRGGBBAA) rgba() function hsla() function
Human Readability Low Medium High
Compact Notation 3–4 digit shorthand None None
Palette Generation Difficult Moderate Easy
Best For Concise CSS values Programmatic use Design & theming
Browser Support Universal Universal Universal
Color Space sRGB sRGB sRGB (cylindrical)

Tools for Working with Colors

Whether you need to pick a color from an image, generate a palette, or convert between formats, there are excellent tools available:

  • FileCraft Pro Color Converter — Our free browser-based tool lets you instantly convert between HEX, RGB, HSL, and more with a live preview and copy-to-clipboard functionality.
  • Browser DevTools Color Picker — Chrome, Firefox, and Edge all include built-in color pickers in their inspector panels. Click any color value in the Styles pane to open it.
  • Figma / Adobe Color — Professional design tools with advanced palette generation, accessibility scoring, and color harmony features.
  • Coolors.co — A popular palette generator that locks colors you like and randomizes the rest until you find a combination that works.

🎨 Try Our Free Color Converter

Instantly convert between HEX, RGB, HSL, and more. Copy-friendly output, live preview, and zero sign-up required.

Open Color Converter

Practical Tips for Using Color Codes

CSS Custom Properties (Variables)

Use CSS custom properties to define your color palette once and reference it everywhere. This makes global theme changes effortless and is the foundation of dark-mode support:

:root {
  --color-primary: #2563eb;
  --color-primary-rgb: 37, 99, 235;
  --color-text: #1f2937;
  --color-bg: #ffffff;
}

[data-theme="dark"] {
  --color-text: #f3f4f6;
  --color-bg: #111827;
}

.card {
  background: rgba(var(--color-primary-rgb), 0.1);
}

Choosing the Right Format

  • Use HEX for simple, static color definitions in stylesheets—it is the most compact and universally understood.
  • Use RGB/RGBA when you need transparency or when manipulating colors programmatically in JavaScript.
  • Use HSL/HSLA when designing themes, creating tint/shade variations, or building color palettes—its parameters map directly to how humans think about color.

Performance Considerations

All color formats are parsed equally fast by modern browsers, so the choice is purely about developer ergonomics and maintainability—not performance. Pick whichever format makes your code the most readable and your workflow the most efficient.

Frequently Asked Questions

What is the difference between HEX, RGB, and HSL?

HEX is a compact hexadecimal notation (#RRGGBB), RGB uses decimal values for red/green/blue channels (0–255), and HSL uses hue angle, saturation percentage, and lightness percentage. All three describe the same sRGB color space—they are simply different ways to express the same colors.

How do I convert HEX to RGB?

Split the six-character HEX code into three pairs (RR, GG, BB). Convert each pair from base-16 to base-10. For example, #FF5733 → R=255, G=87, B=51 → rgb(255, 87, 51). Shorthand like #F00 expands to #FF0000 first.

What WCAG contrast ratio do I need?

WCAG 2.1 Level AA requires at least 4.5:1 for normal text and 3:1 for large text. Level AAA requires 7:1 and 4.5:1 respectively. Always verify with a contrast checker tool.

Which color format should I use in my CSS?

HEX is best for static values due to its compact syntax. RGBA is ideal for transparency and programmatic manipulation. HSL is best for building palettes and themes because its parameters (hue, saturation, lightness) are intuitive. All formats have universal browser support.

Conclusion

Color codes are a fundamental part of every designer's and developer's toolkit. Whether you favor the compact simplicity of HEX, the programmatic clarity of RGB, or the intuitive power of HSL, understanding all three formats gives you the flexibility to work efficiently in any context.

Here is a quick summary of when to reach for each format:

  • HEX (#RRGGBB) — Best for concise CSS declarations and design handoffs.
  • RGB / RGBA — Best for JavaScript manipulation and transparency control.
  • HSL / HSLA — Best for building harmonious palettes and dark/light themes.

Beyond choosing the right format, remember that color choices directly impact usability and accessibility. Always check your contrast ratios, consider color-blind users, and leverage color theory principles to create designs that are both beautiful and inclusive.

Ready to put your knowledge into practice? Try converting some colors right now with our free Color Converter tool—no sign-up, no downloads, everything runs right in your browser.

P

Written by Paras

Founder and lead developer at FileCraft Pro. I build free, privacy-first browser-based tools for file conversion and image processing. Read more about me