Convert a color defined in hexvalues to corresponding dezimal values

@access private
@param string $hex color value in hexformat (e.g. '#FF0000')
@return array associative array with color values in dezimal format (fields: 'red', 'green', 'blue')
5794
function _hexToDecColor($hex)
    {
        $length = strlen($hex);
        $color['red'] = hexdec(substr($hex, $length - 6, 2));
        $color['green'] = hexdec(substr($hex, $length - 4, 2));
        $color['blue'] = hexdec(substr($hex, $length - 2, 2));
        return $color;
    }
4584
static function FromRgbHex($hex)
    {
        $cleanHex = ltrim($hex, '#');
        if (strlen($cleanHex) == 3) {
            $r = hexdec(substr($cleanHex, 0, 1) . substr($cleanHex, 0, 1));
            $g = hexdec(substr($cleanHex, 1, 1) . substr($cleanHex, 1, 1));
            $b = hexdec(substr($cleanHex, 2, 1) . substr($cleanHex, 2, 1));
        }
        else {
            $r = hexdec(substr($cleanHex, 0, 2));
            $g = hexdec(substr($cleanHex, 2, 2));
            $b = hexdec(substr($cleanHex, 4, 2));
        }
        return self::FromRgb($r, $g, $b);
    }
1046900
public static function colourBrightness($hex, $percent)
    {
        // Work out if hash given
        $hash = '';
        if (mb_stristr($hex, '#')) {
            $hex = str_replace('#', '', $hex);
            $hash = '#';
        }
        /// HEX TO RGB
        $rgb = [hexdec(mb_substr($hex, 0, 2)), hexdec(mb_substr($hex, 2, 2)), hexdec(mb_substr($hex, 4, 2))];
        //// CALCULATE
        for ($i = 0; $i < 3; ++$i) {
            // See if brighter or darker
            if ($percent > 0) {
                // Lighter
                $rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1 - $percent));
            } else {
                // Darker
                $positivePercent = $percent - ($percent * 2);
                $rgb[$i] = round($rgb[$i] * $positivePercent) + round(0 * (1 - $positivePercent));
            }
            // In case rounding up causes us to go to 256
            if ($rgb[$i] > 255) {
                $rgb[$i] = 255;
            }
        }
        //// RBG to Hex
        $hex = '';
        for ($i = 0; $i < 3; ++$i) {
            // Convert the decimal digit to hex
            $hexDigit = dechex($rgb[$i]);
            // Add a leading zero if necessary
            if (1 === mb_strlen($hexDigit)) {
                $hexDigit = '0'.$hexDigit;
            }
            // Append to the hex string
            $hex .= $hexDigit;
        }

        return $hash.$hex;
    }
208334
protected function toHex($color)
	    {
	        $rgb = $this->getRGBComponents($color);

	        return sprintf('#%02X%02X%02X', $rgb[0], $rgb[1], $rgb[2]);
	    }
200259
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;
    }
912666
private function toRGB($color)
    {
        preg_match('/^#?([A-F0-9]{2})([A-F0-9]{2})([A-F0-9]{2})/i', $color, $m);

        return array(hexdec($m[1]), hexdec($m[2]), hexdec($m[3]));
    }
611648
public static function get_brightness( $hex ) {
		$hex = self::sanitize_hex( $hex, false );

		// Returns brightness value from 0 to 255.
		return intval( ( ( hexdec( substr( $hex, 0, 2 ) ) * 299 ) + ( hexdec( substr( $hex, 2, 2 ) ) * 587 ) + ( hexdec( substr( $hex, 4, 2 ) ) * 114 ) ) / 1000 );
	}
259176
public static Color fromHex( String hex ) {
        if (hex.startsWith("#")) {
            hex = hex.substring(1);
        }
        int length = hex.length();
        int total = 6;
        if (length < total) {
            // we have a shortened version
            String token = hex;
            int tokenLength = token.length();
            for( int i = 0; i < total; i = i + tokenLength ) {
                hex += token;
            }
        }

        int index = 0;
        String r = hex.substring(index, index + 2);
        String g = hex.substring(index + 2, index + 4);
        String b = hex.substring(index + 4, index + total);
        return new Color(Integer.valueOf(r, 16), Integer.valueOf(g, 16), Integer.valueOf(b, 16));
    }
895542
function _0xColorToHex(color, convertToStr) {
        var hexColor = tinycolor(color.replace("0x", "#"));
        hexColor._format = "0x";

        if (convertToStr) {
            return hexColor.toString();
        }
        return hexColor;
    }
