Search by

sunasty / color-math

bsulyan

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

v1.0.1 2026-10-24 11:10 UTC

This package is auto-updated.

Last update: 2026-11-08 04:27:23 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 WCAG contrast
$ratio = ColorMath::contrastRatio($teal, $white);
$wcag = ColorMath::checkWcag($ratio);

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

Related Tools

License

MIT © Bastien Sulyan