Compare exact color equality

Test representation identity without substituting perceptual similarity.

#ff0000 vs rgb(255 0 0) equal
sRGB red vs converted Lab red not equal
red vs its hex round trip equal

Make representation part of the question

equals() requires the same color space, the same alpha, and the same serialized channel representation. Visually matching colors in different spaces are not exact equals.

Compare identity deliberately

Normalize both inputs to the same space and representation first when that is the equality policy your application needs.

<?php

use PhpColor\Color\Color;

$hex = Color::parse('#ff0000');
$rgb = Color::parse('rgb(255 0 0)');
$lab = $hex->to('lab');

var_dump($hex->equals($rgb));
var_dump($hex->equals($lab));
var_dump($hex->equals(Color::parse($hex->toHex())));

// true
// false
// true
use PhpColor\Color\Color; $hex = Color::parse('#ff0000'); $rgb = Color::parse('rgb(255 0 0)'); $lab = $hex->to('lab'); var_dump($hex->equals($rgb)); var_dump($hex->equals($lab)); var_dump($hex->equals(Color::parse($hex->toHex()))); // true // false // true

Exact equality is not perceptual distance

A false equality result does not say how different two colors appear. A true result only proves the representation contract used by equals().

SpaceDifferent color-space types are not equal.
AlphaDifferent alpha values are not equal.
ChannelsThe serialized channel representation must match.
SimilarityUse a distance calculation for perceptual difference.

Choose a next step

Choose exact identity or perceptual difference