Color converter

Convert color between the different formats (hex, rgb, hsl and css name)

Universal Color Space Converter & Contrast Engine: HEX, RGB, HSL, HSV, CMYK & CSS Color 4/5

1. Quick Overview & Key Benefits

Managing color systems across modern web applications, design systems, digital print workflows, and accessible user interfaces requires rigorous mathematical color science. The Universal Color Space Converter & Contrast Engine is an authoritative, browser-based transformation suite engineered for frontend architects, UI/UX design technologists, accessibility auditors, and graphics engineers. It seamlessly translates color coordinates across device-dependent color models (sRGB, HEX, HSL, HSV, CMYK) and next-generation device-independent wide-gamut spaces (Display P3, Oklab, Oklch, CIE Lab) conforming to the latest W3C CSS Color Module Level 4 and 5 specifications.

Key Benefits

  • Multi-Model Mathematical Translation: Convert interchangeably across 8+ formats: Hexadecimal (with 4- and 8-digit alpha channels), sRGB, RGBA, HSL, HSLA, HSV/HSB, CMYK, and modern CSS Color 4 functional syntaxes (oklch(), color(display-p3)).
  • Exact Gamma Companding & Linearization: Rigorous handling of the non-linear sRGB transfer function ($\gamma \approx 2.2$) to ensure true radiometric calculations when deriving photometric luminance.
  • Built-in WCAG 2.1 / 2.2 Relative Luminance & Contrast Auditing: Instantaneous calculation of relative luminance ($Y$) and contrast ratios against custom foreground/background pairs with pass/fail telemetry for WCAG Level AA, AAA, and Large Text specifications.
  • 100% Client-Side Privacy Guarantee: Color tokens, design palettes, brand assets, and contrast evaluations are processed strictly in local browser memory. Zero external API calls, zero tracking analytics, and zero remote logging ensure your unreleased brand identities and sensitive visual assets remain confidential.

2. Step-by-Step Practical Usage Guide

2.1 Bidirectional Transformation Workflow

Input any standard color string into the primary parser field:

  1. Paste or Type Color String: Supply any valid CSS notation:
    • Hexadecimal: #3B82F6 or #3b82f6cc (80% alpha)
    • sRGB: rgb(59, 130, 246) or rgb(59 130 246 / 0.8)
    • HSL: hsl(217deg, 91%, 60%)
    • HSV: hsv(217, 76%, 96%)
    • CMYK: cmyk(76%, 47%, 0%, 4%)
    • CSS Color 4: oklch(0.623 0.188 259.8)
  2. Inspect Normalized Matrix: The engine parses the token into an internal linear floating-point RGBA representation and updates all corresponding representations instantaneously.
  3. Copy Code Snippet: Click any generated value to copy standard CSS, Tailwind configuration tokens, Swift UIColor, Android Color.argb, or Flutter Color(0xFF...) code directly to your clipboard.

2.2 Practical Conversion Example Matrix

The following table demonstrates conversion results for a single primary brand blue token:

Color Model Notation / Value Alpha Channel Typical Context
HEX #3B82F6 None ($1.0$) Legacy CSS, Figma, SVG
HEX8 #3B82F6E6 $0.90$ ($90%$) Modern CSS, WebGL
sRGB rgb(59, 130, 246) Implicit $1.0$ HTML Canvas 2D, DOM styling
RGBA rgba(59, 130, 246, 0.9) Explicit $0.9$ Standard CSS3 styling
HSL hsl(217, 91.2%, 59.8%) Implicit $1.0$ Dynamic CSS theme variables
HSV / HSB hsv(217, 76.0%, 96.5%) Implicit $1.0$ Photoshop, Illustrator, UI Pickers
CMYK cmyk(76%, 47%, 0%, 4%) N/A Offset print prepress (ISO 12647)
Oklch (CSS 4) oklch(62.32% 0.188 259.81) Implicit $1.0$ Perceptually uniform design tokens

3. Technical Under the Hood: Specifications & Architecture

3.1 sRGB Gamma Correction and Companding Mechanics

