Creates a Color instance based on the given Hexadecimal-formatted color.

@param string $hex A hexadecimal color value, the # isn't required but can be given

@return Color
968854
public static function fromHEX($hex)
    {
        $hex = preg_replace('/[^0-9A-Fa-f]/', '', $hex);
        $rgb = [];
        if (strlen($hex) == 6) {
            $colorVal = hexdec($hex);
            $rgb['red'] = 0xFF & ($colorVal >> 0x10);
            $rgb['green'] = 0xFF & ($colorVal >> 0x8);
            $rgb['blue'] = 0xFF & $colorVal;
        } elseif (strlen($hex) == 3) {
            $rgb['red'] = hexdec(str_repeat(substr($hex, 0, 1), 2));
            $rgb['green'] = hexdec(str_repeat(substr($hex, 1, 1), 2));
            $rgb['blue'] = hexdec(str_repeat(substr($hex, 2, 1), 2));
        } else {
            throw new InvalidArgumentException('Given color does not adhere to hexadecimal format');
        }

        return self::fromRGB($rgb['red'], $rgb['green'], $rgb['blue']);
    }
1141771
public static final Color hex2RGB(final String hex)
    {
        String r, g, b;

        final int len = hex.length();

        if (len == 7)
        {
            r = hex.substring(1, 3);

            g = hex.substring(3, 5);

            b = hex.substring(5, 7);
        }
        else if (len == 4)
        {
            r = hex.substring(1, 2);

            g = hex.substring(2, 3);

            b = hex.substring(3, 4);

            r = r + r;

            g = g + g;

            b = b + b;
        }
        else
        {
            return null;// error - invalid length
        }
        try
        {
            return new Color(Integer.valueOf(r, 16), Integer.valueOf(g, 16), Integer.valueOf(b, 16));
        }
        catch (final NumberFormatException ignored)
        {
            return null;
        }
    }
926798
public static function hex2rgb($hex)
    {
        $hex = str_replace("#", "", $hex);
        $r = hexdec(substr($hex,0,2));
        $g = hexdec(substr($hex,2,2));
        $b = hexdec(substr($hex,4,2));
        return "$r,$g,$b";
    }
72200
public function hexToRgb( $hex ) {
        $hex = ltrim($hex, '#'); // Remove #

        if(strlen($hex) == 3) {
            $r = hexdec(substr($hex,0,1).substr($hex,0,1));
            $g = hexdec(substr($hex,1,1).substr($hex,1,1));
            $b = hexdec(substr($hex,2,1).substr($hex,2,1));
        } else {
            $r = hexdec(substr($hex,0,2));
            $g = hexdec(substr($hex,2,2));
            $b = hexdec(substr($hex,4,2));
        }
        return array($r, $g, $b); // Returns an array with the rgb values
    }
871277
public static function fromHex(string $hex): Uuid
    {
        $clean = strtolower($hex);

        if (!preg_match(static::UUID_HEX, $clean, $matches)) {
            $message = sprintf('Invalid UUID hex: %s', $hex);
            throw new DomainException($message);
        }

        $timeLow = $matches[1];
        $timeMid = $matches[2];
        $timeHiAndVersion = $matches[3];
        $clockSeqHiAndReserved = $matches[4];
        $clockSeqLow = $matches[5];
        $node = $matches[6];

        return new static($timeLow, $timeMid, $timeHiAndVersion, $clockSeqHiAndReserved, $clockSeqLow, $node);
    }
1131925
protected function _color_luminance( $hex, $percent ) {
		$hex        = $this->_hex_normalization( $hex );
		$hue        = $this->_get_hue( $hex );
		$saturation = $this->_get_saturation( $hex );
		$luminance  = $this->_get_luminance( $hex );

		// Add luminance.
		$luminance += $percent * 100;
		$luminance  = ( 100 < $luminance ) ? 100 : $luminance;
		$luminance  = ( 0 > $luminance ) ? 0 : $luminance;

		$hex = $this->_convert_hsl_to_hex( $hue, $saturation, $luminance );
		return $hex;
	}
