Precision and gamut

Understand output precision and wide-gamut web color formats.

Choose the representation from the consumer backward. Native CSS retains the current space and decimal coordinates; hexadecimal and rgb() trade that information for conventional sRGB output.

Understand output precision

Native decimal CSS formatters use up to six digits after the decimal point and remove trailing zeroes. sRGB rgb() formatting instead rounds channels to integers. toHex() also rounds channels to eight-bit values.

Choose output based on the consumer:

  • use native toCss() output to retain the object's space and decimal coordinates;
  • use rgb() or hexadecimal output for conventional sRGB serialization;
  • use getChannels() when another numeric process should control rounding.

Do not use a formatted string as a lossless persistence format. Conversion, gamut boundaries, decimal formatting, and eight-bit serialization can all affect round trips.

Use sRGB and Display P3 for web output

sRGB is the safest default for conventional CSS output and hexadecimal values:

use PhpColor\Color\Color;

$color = Color::parse('oklch(0.65 0.18 264)');
$srgb = $color->to('srgb');

echo $srgb->toCss();
echo $srgb->toHex();
use PhpColor\Color\Color; $color = Color::parse('oklch(0.65 0.18 264)'); $srgb = $color->to('srgb'); echo $srgb->toCss(); echo $srgb->toHex();

toCss() on the converted color emits rgb(...). toHex() also converts through sRGB, clamps each channel to the byte range, rounds it, and emits lowercase hexadecimal.

Use Display P3 when the consumer understands wide-gamut CSS:

use PhpColor\Color\Color;

$p3 = Color::parse('#3b82f6')->to('display-p3');

echo $p3->toCss();
use PhpColor\Color\Color; $p3 = Color::parse('#3b82f6')->to('display-p3'); echo $p3->toCss();

The native output is a color(display-p3 ...) expression. Conversion does not provide a browser fallback automatically. If you publish Display P3 CSS, provide any sRGB fallback required by your application separately.

Verify after serialization

The internal color object is not always the value another system receives. When a tolerance, contrast threshold, or brand comparison matters:

  1. serialize in the intended delivery format;
  2. parse that exact string again;
  3. repeat the material gamut, distance, or contrast check on the restored color.

This catches byte rounding, decimal rounding, and sRGB clipping at the same boundary the application will ship.

PHPColor checks whether a color is in a named RGB gamut and can measure overflow. It does not perform a general perceptual gamut-mapping strategy or create CSS fallback declarations automatically.

Continue with Choose a normalization boundary, Preserve Display P3 in a stored CSS token, or Check color gamuts.