color_from_hex(string $hex): Color Parses #RGB, RGB, #RGBA, RGBA, #RRGGBB, RRGGBB, #RRGGBBAA, or RRGGBBAA (case-insensitive).
color_from_rgb(int $r, int $g, int $b, float $a = 1.0): Color Creates a Color from RGB channels (0..255) and optional alpha (0.0..1.0).
color_from_hsl(float $h, float $s, float $l, float $a = 1.0): Color Creates a Color from hue (0.0..360.0), saturation (0.0..100.0), lightness (0.0..100.0), and optional alpha (0.0..1.0).
Color Class
Properties
public int $r (0..255)
public int $g (0..255)
public int $b (0..255)
public float $a (0.0..1.0)
Methods
new Color(int $r, int $g, int $b, float $a = 1.0) Constructs a validated Color instance.
toHex(bool $includeAlpha = false): string Returns lowercase #rrggbb, or #rrggbbaa when $includeAlpha is true.
All invalid inputs throw Exception with descriptive color: prefixed messages:
Invalid hex length or non-hex characters: color: invalid hex color '#12345' (expected 3, 4, 6, or 8 hex digits)color: invalid hex character in '#zzzzzz'
Out-of-range RGB or alpha channels: color: red channel must be between 0 and 255, got 300color: alpha channel must be between 0.0 and 1.0, got 1.5
Out-of-range HSL channels: color: hue must be between 0.0 and 360.0, got 400color: saturation must be between 0.0 and 100.0, got -5
Out-of-range manipulation parameters: color: lighten amount must be between 0.0 and 100.0, got 120color: mix weight must be between 0.0 and 1.0, got 2
Examples
01Quick start
Bundled with the compiler — import "color"; works offline in any file, or add it to a project with hphp install color.