Computer monitors expect gamma-encoded signals, while physical light addition operates in linear space. Standard sRGB color coordinates ($R_{8bit}, G_{8bit}, B_{8bit} \in [0, 255]$) are first normalized to $C_{\text{srgb}} \in [0, 1]$:

$C_{\text{srgb}} = \frac{C_{\text{8bit}}}{255}$

To translate non-linear sRGB into radiometric linear light values ($C_{\text{linear}}$) for color blending or luminance calculations, the engine executes the piecewise transfer function defined in IEC 61966-2-1:

$C_{\text{linear}} = \begin{cases} \frac{C_{\text{srgb}}}{12.92} & \text{if } C_{\text{srgb}} \le 0.04045 \ \left(\frac{C_{\text{srgb}} + 0.055}{1.055}\right)^{2.4} & \text{if } C_{\text{srgb}} > 0.04045 \end{cases}$

Conversely, when converting linear floating-point coordinates back into display sRGB:

$C_{\text{srgb}} = \begin{cases} 12.92 \times C_{\text{linear}} & \text{if } C_{\text{linear}} \le 0.0031308 \ 1.055 \times C_{\text{linear}}^{\frac{1}{2.4}} - 0.055 & \text{if } C_{\text{linear}} > 0.0031308 \end{cases}$

3.2 HSL and HSV Color Coordinate Math

Given normalized red, green, and blue coordinates $R, G, B \in [0, 1]$:

$V_{\text{max}} = \max(R, G, B), \quad V_{\text{min}} = \min(R, G, B), \quad \Delta = V_{\text{max}} - V_{\text{min}}$

Hue ($H \in [0^{\circ}, 360^{\circ})$):

$H = \begin{cases} 0^{\circ} & \text{if } \Delta = 0 \ 60^{\circ} \times \left(\frac{G - B}{\Delta} \bmod 6\right) & \text{if } V_{\text{max}} = R \ 60^{\circ} \times \left(\frac{B - R}{\Delta} + 2\right) & \text{if } V_{\text{max}} = G \ 60^{\circ} \times \left(\frac{R - G}{\Delta} + 4\right) & \text{if } V_{\text{max}} = B \end{cases}$

Lightness ($L$) and HSL Saturation ($S_{\text{hsl}}$):

$L = \frac{V_{\text{max}} + V_{\text{min}}}{2}$

$S_{\text{hsl}} = \begin{cases} 0 & \text{if } \Delta = 0 \ \frac{\Delta}{1 - |2L - 1|} & \text{if } \Delta \ne 0 \end{cases}$

Value ($V$) and HSV Saturation ($S_{\text{hsv}}$):

$V = V_{\text{max}}, \quad S_{\text{hsv}} = \begin{cases} 0 & \text{if } V_{\text{max}} = 0 \ \frac{\Delta}{V_{\text{max}}} & \text{if } V_{\text{max}} > 0 \end{cases}$

3.3 Subtractive CMYK Transformation

Standard conversion from additive RGB to subtractive CMYK assumes ideal inks over an idealized substrate:

$K = 1 - \max(R, G, B)$

If $K = 1$, then $C = 0, M = 0, Y = 0$. Otherwise:

$C = \frac{1 - R - K}{1 - K}, \quad M = \frac{1 - G - K}{1 - K}, \quad Y = \frac{1 - B - K}{1 - K}$

3.4 WCAG 2.1 Contrast Ratio Algorithm

Under W3C WCAG 2.1 Recommendation §1.4.3, the relative luminance $Y$ of any sRGB color is determined using the ITU-R Recommendation BT.709 coefficients applied to linearized color components:

$Y = 0.2126 \times R_{\text{linear}} + 0.7152 \times G_{\text{linear}} + 0.0722 \times B_{\text{linear}}$

The contrast ratio between two colors with relative luminances $L_1$ and $L_2$ (where $L_1 > L_2$) is:

$\text{Contrast Ratio} = \frac{L_1 + 0.05}{L_2 + 0.05}$

