Published on

HEX vs LCH: What's the Difference and When to Use Each?

Authors

Introduction

Color formats in modern web design are evolving beyond the basics, and understanding the difference between HEX and LCH is crucial for creating truly accessible and visually appealing designs. I've worked extensively with both formats, and I've learned that the choice between them isn't just about syntax—it's about understanding the difference between web-friendly color representation and perceptually uniform color spaces. In this blog, I'll break down the origins, definitions, and practical uses of HEX and LCH, so you can make informed decisions about which format to use in your next project.

HEX and LCH represent two different approaches to color representation in web design. HEX (Hexadecimal) is designed around web-friendly color representation and easy-to-read color codes, while LCH (Lightness, Chroma, Hue) is designed around perceptual uniformity—ensuring that color changes appear equally significant to human eyes. If you've ever wondered why some color adjustments feel more natural than others, or why some color spaces are better for accessibility, you're in the right place. Let's explore these essential color formats together.

HEX vs LCH: What's the Difference and When to Use Each?

What is HEX?

HEX stands for Hexadecimal. It's a color format that represents colors using six hexadecimal digits (0-9, A-F) in the format #RRGGBB. Each pair of digits represents the red, green, and blue components (0-255). For example:

  • #FF0000 is pure red (255, 0, 0)
  • #00FF00 is pure green (0, 255, 0)
  • #0000FF is pure blue (0, 0, 255)
  • #FFFFFF is white (255, 255, 255)
  • #000000 is black (0, 0, 0)

What is LCH?

LCH stands for Lightness, Chroma, and Hue. It's a perceptually uniform color space that ensures color changes appear equally significant to human eyes. L represents lightness (0-100), C represents chroma (0-150+), and H represents hue (0-360 degrees). For example:

  • lch(53.24, 104.55, 40.85) is pure red
  • lch(87.73, 119.78, 136.02) is pure green
  • lch(32.3, 133.81, 306.28) is pure blue
  • lch(100, 0, 0) is white
  • lch(0, 0, 0) is black

Algorithm behind HEX to LCH Conversion and LCH to HEX Conversion

HEX to LCH Conversion

To convert HEX to LCH, we first convert HEX to RGB, then RGB to XYZ, then XYZ to Lab, and finally Lab to LCH. The algorithm involves multiple coordinate system transformations to achieve perceptual uniformity.

function hexToLch(hex) {
  // Remove # if present and normalize to 6 digits
  hex = hex.replace('#', '')
  if (hex.length === 3) {
    hex = hex
      .split('')
      .map((char) => char + char)
      .join('')
  }

  // Convert HEX to RGB
  const r = parseInt(hex.substring(0, 2), 16)
  const g = parseInt(hex.substring(2, 4), 16)
  const b = parseInt(hex.substring(4, 6), 16)

  // Convert RGB to XYZ (sRGB)
  const rNorm = r / 255
  const gNorm = g / 255
  const bNorm = b / 255

  const rLinear = rNorm <= 0.04045 ? rNorm / 12.92 : Math.pow((rNorm + 0.055) / 1.055, 2.4)
  const gLinear = gNorm <= 0.04045 ? gNorm / 12.92 : Math.pow((gNorm + 0.055) / 1.055, 2.4)
  const bLinear = bNorm <= 0.04045 ? bNorm / 12.92 : Math.pow((bNorm + 0.055) / 1.055, 2.4)

  const x = 0.4124 * rLinear + 0.3576 * gLinear + 0.1805 * bLinear
  const y = 0.2126 * rLinear + 0.7152 * gLinear + 0.0722 * bLinear
  const z = 0.0193 * rLinear + 0.1192 * gLinear + 0.9505 * bLinear

  // Convert XYZ to Lab
  const xNorm = x / 0.95047
  const yNorm = y / 1.0
  const zNorm = z / 1.08883

  const xLab = xNorm > 0.008856 ? Math.pow(xNorm, 1 / 3) : 7.787 * xNorm + 16 / 116
  const yLab = yNorm > 0.008856 ? Math.pow(yNorm, 1 / 3) : 7.787 * yNorm + 16 / 116
  const zLab = zNorm > 0.008856 ? Math.pow(zNorm, 1 / 3) : 7.787 * zNorm + 16 / 116

  const l = 116 * yLab - 16
  const a = 500 * (xLab - yLab)
  const b = 200 * (yLab - zLab)

  // Convert Lab to LCH
  const c = Math.sqrt(a * a + b * b)
  const h = Math.atan2(b, a) * (180 / Math.PI)
  const hNormalized = h >= 0 ? h : h + 360

  return {
    l: Math.round(l * 100) / 100,
    c: Math.round(c * 100) / 100,
    h: Math.round(hNormalized * 100) / 100,
  }
}

