Search by

sunasty / color-math

bsulyan

Zero-dependency sRGB, OKLCH, and WCAG 2.1 contrast calculation in PHP

v1.0.2 2026-11-29 21:15 UTC

This package is auto-updated.

Last update: 2026-11-30 20:01:08 UTC


README

Zero-dependency PHP library for sRGB, OKLCH, relative luminance, and WCAG 2.1 accessibility contrast calculation.

Originally designed for the Sunasty Color Picker client-side tool and the Sunasty Developer Tools.

Installation

composer require sunasty/color-math

Quick Start

use Sunasty\ColorMath\ColorMath;

// Parse hex colors
$teal = ColorMath::hexToRgb('#0F766E');
$white = [255, 255, 255];

// Compute OKLCH & WCAG contrast
$oklch = ColorMath::rgbToOklch($teal[0], $teal[1], $teal[2]);
$ratio = ColorMath::contrastRatio($teal, $white);
$wcag = ColorMath::checkWcag($ratio);

echo "OKLCH: oklch({$oklch['l']} {$oklch['c']} {$oklch['h']})\n";
echo "Contrast: {$ratio}:1\n";
echo "Passes AA: " . ($wcag['aa_normal'] ? 'Yes' : 'No') . "\n";

Documentation & Related Tools

License

MIT © Bastien Sulyan