ColorPaletteInterface

Interface

Public contract for color palette value objects.

Methods

all

public function all(): array

Get all colors in the palette as an array.

Returns an associative array for named palettes (name => ColorInterface)
or a sequential array for scale palettes.

at

public function at(float $position): ColorInterface

Get a color at a specific relative position in the palette.

Only available for scale palettes. Uses interpolation between existing
colors to return a color at the given position (0.0 to 1.0).

Parameters
NameTypeDefault
$position float -
public function closest(ColorInterface $color, ColorDistanceInterface $metric): ColorInterface

Find the color in the palette that is closest to a target color.

By default, the match is the one with the smallest Euclidean distance in
the Oklab color space.

or null for the default Oklab Euclidean distance

Parameters
NameTypeDefault
$color ColorInterface -
$metric ColorDistanceInterface null
public function count(): int

Get the number of colors in the palette.

public function darken(float $amount): static

Decrease the lightness of all colors in the palette.

Adjusts lightness in Oklch space by the given amount.

Parameters
NameTypeDefault
$amount float -
public function desaturate(float $amount): static

Decrease the chroma (saturation) of all colors in the palette.

Adjusts chroma in Oklch space by the given amount.

Parameters
NameTypeDefault
$amount float -
public function filter(callable $callback): static

Filter the colors in the palette using a predicate function.

Returns a new palette containing only the colors that match the predicate.

Parameters
NameTypeDefault
$callback callable -

get

public function get(string|int $key): ColorInterface

Get a specific color by its index or name.

Parameters
NameTypeDefault
$key string|int -
public function isNamed(): bool

Check if the palette is a named palette.

Returns true if colors are associated with string names, false if it's a scale.

public function lighten(float $amount): static

Increase the lightness of all colors in the palette.

Adjusts lightness in Oklch space by the given amount.

Parameters
NameTypeDefault
$amount float -

map

public function map(callable $callback): static

Transform all colors in the palette using a mapping function.

Returns a new palette with the results of the transformation.

Parameters
NameTypeDefault
$callback callable -
public function merge(ColorPaletteInterface $palette): static

Merge this palette with another palette.

Only available for scale palettes. Returns a new palette combining both.

Parameters
NameTypeDefault
$palette ColorPaletteInterface -
public function names(): array

Get the names of all colors in the palette.

Only available for named palettes. Returns an array of color names.

public function reverse(): static

Reverse the order of colors in the palette.

Only available for scale palettes.

public function rotateHue(float $angle): static

Rotate the hue of all colors in the palette.

Adjusts the hue in Oklch space by the given number of degrees.

Parameters
NameTypeDefault
$angle float -
public function saturate(float $amount): static

Increase the chroma (saturation) of all colors in the palette.

Adjusts chroma in Oklch space by the given amount.

Parameters
NameTypeDefault
$amount float -
public function slice(int $offset, int $length): static

Get a sub-slice of the palette.

Only available for scale palettes. Works like array_slice().

Parameters
NameTypeDefault
$offset int -
$length int null

to

public function to(string $space): static

Convert all colors in the palette to a specific color space.

Parameters
NameTypeDefault
$space string -
public function toCss(string $format): array

Convert all colors in the palette to CSS color strings.

Parameters
NameTypeDefault
$format string null
public function toCssVariables(string $prefix): string

Generate CSS custom properties (variables) for the palette.

Only available for named palettes. Generates a string of CSS variable
declarations like '--prefix-name: color;'.

sRGB colors are written as hexadecimal, other spaces keep their own CSS
notation so that no gamut or precision is lost.

Parameters
NameTypeDefault
$prefix string "color"
public function toHex(): array

Convert all colors in the palette to hexadecimal color strings.