Measure APCA contrast

Keep lightness contrast and text polarity visible when comparing a foreground with its background.

Dark on light

Polarity changes the signed APCA result.

#1e293b on #f8fafc+98.25 Lcnormal polarity

Light on dark

Polarity changes the signed APCA result.

#f8fafc on #1e293b-100.79 Lcreverse polarity

The sign records which color is the text

Swapping the same two colors changes more than the sign: this implementation returns 98.248081344886799 and -100.787598651287084.

Pass foreground and background in reading order

ApcaContrast::lc() accepts the foreground first. A positive result represents dark text on a lighter background; a negative result represents the reverse polarity.

<?php

use PhpColor\Color\Color;
use PhpColor\Color\Contrast\ApcaContrast;

$text = Color::parse('#1e293b');
$surface = Color::parse('#f8fafc');

echo ApcaContrast::lc($text, $surface);
// 98.248081344886799

echo ApcaContrast::lc($surface, $text);
// -100.787598651287084
use PhpColor\Color\Color; use PhpColor\Color\Contrast\ApcaContrast; $text = Color::parse('#1e293b'); $surface = Color::parse('#f8fafc'); echo ApcaContrast::lc($text, $surface); // 98.248081344886799 echo ApcaContrast::lc($surface, $text); // -100.787598651287084

Keep measurement separate from typography policy

PHPColor returns the signed Lc value. It does not select the typography requirements for the finished interface or turn the measurement into a universal pass or fail verdict.

PositiveDark foreground on light background.
NegativeLight foreground on dark background.
ZeroNo meaningful lightness contrast.
Not suppliedTypography-dependent pass or fail policy.

Keep polarity explicit

Measure the colors in their real roles