Convert colors to grayscale
Remove perceptual chroma while retaining the color's lightness and alpha.
Source
OKLCH chroma 0.247
#7c3aed
Grayscale
OKLCH chroma 0
#6f6f6f
Source unchanged
immutable
#7c3aed
Remove chroma in a perceptual space
PHPColor converts through OKLCH, sets chroma to zero, then returns the result in the source color type.
Create an achromatic counterpart
grayscale() returns a new object. The source remains available for other states or comparisons.
<?php
use PhpColor\Color\Color;
$source = Color::parse('#7c3aed');
$gray = $source->grayscale();
printf("%s\n%s\n", $source->toHex(), $gray->toHex());
// #7c3aed
// #6f6f6f
use PhpColor\Color\Color;
$source = Color::parse('#7c3aed');
$gray = $source->grayscale();
printf("%s\n%s\n", $source->toHex(), $gray->toHex());
// #7c3aed
// #6f6f6f
Grayscale is a color transform
The operation preserves OKLCH lightness, not a particular encoded RGB average or a contrast requirement.
LightnessThe OKLCH lightness coordinate is retained.
ChromaThe OKLCH chroma coordinate becomes zero.
AlphaThe existing alpha channel is retained.
ContrastThe result still needs checking against its actual background.