// Example usage:
// hexToLch('#FF0000') → {l: 53.24, c: 104.55, h: 40.85}
// hexToLch('#00FF00') → {l: 87.73, c: 119.78, h: 136.02}

LCH to HEX Conversion

To convert LCH to HEX, we reverse the process: LCH to Lab, Lab to XYZ, XYZ to RGB, and finally RGB to HEX. The algorithm reconstructs the web-friendly color format from the perceptually uniform space.

function lchToHex(l, c, h) {
  // Convert LCH to Lab
  const lNorm = l
  const a = c * Math.cos((h * Math.PI) / 180)
  const b = c * Math.sin((h * Math.PI) / 180)

  // Convert Lab to XYZ
  const yLab = (lNorm + 16) / 116
  const xLab = a / 500 + yLab
  const zLab = yLab - b / 200

  const xNorm = xLab > 0.206897 ? Math.pow(xLab, 3) : (xLab - 16 / 116) / 7.787
  const yNorm = yLab > 0.206897 ? Math.pow(yLab, 3) : (yLab - 16 / 116) / 7.787
  const zNorm = zLab > 0.206897 ? Math.pow(zLab, 3) : (zLab - 16 / 116) / 7.787

  const x = xNorm * 0.95047
  const y = yNorm * 1.0
  const z = zNorm * 1.08883

  // Convert XYZ to RGB
  const rLinear = 3.2406 * x - 1.5372 * y - 0.4986 * z
  const gLinear = -0.9689 * x + 1.8758 * y + 0.0415 * z
  const bLinear = 0.0557 * x - 0.204 * y + 1.057 * z

  const r = rLinear <= 0.0031308 ? 12.92 * rLinear : 1.055 * Math.pow(rLinear, 1 / 2.4) - 0.055
  const g = gLinear <= 0.0031308 ? 12.92 * gLinear : 1.055 * Math.pow(gLinear, 1 / 2.4) - 0.055
  const b = bLinear <= 0.0031308 ? 12.92 * bLinear : 1.055 * Math.pow(bLinear, 1 / 2.4) - 0.055

  const rClamped = Math.max(0, Math.min(255, Math.round(r * 255)))
  const gClamped = Math.max(0, Math.min(255, Math.round(g * 255)))
  const bClamped = Math.max(0, Math.min(255, Math.round(b * 255)))

  // Convert RGB to HEX
  const rHex = rClamped.toString(16).padStart(2, '0')
  const gHex = gClamped.toString(16).padStart(2, '0')
  const bHex = bClamped.toString(16).padStart(2, '0')

  return `#${rHex}${gHex}${bHex}`.toUpperCase()
}

// Example usage:
// lchToHex(53.24, 104.55, 40.85) → '#FF0000'
// lchToHex(87.73, 119.78, 136.02) → '#00FF00'

Advanced LCH Manipulation

For more complex operations, here's a function to create perceptually uniform color variations:

function createLchVariations(l, c, h, variations = 5) {
  const colors = []
  for (let i = 0; i < variations; i++) {
    const factor = i / (variations - 1)
    const newL = l + (factor - 0.5) * 20 // Vary lightness by ±10
    const newC = c * (0.5 + factor * 0.5) // Vary chroma from 50% to 100%
    const newH = (h + i * 30) % 360 // Vary hue by 30 degrees each

    colors.push({
      l: Math.max(0, Math.min(100, Math.round(newL * 100) / 100)),
      c: Math.round(newC * 100) / 100,
      h: Math.round(newH * 100) / 100,
    })
  }
  return colors
}

HEX vs LCH: What's the Difference?

When to Choose HEX?

  • You're working with traditional web design workflows
  • You want human-readable color codes
  • You're creating color palettes for websites
  • You prefer simple, direct color representation
  • You're working with design tools and color pickers