712048
public function setColor($color)
	{
		if (is_array($color))
		{
			$this->color[0] = $color[0];
			$this->color[1] = $color[1];
			$this->color[2] = $color[2];
		}
		else
		{
			$color = ltrim($color, '#');

			$this->color[0] = hexdec(substr($color, 0, 2));
			$this->color[1] = hexdec(substr($color, 2, 2));
			$this->color[2] = hexdec(substr($color, 4, 2));
		}

		return $this;
	}
1196725
public static function IPv6Hex2Bin($hex) {
			// use PHP-function if PHP was compiled with IPv6-support
		if (defined('AF_INET6')) {
			$bin = inet_pton($hex);
		} else {
			$hex = self::normalizeIPv6($hex);
			$hex = str_replace(':', '', $hex); // Replace colon to nothing
			$bin = pack("H*" , $hex);
		}
		return $bin;
	}
343111
protected static function fromHex(string $hex): string
    {
        $data = '';

        // Convert hex to string.
        for ($i = 0; $i < \strlen($hex) - 1; $i += 2) {
            $data .= \chr(\hexdec($hex[$i].$hex[$i + 1]));
        }

        return $data;
    }
976573
public static function fromHexString($color)
    {
        $color = rtrim($color, '#');
        preg_match_all('([0-9a-f][0-9a-f])', $color, $rgb);

        $c = new self();
        list($c->r, $c->g, $c->b) = array_map('hexdec', $rgb[0]);

        return $c;
    }
129330
protected function create_color($hex, $alpha)
	{
		extract($this->create_hex_color($hex));
		return new \ImagickPixel('rgba('.$red.', '.$green.', '.$blue.', '.round($alpha / 100, 2).')');
	}
485726
public static Color hexToColor(String hex) {
		return getColor(Integer.parseInt(StrUtil.removePrefix("#", hex), 16));
	}
481982
private function hexToDec($hex)
	{
		$dec = 0;
		$bitval = 1;
		$len = strlen($hex);
		for ($pos = ($len - 1); $pos >= 0; --$pos) {
			$dec = bcadd($dec, bcmul(hexdec($hex[$pos]), $bitval));
			$bitval = bcmul($bitval, 16);
		}
		return $dec;
	}
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;
    }
1211014
public static function parseHex($color)
    {
        if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $color)) {
            throw new WrongColorFormatException($color);
        }

        return new Color(
            hexdec(substr($color, 1, 2)),
            hexdec(substr($color, 3, 2)),
            hexdec(substr($color, 5, 2))
        );
    }
824089
private function hex2rgb($hex)
    {
        $hex = str_replace('#', '', $hex);

        // expand shorthand notation (#36A -> #3366AA)
        if (3 == strlen($hex)) {
            $hex = $hex{0}
            .$hex{0}
            .$hex{1}
            .$hex{1}
            .$hex{2}
            .$hex{2};
        }

        return array(
            hexdec(substr($hex, 0, 2)),
            hexdec(substr($hex, 2, 2)),
            hexdec(substr($hex, 4, 2)),
        );
    }
384840
protected static function uni2utf8($hex)
    {

        if($hex < 0x80)
            return chr($hex);
        else if($hex < 0x800)
            return chr(0xc0 | ($hex >>  6)).chr(0x80 | ( $hex & 0x3f));
        else if($hex < 0x10000)
            return chr(0xe0 | ($hex >> 12)).chr(0x80 | (($hex >> 6  ) & 0x3f)).chr(0x80 | ($hex & 0x3f));
        else if($hex < 0x200000)
            return chr(0xf0 | ($hex >> 18)).chr(0x80 | (($hex >> 12 ) & 0x3f)).chr(0x80 | (($hex >> 6) & 0x3f)).chr(0x80 | ($hex & 0x3f));

        return false;
    }