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
contrast
Calculate the WCAG 2.1 contrast ratio between two colors.
| Name | Type | Default |
|---|---|---|
$a |
ColorInterface|string | - |
$b |
ColorInterface|string | - |
deltaE
Calculate the perceptual color difference between two colors using CIEDE2000.
| Name | Type | Default |
|---|---|---|
$a |
ColorInterface|string | - |
$b |
ColorInterface|string | - |
distance
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
| Name | Type | Default |
|---|---|---|
$a |
ColorInterface|string | - |
$b |
ColorInterface|string | - |
$algorithm |
ColorDistanceInterface|string | "CIEDE2000" |
from
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.
| Name | Type | Default |
|---|---|---|
$input |
ColorInterface|string | - |
hex
Create a new sRGB color from a hexadecimal string.
| Name | Type | Default |
|---|---|---|
$hex |
string | - |
mix
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.
| Name | Type | Default |
|---|---|---|
$a |
ColorInterface|string | - |
$b |
ColorInterface|string | - |
$t |
float | - |
$in |
ColorInterface|string | "oklab" |
oklab
Create a new Oklab color.
| Name | Type | Default |
|---|---|---|
$l |
float | - |
$a |
float | - |
$b |
float | - |
$alpha |
float | 1 |
oklch
Create a new Oklch color.
| Name | Type | Default |
|---|---|---|
$l |
float | - |
$c |
float | - |
$h |
float | - |
$alpha |
float | 1 |
parse
Parse a CSS color string into a ColorInterface instance.
Supports hex, rgb(), rgba(), hsl(), hsla(), oklab(), oklch(), lab(), lch(),
and named colors.
| Name | Type | Default |
|---|---|---|
$input |
string | - |
primaries
Get the primary sRGB colors (red, green, blue).
| Name | Type | Default |
|---|---|---|
$alpha |
float | 1 |
rgb
Create a new sRGB color from component values.
| Name | Type | Default |
|---|---|---|
$r |
float | - |
$g |
float | - |
$b |
float | - |
$a |
float | 1 |
secondaries
Get the secondary sRGB colors (cyan, magenta, yellow).
| Name | Type | Default |
|---|---|---|
$alpha |
float | 1 |
tryFrom
Attempt to normalize input to ColorInterface without throwing exceptions.
Returns null if the input string cannot be parsed.
| Name | Type | Default |
|---|---|---|
$input |
ColorInterface|string | - |