Construct typed colors

Create the intended color-space object directly when numeric channels already have an explicit meaning.

Turn a declared numeric payload into the intended object

When an API already identifies the space and channel scale, PHPColor can construct that value directly. The result carries its type, coordinates, alpha, and native CSS representation together.

Payload
display-p3
Channels
0.25 0.5 0.9
Result
DisplayP3Color

Name the space once and keep it attached to the value

The concrete class is the schema. Application code receives a typed object instead of passing three unlabeled numbers through the rest of the workflow.

<?php

use PhpColor\Color\DisplayP3Color;

$color = new DisplayP3Color(
    r: 0.25,
    g: 0.5,
    b: 0.9,
    a: 0.75,
);

echo $color->toCss();
// color(display-p3 0.25 0.5 0.9 / 0.75)
use PhpColor\Color\DisplayP3Color; $color = new DisplayP3Color( r: 0.25, g: 0.5, b: 0.9, a: 0.75, ); echo $color->toCss(); // color(display-p3 0.25 0.5 0.9 / 0.75)

Use every constructed color through one interface

sRGB, Oklch, and Display P3 objects expose the same conversion, formatting, channel, alpha, and immutable update contract. The original space remains inspectable.

SrgbColorColorInterface
OklchColorColorInterface
DisplayP3ColorColorInterface

Do not treat construction as uniform validation

Constructors normalize different coordinates according to their own class. For example, sRGB preserves supplied RGB channel numbers while alpha is clamped. Validate an external numeric payload against its declared space before construction.

<?php

use PhpColor\Color\Color;

$color = Color::rgb(1.2, -0.1, 0.5, 1.4);

var_export($color->getChannels());
// ['r' => 1.2, 'g' => -0.1, 'b' => 0.5]

echo $color->getAlpha();
// 1
use PhpColor\Color\Color; $color = Color::rgb(1.2, -0.1, 0.5, 1.4); var_export($color->getChannels()); // ['r' => 1.2, 'g' => -0.1, 'b' => 0.5] echo $color->getAlpha(); // 1

Continue

Keep numeric color data typed