AbstractColor

Abstract

Base class for all color space implementations.

Provides shared logic for color manipulations, space conversions, and
utility methods for parsing and formatting color components.

Methods

public function analogous(int $count): array
Parameters
NameTypeDefault
$count int 2
public function blend(ColorInterface|string $backdrop, string $mode): static
Parameters
NameTypeDefault
$backdrop ColorInterface|string -
$mode string "normal"
public function complementary(): static
public function cool(float $amount): static
Parameters
NameTypeDefault
$amount float -
public function darken(float $amount): static
Parameters
NameTypeDefault
$amount float -
public function desaturate(float $amount): static
Parameters
NameTypeDefault
$amount float -
public function equals(ColorInterface $other): bool
Parameters
NameTypeDefault
$other ColorInterface -

from

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

fromSrgb

static
public static function fromSrgb(SrgbColor $srgb): static
Parameters
NameTypeDefault
$srgb SrgbColor -
public function getAlpha(): float
public function getChannels(): array

Get the color channel values as an associative array.

Returns the color's channel values in its native color space. The channel names
and value ranges depend on the color space:
- sRGB: ['r', 'g', 'b'] (0-1)
- Oklab: ['l', 'a', 'b'] (l: 0-1, a/b: unbounded)
- Oklch: ['l', 'c', 'h'] (l: 0-1, c: 0+, h: 0-360)
- Lab: ['l', 'a', 'b'] (l: 0-100, a/b: unbounded)
- Lch: ['l', 'c', 'h'] (l: 0-100, c: 0+, h: 0-360)
- XYZ: ['x', 'y', 'z'] (unbounded)
- Display P3: ['r', 'g', 'b'] (0-1)

The alpha channel is accessed separately via getAlpha().

This is particularly useful for CSS Relative Color Syntax or custom color
manipulations where you need direct access to channel values.

Examples:
- Color::parse('red')->getChannels() // ['r' => 1.0, 'g' => 0.0, 'b' => 0.0]
- Color::parse('oklab(0.5 0.1 -0.2)')->getChannels() // ['l' => 0.5, 'a' => 0.1, 'b' => -0.2]

public function getHue(): float
public function getLuminance(): float
public function getOpacity(): float
public function getSaturation(): float

getSpaceName

static
public static function getSpaceName(): string
public function grayscale(): static
public function invert(): static
public function isCold(): bool
public function isDark(): bool
public function isHot(): bool
public function isLight(): bool
public function isOpaque(): bool
public function isTransparent(): bool
public function lighten(float $amount): static
Parameters
NameTypeDefault
$amount float -
public function rotateHue(float $degrees): static
Parameters
NameTypeDefault
$degrees float -
public function saturate(float $amount): static
Parameters
NameTypeDefault
$amount float -
public function shade(float $t): static
Parameters
NameTypeDefault
$t float -
public function splitComplementary(): array
public function temperature(): float
public function tetradic(): array
public function tint(float $t): static
Parameters
NameTypeDefault
$t float -

to

public function to(ColorInterface|string $space): ColorInterface
Parameters
NameTypeDefault
$space ColorInterface|string -
public function toCss(string $space): string

Generate CSS color function output.

Returns a CSS-compatible color string. The output format depends on the space parameter:
- null (auto): Uses the most appropriate format for this color space
- 'srgb' or 'rgb': rgb() function
- 'hsl': hsl() function (for sRGB colors)
- 'color': color() function with explicit color space
- 'oklab', 'oklch', 'lab', 'lch': Respective CSS color functions

Examples:
- ->toCss() => 'rgb(255 0 0)' or 'oklab(0.628 0.225 0.126)'
- ->toCss('hsl') => 'hsl(0 100% 50%)'
- ->toCss('srgb') => 'rgb(255 0 0)'
- ->toCss('color') => 'color(srgb 1 0 0)'

Alpha channel is included in output when not fully opaque (< 1.0):
- 'rgb(255 0 0 / 0.5)' or 'oklab(0.628 0.225 0.126 / 0.8)'

Parameters
NameTypeDefault
$space string null
public function toHex(bool $withAlpha): string
Parameters
NameTypeDefault
$withAlpha bool false
public function toOklch(): OklchColor
public function toSrgb(): SrgbColor

Convert to sRGB color space.

Convenience method for converting to sRGB, the most common color space for web use.
This method is used internally by many operations that need to work in sRGB space.

public function toString(): string
public function triadic(): array

tryFrom

static
public static function tryFrom(ColorInterface|string $input): static
Parameters
NameTypeDefault
$input ColorInterface|string -
public function warm(float $amount): static
Parameters
NameTypeDefault
$amount float -
public function withAlpha(float $alpha): static
Parameters
NameTypeDefault
$alpha float 1
public function withChannel(string $name, int|float $value): static
Parameters
NameTypeDefault
$name string -
$value int|float -
public function withChannels(array $partial): static
Parameters
NameTypeDefault
$partial array -