Convert HEX color into RGB (or RGBA) value and return parts as array.

@param string $hex
@param float  $alpha [optional]

@return array
487142
public static function hex2rgb($hex, $alpha = null)
    {
        $hex = str_replace('#', '', $hex);

        if (strlen($hex) == 6) {
            $rgb['r'] = hexdec(substr($hex, 0, 2));
            $rgb['g'] = hexdec(substr($hex, 2, 2));
            $rgb['b'] = hexdec(substr($hex, 4, 2));
        } elseif (strlen($hex) == 3) {
            $rgb['r'] = hexdec(str_repeat(substr($hex, 0, 1), 2));
            $rgb['g'] = hexdec(str_repeat(substr($hex, 1, 1), 2));
            $rgb['b'] = hexdec(str_repeat(substr($hex, 2, 1), 2));
        } else {
            $rgb['r'] = '0';
            $rgb['g'] = '0';
            $rgb['b'] = '0';
        }

        if (null !== $alpha) {
            $rgb['a'] = $alpha;
        }

        return $rgb;
    }
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)),
        );
    }
336535
def _hex_to_rgba(hexs):
    """"""
    hexs = np.atleast_1d(np.array(hexs, '|U9'))
    out = np.ones((len(hexs), 4), np.float32)
    for hi, h in enumerate(hexs):
        assert isinstance(h, string_types)
        off = 1 if h[0] == '#' else 0
        assert len(h) in (6+off, 8+off)
        e = (len(h)-off) // 2
        out[hi, :e] = [int(h[i:i+2], 16) / 255.
                       for i in range(off, len(h), 2)]
    return out
799434
public static function rgba(Color $color, $alpha = 1.0)
    {
        return sprintf(
            'rgba(%d, %d, %d, %s)',
            $color->getRgb()['r'],
            $color->getRgb()['g'],
            $color->getRgb()['b'],
            $alpha + 0
        );
    }
886328
private function paletteToHex($palette): array
    {
        $r = [];
        foreach ($palette as $paletteColor) {
            $r[] = self::rgb2hex($paletteColor);
        }

        return $r;
    }
302526
public static function hexToRgb($hex) {
    if (!self::validateHex($hex)) {
      throw new \InvalidArgumentException("'$hex' is not a valid hex value.");
    }

    // Ignore '#' prefixes.
    $hex = ltrim($hex, '#');

    // Convert shorthands like '#abc' to '#aabbcc'.
    if (strlen($hex) == 3) {
      $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
    }

    $c = hexdec($hex);

    return array(
      'red' => $c >> 16 & 0xFF,
      'green' => $c >> 8 & 0xFF,
      'blue' => $c & 0xFF,
    );
  }
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;
    }
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 );
	}
1068690
private function hex2rgb($hex_color)
    {

        if ($hex_color[0] == '#') {
            $hex_color = substr($hex_color, 1);
        }
        if (strlen($hex_color) == 6) {
            list($r, $g, $b) = array(
                $hex_color[0] . $hex_color[1],
                $hex_color[2] . $hex_color[3],
                $hex_color[4] . $hex_color[5]
            );
        } elseif (strlen($hex_color) == 3) {
            list($r, $g, $b) = array(
                $hex_color[0] . $hex_color[0],
                $hex_color[1] . $hex_color[1],
                $hex_color[2] . $hex_color[2]
            );
        } else {
            return false;
        }

        return array(
            'r' => hexdec($r),
            'g' => hexdec($g),
            'b' => hexdec($b)
        );

    }
966377
public static function parseHexString(string $string) : array
    {
        if (!preg_match(self::REGEX_HEX, $string, $matches)) {
            throw InvalidArgumentException::format(
                    'Invalid rgba string passed to %s: expecting format "#...", "%s" given',
                    __METHOD__, $string
            );
        }

        $string = str_replace('#', '', $string);

        if (strlen($string) === 3) {
            $r = hexdec($string[0] . $string[0]);
            $g = hexdec($string[1] . $string[1]);
            $b = hexdec($string[2] . $string[2]);
        } else {
            $r = hexdec($string[0] . $string[1]);
            $g = hexdec($string[2] . $string[3]);
            $b = hexdec($string[4] . $string[5]);
        }

        return [(int)$r, (int)$g, (int)$b];
    }
