Converters

Color Converter Guide: HEX, RGB, HSL, and Modern OKLCH in CSS

A complete guide to digital color models: HEX, RGB, HSL, and modern OKLCH in CSS Color Module Level 4. Includes WCAG contrast accessibility formulas.

WebUtil Team

1. The Physics and Math Behind Digital Color Models

Every color displayed on an OLED monitor, phone screen, or laptop display is created by emitting light from microscopic red, green, and blue subpixels. In digital computing, various color spaces and notation models have been created to translate human visual perception into mathematical coordinates.

Understanding how HEX, RGB, HSL, and next-generation OKLCH color models function allows frontend developers and UI designers to build accessible themes, design systems, and color palette generators that render consistently across all devices.

2. HEX Codes: 3-Digit, 6-Digit, and 8-Digit Alpha Channels

HEX color codes are base-16 representations of RGB intensity, prefixed by a hash symbol (#). Each pair of hexadecimal digits (00 to FF in hex, 0 to 255 in decimal) defines the intensity of the red, green, and blue channels:

  • 6-Digit Syntax: #RRGGBB. For example, #5C7CFA represents 92 Red, 124 Green, and 250 Blue.
  • 3-Digit Shorthand: #RGB expands by duplicating each digit (#F0A becomes #FF00AA).
  • 8-Digit Alpha Syntax: #RRGGBBAA. The final two hex digits specify the alpha opacity (e.g. #00000080 represents 50% transparent black, where 0x80 is 128/255).

3. RGB & RGBA: Additive Light Color Synthesis

The RGB model is the native color coordinate system of digital hardware. Intensities are specified as integers from 0 to 255 or percentages from 0% to 100%:

```css /* CSS Color Module Level 3 */ color: rgba(92, 124, 250, 0.8);

/* Modern CSS Color Module Level 4 syntax (no comma, forward-slash alpha) */ color: rgb(92 124 250 / 80%); ```

When all three channels are 0, no light is emitted (pure black). When all three channels are 255, maximum light is combined (pure white).

Sponsored
Advertisement

4. HSL & HSLA: Cylindrical Coordinates for Intuitive Palettes

While RGB is intuitive for computers, it is notoriously counter-intuitive for humans. If a designer asks you to make a button 'slightly lighter or less saturated', modifying RGB values requires recalculating all three channels simultaneously. HSL solves this by using a cylindrical coordinate geometry:

  • Hue (H): An angle from 0 to 360 degrees on the color wheel (0° = Red, 120° = Green, 240° = Blue).
  • Saturation (S): A percentage from 0% (monochrome gray) to 100% (vibrant, pure pigment).
  • Lightness (L): A percentage from 0% (pure black) through 50% (normal color) to 100% (pure white).

HSL makes generating UI state variations (hover states, active borders, disabled states) effortless: simply increment or decrement the lightness channel by 5% or 10%.

5. CSS Color Module Level 4: Why OKLCH is the Future of Web UI

Despite HSL's popularity, it suffers from a major mathematical flaw: it is not 'perceptually uniform'. In HSL, pure blue (hsl(240, 100%, 50%)) and pure yellow (hsl(60, 100%, 50%)) both have 50% lightness, yet the human eye perceives yellow as significantly brighter than blue!

The new W3C CSS Color Module Level 4 introduces `oklch()`, based on the OKLab color space designed by Björn Ottosson:


              /* OKLCH: Lightness (0-1), Chroma (saturation), Hue (0-360) */
background-color: oklch(0.65 0.22 260);
            

In OKLCH, two colors with the same lightness value are guaranteed to have identical perceived brightness to human eyes, making automated accessible color palette generation truly reliable.

6. Web Accessibility (WCAG 2.1): Calculating Contrast Ratios

The Web Content Accessibility Guidelines (WCAG 2.1) require text to maintain sufficient contrast against its background:

  • Level AA: Minimum contrast ratio of 4.5:1 for standard body text, and 3:1 for large headings (18pt+ or 14pt bold).
  • Level AAA: Minimum contrast ratio of 7:1 for normal text, and 4.5:1 for large text.

Relative luminance is calculated using standardized sRGB coefficients: L = 0.2126 * R + 0.7152 * G + 0.0722 * B. Always test your color palettes against these standards to ensure your site is readable for users with low vision or color blindness.

7. JavaScript Algorithms for Color Format Conversions

WebUtil's Color Converter tool allows you to convert seamlessly between HEX, RGB, and HSL formats in real-time with automatic WCAG contrast calculation. All conversions execute locally in your browser with zero latency.