toolkit

Color

Color helpers: read any CSS color (#hex, rgb(), hsl() or oklch()) into rgb/hsl, pick a readable text color, lighten and darken.

Color:: Phlat\Toolkit\Color 10 methods

Conversion 7

  • parse red, green, blue (0-255) of a CSS color written as #rgb, #rrggbb, rgb(), hsl(), oklch() or a basic color name, or null if it is none of those. oklch can reach past the sRGB gamut; those colors are clipped to it.
  • rgb The color as red, green and blue; black when parse() can't read it.
  • hex The color as a normalized #rrggbb hex.
  • hsl The color as hue, saturation and lightness.
  • fromHsl Builds a #rrggbb hex from hue (0-359), saturation (0-100) and lightness (0-100).
  • rgbString CSS `rgb(237 51 94)`.
  • hslString CSS `hsl(346 84% 56%)`.

Adjustment 3

  • lighten Raises HSL lightness by $percent points, capped at 100.
  • darken Lowers HSL lightness by $percent points, floored at 0.
  • contrast #000000 or #ffffff, whichever reads better on top of $color (WCAG relative luminance).

Conversion

Color::parse(string $color): array{int, int, int}|null

red, green, blue (0-255) of a CSS color written as #rgb, #rrggbb, rgb(), hsl(), oklch() or a basic color name, or null if it is none of those. oklch can reach past the sRGB gamut; those colors are clipped to it.

Parameters (1)
$color string

Returns array{int, int, int}|null

phlat/src/Toolkit/Color.php:44

rgb

Color::rgb(string $color): array{int, int, int}

The color as red, green and blue; black when parse() can't read it.

Parameters (1)
$color string

Returns array{int, int, int}: 0-255 each

phlat/src/Toolkit/Color.php:79

hex

Color::hex(string $color): string

The color as a normalized #rrggbb hex.

Parameters (1)
$color string

Returns string

phlat/src/Toolkit/Color.php:85

hsl

Color::hsl(string $color): array{int, int, int}

The color as hue, saturation and lightness.

Parameters (1)
$color string

Returns array{int, int, int}: hue 0-359, saturation and lightness 0-100

phlat/src/Toolkit/Color.php:95
Color::fromHsl(int $h, int $s, int $l): string

Builds a #rrggbb hex from hue (0-359), saturation (0-100) and lightness (0-100).

Parameters (3)
$h int
$s int
$l int

Returns string

phlat/src/Toolkit/Color.php:118
Color::rgbString(string $color): string

CSS `rgb(237 51 94)`.

Parameters (1)
$color string

Returns string

phlat/src/Toolkit/Color.php:163
Color::hslString(string $color): string

CSS `hsl(346 84% 56%)`.

Parameters (1)
$color string

Returns string

phlat/src/Toolkit/Color.php:169

Adjustment

Color::lighten(string $color, int $percent): string

Raises HSL lightness by $percent points, capped at 100.

Returns a #rrggbb hex.

Parameters (2)
$color string
$percent int

Returns string

phlat/src/Toolkit/Color.php:178
Color::darken(string $color, int $percent): string

Lowers HSL lightness by $percent points, floored at 0.

Returns a #rrggbb hex.

Parameters (2)
$color string
$percent int

Returns string

phlat/src/Toolkit/Color.php:185
Color::contrast(string $color): string

#000000 or #ffffff, whichever reads better on top of $color (WCAG relative luminance).

Parameters (1)
$color string

Returns string

phlat/src/Toolkit/Color.php:191