934206
function getHex(color) {
    var c = [ color[0], color[1], color[2] ].map(function(val) {
        return Math.floor(val * 255);
    });

    return "#" + ((c[2] | c[1] << 8 | c[0] << 16) | 1 << 24)
        .toString(16)
        .slice(1)
        .toUpperCase();
}
1029607
function(color, alpha) {
            var colorAsHex = (typeof color === "number") ? color : this.hexFromColor(color);
            return this.colorFromHex(colorAsHex, alpha);
        }
1195837
public static function hex2rgb($hex)
    {
        $x = hexdec(substr($hex, 1));
        $b = $x % 256;
        $g = ($x >> 8) % 256;
        $r = ($x >> 16) % 256;

        $rgb = array($r, $g, $b);
        return $rgb;
    }
683334
public static function hsla(Color $color, float $alpha = 1.0)
    {
        $hsl = $color->getHsl();

        return sprintf(
            'hsla(%s, %s%%, %s%%, %s)',
            sprintf('%0.1f', $hsl['h'] * 360) + 0,
            sprintf('%0.1f', $hsl['s'] * 100) + 0,
            sprintf('%0.1f', $hsl['l'] * 100) + 0,
            $alpha + 0
        );
    }
650872
def rgb_to_hex(rgba):
    
    rgba = rgba.split('/')
    hex_value = '#' + rgba[0][:2] + rgba[1][:2] + rgba[2][:2]
    return hex_value
568224
public function fromHex($hex)
    {
        $R = hexdec($hex[0] . $hex[1]);
        $G = hexdec($hex[2] . $hex[3]);
        $B = hexdec($hex[4] . $hex[5]);

        $HSL = array();

        $var_R = ($R / 255);
        $var_G = ($G / 255);
        $var_B = ($B / 255);

        $var_Min = min($var_R, $var_G, $var_B);
        $var_Max = max($var_R, $var_G, $var_B);
        $del_Max = $var_Max - $var_Min;

        $L = ($var_Max + $var_Min) / 2;

        if ($del_Max == 0) {
            $H = 0;
            $S = 0;
        } else {

            if ($L < 0.5) {
                $S = $del_Max / ($var_Max + $var_Min );
            } else {
                 $S = $del_Max / (2 - $var_Max - $var_Min );
            }

            $del_R = ((($var_Max - $var_R ) / 6) + ($del_Max / 2)) / $del_Max;
            $del_G = ((($var_Max - $var_G ) / 6) + ($del_Max / 2)) / $del_Max;
            $del_B = ((($var_Max - $var_B ) / 6) + ($del_Max / 2)) / $del_Max;

            if ($var_R == $var_Max) {
                $H = $del_B - $del_G;
            } elseif ($var_G == $var_Max) {
                $H = (1 / 3) + $del_R - $del_B;
            } else if ($var_B == $var_Max) {
                $H = (2 / 3) + $del_G - $del_R;
            }

            if ($H < 0) {
                $H++;
            }
            if ($H > 1) {
                $H--;
            }
        }

        $this->values = array(
            'hue' => ($H * 360),
            'saturation' => floatval($S),
            'lightness' => floatval($L)
        );
    }
8720
public static function rgbaValueToRgbaComponents($rgb)
    {
        self::throwExceptionIfNotRgbaValue($rgb);

        $alpha = self::rgbaValueToAlphaComponent($rgb);

        if (0 === $alpha) {
            $alpha = 0xFF;
        }

        return [
            'R' => self::rgbaValueToRedComponent($rgb),
            'G' => self::rgbaValueToGreenComponent($rgb),
            'B' => self::rgbaValueToBlueComponent($rgb),
            'A' => $alpha
        ];
    }