A color in PHPColor is a set of channel values in a color space. You can build one from values you already have, or parse one from a CSS string. Both paths return the same immutable objects, so nothing downstream cares which one you took.
Construct
When your code already knows the numbers, build the color directly. Every factory returns the concrete class of its space, ready to convert, transform, or format.
Space factories
Give channel values to the space you think in. sRGB channels are normalized from 0 to 1; OKLCH takes lightness, chroma, and hue in degrees:
use PhpColor\Color\Color;
$sky = Color::rgb(0.231, 0.510, 0.965);
$brand = Color::oklch(0.62, 0.19, 260);
use PhpColor\Color\Color;
$sky = Color::rgb(0.231, 0.510, 0.965);
$brand = Color::oklch(0.62, 0.19, 260);
The two variables hold different classes, SrgbColor and OklchColor, behind the same interface. That difference stops mattering the moment you convert or format.
Named constructors
Twelve everyday colors ship as ready-made constructors: the primaries and secondaries, plus white(), black(), gray(), orange(), purple(), and teal(). They make convenient anchors for a quick mix or a contrast check:
$white = Color::white();
$teal = Color::teal();
$red = Color::red();
$white = Color::white();
$teal = Color::teal();
$red = Color::red();
Hex values
Color::hex() is the explicit constructor for hexadecimal input, long or short form. Unlike parse(), it throws on anything else, which documents intent when hex is the only format you should ever receive:
$blue = Color::hex('#3b82f6');
$short = Color::hex('#38f');
$blue = Color::hex('#3b82f6');
$short = Color::hex('#38f');
From string
Strings are how colors arrive from the outside world: design tokens, configuration, user input. Color::parse() turns supported, self-contained CSS color values into the same objects as the factories above. Contextual values such as var() and currentColor need a resolution context instead.
Names
The full set of CSS named colors is understood, handy when tokens arrive as plain words:
$purple = Color::parse('rebeccapurple');
$purple = Color::parse('rebeccapurple');
Values
Hexadecimal strings parse in every CSS form, from three to eight digits, alpha included:
$blue = Color::parse('#3b82f6');
$faded = Color::parse('#3b82f680');
$blue = Color::parse('#3b82f6');
$faded = Color::parse('#3b82f680');
CSS expressions
Functional syntaxes parse too, modern space-separated forms as well as legacy commas:
$a = Color::parse('rgb(59 130 246)');
$b = Color::parse('hsl(217deg 91% 60%)');
$c = Color::parse('oklch(0.62 0.19 260)');
$a = Color::parse('rgb(59 130 246)');
$b = Color::parse('hsl(217deg 91% 60%)');
$c = Color::parse('oklch(0.62 0.19 260)');
Three of those four are the same blue arriving through three different doors. That is the point of parse(): supported concrete syntax can cross one application boundary and become a typed color object.
For user input, prefer Color::tryFrom(): it returns null on anything that is not a color instead of throwing, so a form can simply re-display.
Go deeper: Creating and parsing colors covers every accepted syntax, the parsing rules, and the edge cases.