When to Choose LCH?

  • You're working with modern CSS and want perceptual uniformity
  • You need consistent color transitions and animations
  • You're creating accessible designs with proper contrast
  • You want more predictable color relationships
  • You're working with design systems that need mathematical precision

Understanding the Fundamental Differences

FeatureHEX (Web-Friendly)LCH (Perceptual)
Format#FF0000lch(53.24, 104.55, 40.85)
Color SpaceRGB-basedPerceptually uniform
Human PerceptionIntuitive but unevenMathematically uniform
Color TransitionsUneven brightnessSmooth and consistent
AccessibilityGoodExcellent
Browser SupportUniversalModern browsers
Use CaseTraditional designModern, accessible design

Color and Range Limitations

  • HEX has uneven perceptual distribution across color values
  • LCH provides consistent perceptual steps across the entire color space
  • HEX color changes affect brightness perception unevenly
  • LCH color changes maintain consistent lightness perception
  • Both can represent the same colors but with different perceptual characteristics

Practical Examples

Examples of HEX to LCH Conversion

  • #FF0000lch(53.24, 104.55, 40.85) (red)
  • #00FF00lch(87.73, 119.78, 136.02) (green)
  • #0000FFlch(32.3, 133.81, 306.28) (blue)
  • #FFFFFFlch(100, 0, 0) (white)
  • #000000lch(0, 0, 0) (black)

Examples of LCH to HEX Conversion

  • lch(53.24, 104.55, 40.85)#FF0000 (red)
  • lch(87.73, 119.78, 136.02)#00FF00 (green)
  • lch(32.3, 133.81, 306.28)#0000FF (blue)
  • lch(100, 0, 0)#FFFFFF (white)
  • lch(0, 0, 0)#000000 (black)

Common Conversion Challenges

  • Complex mathematical transformations between color spaces
  • Precision loss during coordinate system conversions
  • Different perceptual characteristics between spaces
  • Browser compatibility considerations for LCH
  • Understanding the relationship between chroma and color intensity

Best Practices for Conversion

Features of HEX and LCH

HEX Features

  • Human-readable color representation
  • Web-friendly format for CSS and design tools
  • Easy to copy, paste, and share
  • Universal browser support
  • Direct RGB color control

LCH Features

  • Perceptually uniform color space for consistent transitions
  • Better accessibility and contrast calculations
  • Mathematical precision for color relationships
  • Smooth animations and color interpolations
  • Modern CSS support with advanced color features

Use-cases of HEX and LCH

HEX Use-cases

  • Traditional web design and CSS color specification
  • Design tools and color pickers
  • Color palette creation and sharing
  • Frontend development and styling
  • Cross-platform color communication

LCH Use-cases

  • Modern CSS with perceptual uniformity requirements
  • Design systems that need mathematical precision
  • Accessible design with proper contrast ratios
  • Smooth color animations and transitions
  • Advanced color manipulation and interpolation

Conclusion

In my experience, understanding HEX vs LCH: What's the Difference and When to Use Each? is crucial for modern web design. My recommendation? Use HEX when you're working with traditional design workflows, want human-readable color codes, or need universal browser support—it's familiar, accessible, and perfect for most web design tasks. Use LCH when you're working with modern CSS, need perceptual uniformity, or want mathematically precise color relationships—it's the future of color on the web and provides superior accessibility. The best approach is to understand both, use the right tool for the job, and always have reliable conversion tools at your fingertips. With these best practices, you'll be able to create more accessible and visually consistent designs than ever before.

Frequently Asked Questions

Q: Which format is better for web design?
A: It depends on your needs—HEX is more traditional and universally supported, while LCH provides better perceptual uniformity and accessibility.

Q: Can I use HEX and LCH in the same project?
A: Yes, you can convert between them, but each is optimized for different use cases and browser support.

Q: Is one format more accessible than the other?
A: LCH is more accessible because it provides perceptually uniform color spaces and better contrast calculations.

Q: Which format should I use for color animations?
A: Use LCH for color animations as it provides smooth, perceptually uniform transitions.

Q: Why is LCH considered more modern?
A: LCH is considered more modern because it's based on human visual perception and provides mathematical precision for color relationships.

Q: Where can I learn more about color formats?
A: Check out HEX vs RGB: What's the Difference and When to Use Each? and explore more color tools on ToolsChimp.