Supported concrete color input

Identify the concrete CSS color values PHPColor can parse, the typed object each syntax produces, and the boundary with contextual CSS.

Use Color::parse() when a string contains every value needed to describe a color. PHPColor returns a concrete color object immediately and keeps the input space when it has a corresponding class.

Follow the input from syntax to object

A concrete color string carries both coordinates and the notation needed to interpret them. Parsing turns that text into a ColorInterface implementation:

use PhpColor\Color\Color;

$inputs = [
    '#3b82f6',
    'hsl(217 91% 60%)',
    'oklch(0.65 0.18 264)',
    'color(display-p3 0.82 0.23 0.18)',
    'rebeccapurple',
];

foreach ($inputs as $input) {
    $color = Color::parse($input);
    echo $color::getSpaceName().PHP_EOL;
}
use PhpColor\Color\Color; $inputs = [ '#3b82f6', 'hsl(217 91% 60%)', 'oklch(0.65 0.18 264)', 'color(display-p3 0.82 0.23 0.18)', 'rebeccapurple', ]; foreach ($inputs as $input) { $color = Color::parse($input); echo $color::getSpaceName().PHP_EOL; }

The output is:

srgb
srgb
oklch
display-p3
srgb
srgb srgb oklch display-p3 srgb

Hexadecimal, RGB, HSL, and named colors describe sRGB values, so they produce SrgbColor objects. Functions such as oklch() and color(display-p3 ...) produce objects in their named spaces.

Know the supported concrete families

PHPColor 1.0 parses these concrete input families:

Input family Examples Resulting space
Hexadecimal #rgb, #rgba, #rrggbb, #rrggbbaa sRGB
Named CSS colors red, rebeccapurple, steelblue sRGB
RGB and HSL rgb(), rgba(), hsl(), hsla() sRGB
Other CSS functions hwb(), lab(), lch(), oklab(), oklch(), device-cmyk() The function's space
color() sRGB, linear sRGB, Display P3, XYZ D65, Rec. 2020, ProPhoto RGB, A98 RGB The named space
Self-contained relative colors A relative function whose origin and channel expressions are concrete The target space

The parser accepts tested modern space-separated RGB and HSL forms as well as legacy comma-separated forms. Alpha can be expressed as a number or percentage where the notation supports it.

This matrix describes parsing support, not every CSS color construct. A custom property or currentColor does not contain enough information to resolve a color by itself.

Keep parsing separate from formatting

Parsing identifies the color represented by the input. It does not preserve the original spelling as provenance. Two strings can therefore produce the same concrete object:

use PhpColor\Color\Color;

$named = Color::parse('blue');
$hex = Color::parse('#0000ff');

echo $named->toHex().PHP_EOL;
echo $hex->toHex().PHP_EOL;
use PhpColor\Color\Color; $named = Color::parse('blue'); $hex = Color::parse('#0000ff'); echo $named->toHex().PHP_EOL; echo $hex->toHex().PHP_EOL;

Both lines output #0000ff. PHPColor does not expose a public detector that reports whether the caller originally used a name, hexadecimal, or another equivalent notation.

Choose the required representation when the color leaves your application. Format CSS colors explains native CSS output, while Choose a normalization boundary covers storage and exchange decisions.

Separate concrete input from contextual CSS

Color::parse() can evaluate a relative color when its origin and expressions are self-contained. It cannot resolve a value that depends on a custom property, the current foreground color, or the active color scheme.

Use Concrete and deferred CSS colors for var(), currentColor, and light-dark(). Use Choose text or channel input next when your application already has numeric coordinates rather than CSS text.

Continue