PHPColor provides several entry points for converting strings and other inputs into concrete color objects.
This is the most common way to create a color. It supports all standard CSS notations, hexadecimal strings, and modern color functions.
use PhpColor\Color\Color;
$c1 = Color::parse('#ff6600');
$c2 = Color::parse('rgb(255 0 0)');
$c3 = Color::parse('oklch(60% 0.15 250)');If you have a value that might already be a ColorInterface or a string, Color::from() is the safest way to ensure you have a color object.
$color = Color::from($input); // $input can be a string or ColorInterfaceTo avoid exceptions, use tryFrom(), which returns null if the input cannot be parsed.
$color = Color::tryFrom('invalid'); // Returns nullFor advanced use cases where you need to parse expressions that are resolved later (like CSS variables or light-dark() functions), use the CssColor parser.
use PhpColor\Color\Css\CssColor;
$expr = CssColor::parse('var(--brand, blue)');
// Returns a resolvable expression instead of a concrete colorWhen Color::parse() or Color::from() fail, they throw a ParseException.
use PhpColor\Color\Exception\ParseException;
try {
$color = Color::parse('not-a-color');
} catch (ParseException $e) {
// Handle error
}Next: Formatting Colors