Calculate contrast ratios of colors.

v1.0.1 2019-01-23 20:17 UTC

This package is auto-updated.

Last update: 2023-05-28 00:42:31 UTC


Accessibility library that calculates contrast ratios of colors as well as rates the contrast ratio against WCAG standards.

Getting started

  1. PHP 7.1.x is required
  2. Install the Contrast Ratio Calculator using Composer (recommended) or manually

Composer Installation

  1. Get Composer
  2. Install with composer require harmstyler/contrast-ratio-calculator
  3. Add the following to your application's main PHP file: require 'vendor/autoload.php';



use HarmsTyler\ContrastRatioCalculator\Color;
use HarmsTyler\ContrastRatioCalculator\ContrastRatio;
use HarmsTyler\ContrastRatioCalculator\WCAGContrastRating;

$primaryColor = new Color();
$secondaryColor = new Color();

$contrastRatio = new ContrastRatio($primaryColor, $secondaryColor);

echo $contrastRatio->getRatio(); // floating decimal point of calculated ratio

$rating = new WCAGContrastRating();
echo $rating->rateContrastRatio($contrastRatio); // the WCAGContrast grade, either 'fail', 'aa-large', 'aa', or 'aaa'

Run the tests

./vendor/bin/phpunit tests/