Parses a valid HSL[A] CSS color function/string.
Description
Direct port of colord’s parseHslaString function.
Parameters
$input
stringrequired- The HSLA string to parse.
Source
private static function colord_parse_hsla_string( $input ) {
// Functional syntax.
$is_match = preg_match(
'/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i',
$input,
$match
);
if ( ! $is_match ) {
// Whitespace syntax.
$is_match = preg_match(
'/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i',
$input,
$match
);
}
if ( ! $is_match ) {
return null;
}
/*
* For some reason, preg_match doesn't include empty matches at the end
* of the array, so we add them manually to make things easier later.
*/
for ( $i = 1; $i <= 6; $i++ ) {
if ( ! isset( $match[ $i ] ) ) {
$match[ $i ] = '';
}
}
$hsla = self::colord_clamp_hsla(
array(
'h' => self::colord_parse_hue( $match[1], $match[2] ),
's' => (float) $match[3],
'l' => (float) $match[4],
'a' => '' === $match[5] ? 1 : (float) $match[5] / ( $match[6] ? 100 : 1 ),
)
);
return self::colord_hsla_to_rgba( $hsla );
}
Changelog
Version | Description |
---|---|
6.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.