215058
public static function parse($color) 
    {
        if (gettype($color) == "integer") {
            return $color & 0xffffffff;
        }

        $color = "$color";

        if (preg_match('/^#?[0-9a-fA-F]+$/', $color)) {
            $hexColor = $color; 
            if ($hexColor[0] == '#') {
                $hexColor = substr($hexColor, 1);
            }

            switch (strlen($hexColor)) {
                case 3:
                    $numeric = intval($hexColor, 16);
                    return (
                        (($numeric & 0xf00) << 20) |
                        (($numeric & 0xf00) << 16) |
                        (($numeric & 0x0f0) << 16) |
                        (($numeric & 0x0f0) << 12) |
                        (($numeric & 0x00f) << 12) |
                        (($numeric & 0x00f) << 8) | 
                        0xff);
                case 6:
                    return (intval($hexColor, 16) << 8) | 0xff;
                case 8:
                    // Workaround to cope with PHP limitation of intval
                    $numeric = 
                        (intval(substr($hexColor, 0, 4), 16) << 16) |
                        (intval(substr($hexColor, 4, 4), 16));
                    return $numeric;
            }
        }

        throw new \InvalidArgumentException("Invalid color '$color'.");
    }
450008
private function parseColor($color)
    {
        try {
            if ($color instanceof ColorJizz) {
                return $color;
            }
            if (is_string($color)) {
                if (substr($color, 0, 1) == '#') {
                    return Hex::fromString(substr($color, 1));
                }

                if (strlen($color) == 6) {
                    return Hex::fromString($color);
                }
            }
            if (is_integer($color)) {
                return new Hex($color);
            }

        } catch (InvalidArgumentException $e) {
            throw new InvalidColorException('Could not parse color. Currently supported formats are 0x000000, #ff0000 and ff0000', 0, $e);
        }
        throw new InvalidColorException(sprintf('Could not detect color %s', $color));
    }
39148
private function formatHex(string $hex): string
    {
        $hex = str_pad($hex, 32, '0');
        preg_match('/([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{12})/', $hex, $matches);
        array_shift($matches);
        $expandedUUID = implode('-', $matches);

        return $expandedUUID;
    }
854757
private static function _get_luminance( $hex ) {
		$red   = hexdec( substr( $hex, 0, 2 ) );
		$green = hexdec( substr( $hex, 2, 2 ) );
		$blue  = hexdec( substr( $hex, 4, 2 ) );
		$max_rgb = max( $red, $green, $blue );
		$min_rgb = min( $red, $green, $blue );

		return ( $max_rgb + $min_rgb ) / 2 / 255 * 100;
	}
71568
public static function getInvertedColor($color)
    {
        // Prepare the color for later usage
        $color = trim($color);
        $withHash = Regex::startsWith($color, '#');

        /*
         * Verify and get valid value of color.
         * An exception will be thrown if the value is not a color.
         */
        $validColor = Regex::getValidColorHexValue($color);

        // Grab color's components
        $red = hexdec(substr($validColor, 0, 2));
        $green = hexdec(substr($validColor, 2, 2));
        $blue = hexdec(substr($validColor, 4, 2));

        // Calculate inverted color's components
        $redInverted = self::getValidColorComponent(255 - $red);
        $greenInverted = self::getValidColorComponent(255 - $green);
        $blueInverted = self::getValidColorComponent(255 - $blue);

        // Voila, here is the inverted color
        $invertedColor = sprintf('%s%s%s', $redInverted, $greenInverted, $blueInverted);

        if ($withHash) {
            return sprintf('#%s', $invertedColor);
        }

        return $invertedColor;
    }
687947
public function getBaseColors($limit = 10) {

		$hexs 	= $this -> getHexColors();
		$output = [];
		$index  = 0;

		foreach($hexs as $hex => $amount) {

			if($index >= $limit) {
				break;
			}

			$color = Color :: name((string) $hex);

			if(null !== $color) {
				$output[] = $color;
			}

			$index++;
		}

		return $output;
	}
658150
private function _hexToIntArray($hex)
	{
		$array = array();

		$j = (int) strlen($hex) / 2;

		for ($i = 0; $i < $j; $i++)
		{
			$array[$i] = (int) $this->_hexToInt($hex, $i);
		}

		return $array;
	}
1078792
private static function hexToRgb(string $color): array
    {
        $color = ltrim($color, '#');

        // Convert the shorthand hex to the full hex (09F => 0099FF)
        if (strlen($color) == 3) {
            $color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
        }

        return [
            'r' => (int)hexdec(substr($color, 0, 2)),
            'g' => (int)hexdec(substr($color, 2, 2)),
            'b' => (int)hexdec(substr($color, 4, 2)),
        ];
    }