Methods & References

This page documents the scientific methods, algorithms, and constants used in hexcode.info for color calculations and simulations.

Color Space Conversions

sRGB to XYZ Matrix (D65 Illuminant)

\[ \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} = \begin{bmatrix} 0.4124564 & 0.3575761 & 0.1804375 \\ 0.2126729 & 0.7151522 & 0.0721750 \\ 0.0193339 & 0.1191920 & 0.9503041 \end{bmatrix} \begin{bmatrix} R \\ G \\ B \end{bmatrix} \]

Standard sRGB to CIE XYZ transformation matrix using D65 standard illuminant (6504K). Input RGB values are linearized using sRGB gamma correction before transformation.

Gamma Correction

\[ \operatorname{Linear}(u) = \begin{cases} \dfrac{u}{12.92}, & u \le 0.04045 \\ \left(\dfrac{u + 0.055}{1.055}\right)^{2.4}, & u > 0.04045 \end{cases} \]

\[ \operatorname{sRGB}(u) = \begin{cases} 12.92u, & u \le 0.0031308 \\ 1.055u^{1/2.4} - 0.055, & u > 0.0031308 \end{cases} \]

Standard sRGB gamma correction functions for linearization and encoding.

OKLab Implementation

OKLab is a perceptually uniform color space designed for image processing applications. Our implementation follows the original specification by Björn Ottosson.

Reference: Ottosson, B. (2020). “A perceptual color space for image processing”
Implementation: src/utils/oklab.ts

Color Difference Calculations

ΔE00 (CIEDE2000)

Industry-standard color difference calculation using the CIEDE2000 formula. Provides perceptually uniform color difference measurements.

Standard: CIE 142-2001 (ISO 11664-6:2014)
Implementation: src/utils/colorMath.ts:315

Colorblindness Simulation

Machado, Brettel & Viénot Method

Physiologically accurate color vision deficiency simulation using LMS cone response matrices. Supports protanopia, deuteranopia, tritanopia, and their anomalous variants.

Reference: Machado, G.M., Oliveira, M.M., Fernandes, L.A.F. (2009). “A Physiologically-based Model for Simulation of Color Vision Deficiency”
Implementation: src/utils/colorMath.ts:437

Contrast Calculations

WCAG 2.2 Contrast Ratio

Contrast = (L₁ + 0.05) / (L₂ + 0.05)
where L₁ is the lighter and L₂ is the darker relative luminance

Standard WCAG contrast calculation using relative luminance (Y component in XYZ).

Standard: WCAG 2.2 Success Criteria 1.4.3 & 1.4.6
Implementation: src/utils/colorMath.ts:389

APCA (Advanced Perceptual Contrast Algorithm)

Modern perceptual contrast algorithm designed for digital displays. Provides more accurate readability predictions than WCAG contrast ratios.

Reference: Somers, A. (2022). “APCA Readability Criterion”
Implementation: src/utils/apca.ts

Constants & Standards

CIE Standard Illuminant D65

Chromaticity: x = 0.31272, y = 0.32903
White point (XYZ): 0.95047, 1.0, 1.08883
Color temperature: 6504K

ΔE Thresholds

Just noticeable: ΔE ≤ 1.0
Perceptible: 1.0 < ΔE ≤ 3.0
Acceptable: 3.0 < ΔE ≤ 6.0
Different colors: ΔE > 6.0

Source Code

All calculations are implemented in TypeScript with comprehensive test coverage. The source code is available in the following modules:

  • src/utils/colorMath.ts - Core color space conversions and calculations
  • src/utils/oklab.ts - OKLab color space implementation
  • src/utils/apca.ts - APCA contrast calculations
  • src/__tests__/colorMath.spec.ts - Comprehensive test suite

Validation & Testing

All color calculations are validated against reference implementations and established test datasets. The test suite includes round-trip conversions, edge cases, and comparison with industry standards.

Test Framework: Vitest with comprehensive color math validation
Coverage: Matrix transformations, gamma correction, color difference calculations
Methods & References | hexcode.info