Inspect a color in several spaces

Build a compact report that keeps delivery values, perceptual coordinates, and luminance distinct.

Keep delivery values, perceptual coordinates, and luminance in separate fields. They answer different questions about the same color.

Build a compact report

Convert explicitly before reading a space-specific channel:

use PhpColor\Color\Color;

$color = Color::parse('#7c3aed');
$oklch = $color->to('oklch');

$report = [
    'css' => $color->toCss(),
    'hex' => $color->toHex(),
    'oklch' => $oklch->getChannels(),
    'display_p3' => $color->to('display-p3')->getChannels(),
    'luminance' => $color->getLuminance(),
    'alpha' => $color->getAlpha(),
];

assert(['l', 'c', 'h'] === array_keys($report['oklch']));
use PhpColor\Color\Color; $color = Color::parse('#7c3aed'); $oklch = $color->to('oklch'); $report = [ 'css' => $color->toCss(), 'hex' => $color->toHex(), 'oklch' => $oklch->getChannels(), 'display_p3' => $color->to('display-p3')->getChannels(), 'luminance' => $color->getLuminance(), 'alpha' => $color->getAlpha(), ]; assert(['l', 'c', 'h'] === array_keys($report['oklch']));

Format comparable output

Use native CSS for a quick, human-readable view of each coordinate system. Do not compare values across these fields directly: OKLCH lightness is a perceptual coordinate; luminance is the quantity used by WCAG contrast; RGB channels describe a destination encoding.

foreach (['srgb', 'oklch', 'display-p3'] as $space) {
    $converted = $color->to($space);

    printf("%-12s %s\n", $space, $converted->toCss());
}
foreach (['srgb', 'oklch', 'display-p3'] as $space) { $converted = $color->to($space); printf("%-12s %s\n", $space, $converted->toCss()); }

Continue with read color channels, choose a color space, or measure WCAG contrast.