The resulting ratio ranges from $1:1$ (identical colors) to $21:1$ (pure black #000000 vs pure white #FFFFFF).

3.5 Core TypeScript Parsing and Conversion Implementation

// color-converter-core.ts

export interface RGBA {
  r: number; // 0 to 255
  g: number; // 0 to 255
  b: number; // 0 to 255
  a: number; // 0 to 1
}

export interface HSL {
  h: number; // 0 to 360
  s: number; // 0 to 100
  l: number; // 0 to 100
  a: number; // 0 to 1
}

export interface CMYK {
  c: number; // 0 to 100
  m: number; // 0 to 100
  y: number; // 0 to 100
  k: number; // 0 to 100
}

export class ColorConverter {
  /**
   * Linearize an 8-bit sRGB channel per IEC 61966-2-1.
   */
  public static linearizeChannel(val8Bit: number): number {
    const c = Math.min(255, Math.max(0, val8Bit)) / 255;
    return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
  }

  /**
   * Calculate Relative Luminance per WCAG 2.1 definition.
   */
  public static calculateRelativeLuminance(rgba: RGBA): number {
    const rLin = this.linearizeChannel(rgba.r);
    const gLin = this.linearizeChannel(rgba.g);
    const bLin = this.linearizeChannel(rgba.b);
    return 0.2126 * rLin + 0.7152 * gLin + 0.0722 * bLin;
  }

  /**
   * Compute WCAG Contrast Ratio between two RGBA colors.
   */
  public static getContrastRatio(fg: RGBA, bg: RGBA): number {
    const lum1 = this.calculateRelativeLuminance(fg);
    const lum2 = this.calculateRelativeLuminance(bg);
    const lighter = Math.max(lum1, lum2);
    const darker = Math.min(lum1, lum2);
    return (lighter + 0.05) / (darker + 0.05);
  }

  /**
   * Convert RGBA to 6-digit or 8-digit Hex string.
   */
  public static rgbaToHex(rgba: RGBA): string {
    const toHex = (n: number) => Math.round(n).toString(16).padStart(2, '0');
    const hexRGB = `#${toHex(rgba.r)}${toHex(rgba.g)}${toHex(rgba.b)}`.toUpperCase();
    if (rgba.a < 1) {
      const alphaHex = Math.round(rgba.a * 255).toString(16).padStart(2, '0').toUpperCase();
      return `${hexRGB}${alphaHex}`;
    }
    return hexRGB;
  }

  /**
   * Convert RGBA to HSL representation.
   */
  public static rgbaToHsl(rgba: RGBA): HSL {
    const r = rgba.r / 255;
    const g = rgba.g / 255;
    const b = rgba.b / 255;

    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    const delta = max - min;

    let h = 0;
    let s = 0;
    const l = (max + min) / 2;

    if (delta !== 0) {
      s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
      switch (max) {
        case r: h = ((g - b) / delta + (g < b ? 6 : 0)); break;
        case g: h = ((b - r) / delta + 2); break;
        case b: h = ((r - g) / delta + 4); break;
      }
      h = Math.round(h * 60);
    }

    return {
      h: (h + 360) % 360,
      s: Math.round(s * 1000) / 10,
      l: Math.round(l * 1000) / 10,
      a: rgba.a,
    };
  }

  /**
   * Convert RGBA to subtractive CMYK.
   */
  public static rgbaToCmyk(rgba: RGBA): CMYK {
    const r = rgba.r / 255;
    const g = rgba.g / 255;
    const b = rgba.b / 255;

    const k = 1 - Math.max(r, g, b);
    if (k === 1) {
      return { c: 0, m: 0, y: 0, k: 100 };
    }

    const c = (1 - r - k) / (1 - k);
    const m = (1 - g - k) / (1 - k);
    const y = (1 - b - k) / (1 - k);

    return {
      c: Math.round(c * 100),
      m: Math.round(m * 100),
      y: Math.round(y * 100),
      k: Math.round(k * 100),
    };
  }
}

4. Real-World Production Use Cases

4.1 Enterprise Design Systems & CSS Variable Tokenization

Design systems engineering teams (e.g., maintaining corporate style guides across React and Web Components) need to convert hex tokens into HSL and modern oklch() values. HSL enables automated runtime programmatic theme shifting (light/dark mode palette generations) by manipulating the lightness and saturation axes independently:

:root {
  --primary-h: 217;
  --primary-s: 91%;
  --primary-l: 60%;
  --color-primary: hsl(var(--primary-h), var(--primary-s), var(--primary-l));
  --color-primary-hover: hsl(var(--primary-h), var(--primary-s), calc(var(--primary-l) - 10%));
}

4.2 Automated CI/CD Web Accessibility (WCAG) Gatekeeping

DevOps pipelines running end-to-end design audits utilize the linearized relative luminance formulas to validate contrast ratios for button components, modal alerts, and text layers. If a developer attempts to commit button styling with a contrast ratio below $4.5:1$ for body text (or $3.0:1$ for large text), the CI gate halts execution to prevent regulatory ADA (Americans with Disabilities Act) and European Accessibility Act (EAA) compliance infractions.

4.3 Omnichannel Brand Publishing: Digital to Print Prepress

Brand agencies producing cross-media campaigns must verify that digital screen assets (sRGB / HEX) translate accurately to 4-color offset printing presses without out-of-gamut clipping. By validating CMYK output approximations and total ink limit (Total Area Coverage / TAC), graphic production leads detect mudding and chromatic clipping before commissioning costly physical print runs.


5. Frequently Asked Questions (FAQs)

What is the difference between HSL and HSV/HSB?

While both models represent color cylindrical coordinates, their definition of lightness/value differs. In HSL, Lightness ($L$) ranges from black ($0%$) to the pure color hue ($50%$) to pure white ($100%$). In HSV (Hue, Saturation, Value, also known as HSB in Adobe products), Value ($V$) represents brightness: $0%$ is pure black, while $100%$ represents the brightest vivid shade of the color without shifting towards white.

Why does a simple linear average of RGB channels fail for WCAG contrast?

Human visual perception does not perceive photon wavelengths with equal sensitivity. Photoreceptive cone cells in the human retina are overwhelmingly more sensitive to green wavelengths ($\sim 555\text{ nm}$) than to blue wavelengths. The ITU-R BT.709 standard accounts for human photopic vision by weighting green at $71.52%$, red at $21.26%$, and blue at only $7.22%$. Applying an unweighted arithmetic mean would cause severe accessibility auditing errors.

How does CSS Color Module Level 4 handle wide-gamut displays?

Standard sRGB covers only about $35%$ of the visible CIE 1931 chromaticity spectrum. Modern mobile screens and displays (Apple Retina, OLED TVs) natively support the Display P3 color space, which provides roughly $50%$ more chromatic volume. CSS Color 4 introduces color(display-p3 r g b) and oklch(L C H) syntaxes, allowing web applications to render vivid neon greens, deep oranges, and vibrant magentas previously clipped by standard 8-bit sRGB gamut limiters.

Can CMYK accurately reproduce all colors visible on a monitor?

No. sRGB is an additive color space (light is emitted), while CMYK is a subtractive space (pigments absorb reflected light). Many vivid screen colors—such as intense electric cyan, bright lime green, or deep saturated purples—cannot be reproduced using standard cyan, magenta, yellow, and black pigments without specialized spot inks (Pantone Matching System).


6. Technical Accuracy & Client-Side Privacy Notice

Standards Compliance

  • IEC 61966-2-1:1999: Official sRGB color space definition and non-linear electro-optical companding curves.
  • W3C CSS Color Module Level 4 & 5: Syntax conventions for modern functional color representations, including rgb(), hsl(), hwb(), lab(), and oklch().
  • W3C Web Content Accessibility Guidelines (WCAG) 2.1 / 2.2: Recommendation §1.4.3 (Contrast Minimum Level AA) and §1.4.6 (Contrast Enhanced Level AAA).
  • ITU-R Recommendation BT.709-6: Parameter values for the HDTV standard and relative luminance coefficients.

Zero-Telemetry Privacy Guarantee

All color manipulations, gamut conversions, contrast computations, and palette exports execute 100% locally within your browser sandbox. No hex codes, image uploads, design system tokens, or interaction payloads are transmitted across external networks. The tool operates with complete offline fidelity.