src/Color.php
public static function validateHexColorString(string $hex) : bool
    {
        // optional leading hash #
        $length = strlen( ltrim($hex, '#') );

        // must be 3 or 6 characters
        switch ($length) {
            case 3: break;
            case 6: break;
            default: return false;
        }

        return preg_match('/^#?[0-9a-fA-F]{3,6}$/', $hex) === 1;
    }
public static function validateHexColorString(string $hex) : bool
    {
        // optional leading hash #
        $length = strlen( ltrim($hex, '#') );

        // must be 3 or 6 characters
        switch ($length) {
            case 3: break;
            case 6: break;
            default: return false;
        }

        return preg_match('/^#?[0-9a-fA-F]{3,6}$/', $hex) === 1;
    }
public static function validateHexColorString ( string $ hex ) : bool { // optional leading hash # $ length = strlen ( ltrim ( $ hex , '#' ) ) ; // must be 3 or 6 characters switch ( $ length ) { case 3 : break ; case 6 : break ; default : return false ; } return preg_match ( '/^#?[0-9a-fA-F]{3,6}$/' , $ hex ) === 1 ; }
Validates CSS hexadecimal color.

@param string $hex CSS hexadecimal color with optional leading hash #

@return bool Returns true if valid CSS hexadecimal color, otherwise false.
Validates CSS hexadecimal color .