Color

Main entry point for the PHPColor library.

Provides factory methods for common colors and high-level utilities for parsing,
mixing, and analyzing colors.

Methods

black

static
public static function black(): SrgbColor

Create a new sRGB black color.

blue

static
public static function blue(): SrgbColor

Create a new sRGB blue color.

contrast

static
public static function contrast(ColorInterface|string $a, ColorInterface|string $b): float

Calculate the WCAG 2.1 contrast ratio between two colors.

Parameters
NameTypeDefault
$a ColorInterface|string -
$b ColorInterface|string -

cyan

static
public static function cyan(): SrgbColor

Create a new sRGB cyan color.

deltaE

static
public static function deltaE(ColorInterface|string $a, ColorInterface|string $b): float

Calculate the perceptual color difference between two colors using CIEDE2000.

Parameters
NameTypeDefault
$a ColorInterface|string -
$b ColorInterface|string -

distance

static
public static function distance(ColorInterface|string $a, ColorInterface|string $b, ColorDistanceInterface|string $algorithm): float

Calculate color distance using a specific algorithm.

Supported algorithms:
- 'CIEDE2000' or 'DE2000': Most accurate modern formula
- 'DeltaE94' or 'DE94': Improved 1994 formula
- 'CMC' or 'CMC(2:1)': UK textile industry standard (acceptability)
- 'CMC(1:1)': CMC perceptibility variant

Parameters
NameTypeDefault
$a ColorInterface|string -
$b ColorInterface|string -
$algorithm ColorDistanceInterface|string "CIEDE2000"

from

static
public static function from(ColorInterface|string $input): ColorInterface

Normalize input to a ColorInterface instance.

Accepts either a ColorInterface instance (returned as-is) or a CSS color string (parsed).
This is the single entry point for input normalization across the API.

Parameters
NameTypeDefault
$input ColorInterface|string -

gray

static
public static function gray(): SrgbColor

Create a new sRGB gray color.

green

static
public static function green(): SrgbColor

Create a new sRGB green color.

hex

static
public static function hex(string $hex): SrgbColor

Create a new sRGB color from a hexadecimal string.

Parameters
NameTypeDefault
$hex string -

magenta

static
public static function magenta(): SrgbColor

Create a new sRGB magenta color.

mix

static
public static function mix(ColorInterface|string $a, ColorInterface|string $b, float $t, ColorInterface|string $in): ColorInterface

Mix two colors together in a specified color space.

Supports mixing in sRGB, linear sRGB and Oklab spaces. Color channels are interpolated with
premultiplied alpha, as specified by CSS Color 4, so a transparent endpoint does not tint
the result.

Parameters
NameTypeDefault
$a ColorInterface|string -
$b ColorInterface|string -
$t float -
$in ColorInterface|string "oklab"

oklab

static
public static function oklab(float $l, float $a, float $b, float $alpha): OklabColor

Create a new Oklab color.

Parameters
NameTypeDefault
$l float -
$a float -
$b float -
$alpha float 1

oklch

static
public static function oklch(float $l, float $c, float $h, float $alpha): OklchColor

Create a new Oklch color.

Parameters
NameTypeDefault
$l float -
$c float -
$h float -
$alpha float 1

orange

static
public static function orange(): SrgbColor

Create a new sRGB orange color.

parse

static
public static function parse(string $input): ColorInterface

Parse a CSS color string into a ColorInterface instance.

Supports hex, rgb(), rgba(), hsl(), hsla(), oklab(), oklch(), lab(), lch(),
and named colors.

Parameters
NameTypeDefault
$input string -

primaries

static
public static function primaries(float $alpha): array

Get the primary sRGB colors (red, green, blue).

Parameters
NameTypeDefault
$alpha float 1

purple

static
public static function purple(): SrgbColor

Create a new sRGB purple color.

red

static
public static function red(): SrgbColor

Create a new sRGB red color.

rgb

static
public static function rgb(float $r, float $g, float $b, float $a): SrgbColor

Create a new sRGB color from component values.

Parameters
NameTypeDefault
$r float -
$g float -
$b float -
$a float 1

secondaries

static
public static function secondaries(float $alpha): array

Get the secondary sRGB colors (cyan, magenta, yellow).

Parameters
NameTypeDefault
$alpha float 1

teal

static
public static function teal(): SrgbColor

Create a new sRGB teal color.

tryFrom

static
public static function tryFrom(ColorInterface|string $input): ColorInterface

Attempt to normalize input to ColorInterface without throwing exceptions.

Returns null if the input string cannot be parsed.

Parameters
NameTypeDefault
$input ColorInterface|string -

white

static
public static function white(): SrgbColor

Create a new sRGB white color.

yellow

static
public static function yellow(): SrgbColor

Create a new sRGB yellow color.