Parses a valid RGB[A] CSS color function/string.
Description
Direct port of colord’s parseRgbaString function.
Parameters
$input
stringrequired- The RGBA string to parse.
Source
private static function colord_parse_rgba_string( $input ) {
// Functional syntax.
$is_match = preg_match(
'/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\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(
'/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\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 <= 8; $i++ ) {
if ( ! isset( $match[ $i ] ) ) {
$match[ $i ] = '';
}
}
if ( $match[2] !== $match[4] || $match[4] !== $match[6] ) {
return null;
}
return self::colord_clamp_rgba(
array(
'r' => (float) $match[1] / ( $match[2] ? 100 / 255 : 1 ),
'g' => (float) $match[3] / ( $match[4] ? 100 / 255 : 1 ),
'b' => (float) $match[5] / ( $match[6] ? 100 / 255 : 1 ),
'a' => '' === $match[7] ? 1 : (float) $match[7] / ( $match[8] ? 100 : 1 ),
)
);
}
Changelog
Version | Description |
---|---|
6.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.