src/Utilities/Regex.php
public static function getValidColorHexValue($color, $throwException = true)
    {
        /*
         * Not a scalar value?
         * Nothing to do
         */
        if (!is_scalar($color)) {
            return false;
        }

        $color = Miscellaneous::replace($color, '/#/', '');
        $length = strlen($color);

        /*
         * Color is not 3 or 6 characters long?
         * Nothing to do
         */
        if (3 !== $length && 6 !== $length) {
            if ($throwException) {
                throw new IncorrectColorHexLengthException($color);
            }

            return false;
        }

        /*
         * Color is 3 characters long?
         * Let's make it 6 characters long
         */
        if (3 === $length) {
            $color = Miscellaneous::replace($color, '/(.)(.)(.)/', '$1$1$2$2$3$3');
        }

        $pattern = self::$patterns['color'];
        $match = (bool)preg_match($pattern, $color);

        /*
         * It's valid color
         * Nothing to do more
         */
        if ($match) {
            return strtolower($color);
        }

        if ($throwException) {
            throw new InvalidColorHexValueException($color);
        }

        return false;
    }
public static function getValidColorHexValue($color, $throwException = true)
    {
        /*
         * Not a scalar value?
         * Nothing to do
         */
        if (!is_scalar($color)) {
            return false;
        }

        $color = Miscellaneous::replace($color, '/#/', '');
        $length = strlen($color);

        /*
         * Color is not 3 or 6 characters long?
         * Nothing to do
         */
        if (3 !== $length && 6 !== $length) {
            if ($throwException) {
                throw new IncorrectColorHexLengthException($color);
            }

            return false;
        }

        /*
         * Color is 3 characters long?
         * Let's make it 6 characters long
         */
        if (3 === $length) {
            $color = Miscellaneous::replace($color, '/(.)(.)(.)/', '$1$1$2$2$3$3');
        }

        $pattern = self::$patterns['color'];
        $match = (bool)preg_match($pattern, $color);

        /*
         * It's valid color
         * Nothing to do more
         */
        if ($match) {
            return strtolower($color);
        }

        if ($throwException) {
            throw new InvalidColorHexValueException($color);
        }

        return false;
    }
public static function getValidColorHexValue ( $ color , $ throwException = true ) { /*
         * Not a scalar value?
         * Nothing to do
         */ if ( ! is_scalar ( $ color ) ) { return false ; } $ color = Miscellaneous :: replace ( $ color , '/#/' , '' ) ; $ length = strlen ( $ color ) ; /*
         * Color is not 3 or 6 characters long?
         * Nothing to do
         */ if ( 3 !== $ length && 6 !== $ length ) { if ( $ throwException ) { throw new IncorrectColorHexLengthException ( $ color ) ; } return false ; } /*
         * Color is 3 characters long?
         * Let's make it 6 characters long
         */ if ( 3 === $ length ) { $ color = Miscellaneous :: replace ( $ color , '/(.)(.)(.)/' , '$1$1$2$2$3$3' ) ; } $ pattern = self :: $ patterns [ 'color' ] ; $ match = ( bool ) preg_match ( $ pattern , $ color ) ; /*
         * It's valid color
         * Nothing to do more
         */ if ( $ match ) { return strtolower ( $ color ) ; } if ( $ throwException ) { throw new InvalidColorHexValueException ( $ color ) ; } return false ; }
Returns valid given hexadecimal value of color.
If the value is invalid, throws an exception or returns false.

@param string $color          Color to verify
@param bool   $throwException (optional) If is set to true, throws an exception if given color is invalid
(default behaviour). Otherwise - not.
@throws IncorrectColorHexLengthException
@throws InvalidColorHexValueException
@return bool|string
Returns valid given hexadecimal value of color . If the value is invalid throws an exception or returns false .