1.20.x color.inc | public static Color::validateHex($hex) |
Validates whether a hexadecimal color value is syntatically 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.
File
- includes/
color.inc, line 18
Class
- Color
- Performs color conversions.
Code
public static function validateHex($hex) {
// Must be a string.
$valid = is_string($hex);
// Hash prefix is optional.
$hex = ltrim($hex, '#');
// Must be either RGB or RRGGBB.
$length = backdrop_strlen($hex);
$valid = $valid && ($length === 3 || $length === 6);
// Must be a valid hex value.
$valid = $valid && ctype_xdigit($hex);
return $valid;
}