class Color (View source)

Performs color conversions.

Methods

static bool
validateHex($hex)

Validates whether a hexadecimal color value is syntactically correct.

static array
hexToRgb(string $hex)

Parses a hexadecimal color string like '#abc' or '#aabbcc'.

static string
rgbToHex(array|string $input)

Converts RGB color arrays and RGB strings in CSS notation to lowercase simple colors like '#aabbcc'.

static string
normalizeHexLength(string $hex)

Normalize the hex color length to 6 characters for comparison.

Details

static bool validateHex($hex)

Validates whether a hexadecimal color value is syntactically correct.

Parameters

$hex

The hexadecimal string to validate. May contain a leading '#'. May use the shorthand notation (e.g., '123' for '112233').

Return Value

bool

TRUE if $hex is valid or FALSE if it is not.

static array hexToRgb(string $hex)

Parses a hexadecimal color string like '#abc' or '#aabbcc'.

Parameters

string $hex

The hexadecimal color string to parse.

Return Value

array

An array containing the values for 'red', 'green', 'blue'.

Exceptions

InvalidArgumentException

static string rgbToHex(array|string $input)

Converts RGB color arrays and RGB strings in CSS notation to lowercase simple colors like '#aabbcc'.

Parameters

array|string $input

The value to convert. If the value is an array the first three elements will be used as the red, green and blue components. String values in CSS notation like '10, 20, 30' are also supported.

Return Value

string

The lowercase simple color representation of the given color.

static string normalizeHexLength(string $hex)

Normalize the hex color length to 6 characters for comparison.

Parameters

string $hex

The hex color to normalize.

Return Value

string

The 6 character hex color.