Format concrete CSS colors

Choose the representation that crosses your application boundary and inspect the exact string PHPColor emits.

nativergb(128 110 174)
hslhsl(256.875 28.3186% 55.6863%)
color-srgbcolor(srgb 0.501961 0.431373 0.682353)
hex#806eae

One color, four deliberate representations

The value remains the same while syntax and precision change. Select the format for the receiving system instead of treating every CSS string as interchangeable.

Make the output choice visible in code

toCss() emits the object's native CSS notation. A format argument can request another representation when that concrete class supports it; toHex() performs an sRGB byte conversion.

<?php

use PhpColor\Color\Color;

$color = Color::parse('#806eae');

echo $color->toCss();
// rgb(128 110 174)

echo $color->toCss('hsl');
// hsl(256.875 28.3186% 55.6863%)

echo $color->toHex();
// #806eae
use PhpColor\Color\Color; $color = Color::parse('#806eae'); echo $color->toCss(); // rgb(128 110 174) echo $color->toCss('hsl'); // hsl(256.875 28.3186% 55.6863%) echo $color->toHex(); // #806eae

Formatting is not gamut mapping

Converting a wide-gamut color to sRGB may produce channels outside the usual range. Hexadecimal output clamps and quantizes them. Check the target gamut before choosing that representation.

Native CSSPreserves the object's declared space.
HexConverts through sRGB and rounds to bytes.
AlphaIncluded only when the selected method requests it.
PrecisionCSS channel values use finite decimal precision.

Choose the receiving format

Send one explicit color representation