/* Hex to RGB conversion. Copyright Tim Down: http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
425490
function (hex) {
                var shortformRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i,
                    longformRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i,
                    rgbParts;

                hex = hex.replace(shortformRegex, function (m, r, g, b) {
                    return r + r + g + g + b + b;
                });

                rgbParts = longformRegex.exec(hex);

                return rgbParts ? [ parseInt(rgbParts[1], 16), parseInt(rgbParts[2], 16), parseInt(rgbParts[3], 16) ] : [ 0, 0, 0 ];
            }
757641
function hex2dec($hex)
		# Purpose:  Convert #hex color to RGB
	{
		$color = str_replace('#', '', $hex);

		if (strlen($color) == 3)
		{
			$color = $color . $color;
		}

		$rgb = array(
			'r' => hexdec(substr($color, 0, 2)),
			'g' => hexdec(substr($color, 2, 2)),
			'b' => hexdec(substr($color, 4, 2)),
			'a' => 0
		);

		return $rgb;
	}
1065171
def hex_to_rgb(hexx):
    
    h = hexx.strip('#')
    l = len(h)

    return tuple(int(h[i:i+l//3], 16) for i in range(0, l, l//3))
379032
def hex2rgb(hexvalue):
    """"""

    # Make sure a possible '#' char is eliminated
    # before processing the color.
    if ('#' in hexvalue):
        hexcolor = hexvalue.replace('#', '')
    else:
        hexcolor = hexvalue

    # Hex colors have a fixed length of 6 characters excluding the '#'
    # TODO: Include custom exception here, even if it should never happen.
    if (len(hexcolor) != 6):
        print ("Unexpected length of hex color value.\nSix characters excluding \'#\' expected.")
        return 0

    # Convert each two characters of
    # the hex to an RGB color value.
    r = int(hexcolor[0:2], HEX_BASE)
    g = int(hexcolor[2:4], HEX_BASE)
    b = int(hexcolor[4:6], HEX_BASE)

    return r, g, b
55866
function rgb2hex(rgbarray) {
    var hex = '#'
    rgbarray.forEach( function (dig) {
      dig = dig.toString(16)
      hex += ("00" + dig).substr( dig.length )
    })
    return hex
  }
767669
public static final String rgbToBrowserHexColor(final int r, final int g, final int b)
    {
        return "#" + toBrowserHexValue(r) + toBrowserHexValue(g) + toBrowserHexValue(b);
    }
56690
def _hexvalue_to_rgb(hexvalue):
        
        r = int(hexvalue[0:2], 16)
        g = int(hexvalue[2:4], 16)
        b = int(hexvalue[4:6], 16)

        return (r, g, b)
1365819
function rgbaToHex(r, g, b, a, allow4Char) {

    var hex = [
        pad2(mathRound(r).toString(16)),
        pad2(mathRound(g).toString(16)),
        pad2(mathRound(b).toString(16)),
        pad2(convertDecimalToHex(a))
    ];

    // Return a 4 character hex if possible
    if (allow4Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1) && hex[3].charAt(0) == hex[3].charAt(1)) {
        return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0) + hex[3].charAt(0);
    }

    return hex.join("");
}
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)
        );

    }
596756
def rgb2termhex(r: int, g: int, b: int) -> str:
    
    incs = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]

    res = []
    parts = r, g, b
    for part in parts:
        if (part < 0) or (part > 255):
            raise ValueError(
                'Expecting 0-255 for RGB code, got: {!r}'.format(parts)
            )
        i = 0
        while i < len(incs) - 1:
            s, b = incs[i], incs[i + 1]  # smaller, bigger
            if s <= part <= b:
                s1 = abs(s - part)
                b1 = abs(b - part)
                if s1 < b1:
                    closest = s
                else:
                    closest = b
                res.append(closest)
                break
            i += 1

    # Convert back into nearest hex value.
    return rgb2hex(*res)
368224
private static String getHexSingle(String hex, int colorIndex) {
		validateHex(hex);

		if (hex.startsWith("#")) {
			hex = hex.substring(1);
		}

		int colorCharacters = 1;
		int numColors = hex.length();
		if (numColors > 4) {
			colorCharacters++;
			numColors /= 2;
		}

		String color = null;
		if (colorIndex >= 0 || numColors > 3) {
			if (numColors > 3) {
				colorIndex++;
			}
			int startIndex = colorIndex;
			if (colorCharacters > 1) {
				startIndex *= 2;
			}
			color = hex.substring(startIndex, startIndex + colorCharacters);
			color = expandShorthandHexSingle(color);
		}

		return color;
	}
734377
function hex2num(hex) {
        if (hex.charAt(0) == "#") {
            hex = hex.slice(1);
        } //Remove the '#' char - if there is one.
        hex = hex.toUpperCase();
        var hex_alphabets = "0123456789ABCDEF";
        var value = new Array(3);
        var k = 0;
        var int1, int2;
        for (var i = 0; i < 6; i += 2) {
            int1 = hex_alphabets.indexOf(hex.charAt(i));
            int2 = hex_alphabets.indexOf(hex.charAt(i + 1));
            value[k] = int1 * 16 + int2;
            k++;
        }
        return value;
    }
296516
def _rgb_to_hex(rgbs):
    """"""
    rgbs, n_dim = _check_color_dim(rgbs)
    return np.array(['#%02x%02x%02x' % tuple((255*rgb[:3]).astype(np.uint8))
                     for rgb in rgbs], '|U7')
859578
function toHex(c) {
    var m = c.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/)
    return (m ? rgb(m[1], m[2], m[3]) : c)
      .replace(/#(\w)(\w)(\w)$/, '#$1$1$2$2$3$3') // short skirt to long jacket
  }
1013355
function setHex(color, hex) {
    hex = hex.replace(/^#/, "");
    var num = parseInt(hex, 16);

    color[0] = (num >> 16 & 255) / 255;
    color[1] = (num >> 8 & 255) / 255;
    color[2] = (num & 255) / 255;
    color[3] = 1

    return color;
}
767711
function hexToRgb(hex) {

  var match = hex.toString(16).match(/[a-f0-9]{6}/i);

  if (!match) {
    return {r: 0, g: 0, b: 0};
  }

  var integer = parseInt(match[0], 16);
  var r = (integer >> 16) & 0xFF;
  var g = (integer >> 8) & 0xFF;
  var b = integer & 0xFF;

  return {
    r: r,
    g: g,
    b: b
  };
}
1118744
function hexaToRgba(hex) {
  if (hex.length === 3) {
    hex += "f"
  }
  if (hex.length === 4) {
    var h0 = hex.charAt(0)
    var h1 = hex.charAt(1)
    var h2 = hex.charAt(2)
    var h3 = hex.charAt(3)
    hex = h0 + h0 + h1 + h1 + h2 + h2 + h3 + h3
  }
  if (hex.length === 6) {
    hex += "ff"
  }
  var rgb = []
  for (var i = 0, l = hex.length; i < l; i += 2) {
    rgb.push(Math.round(parseInt(hex.substr(i, 2), 16) / (i === 6 ? 255 : 1) * DECIMAL_PRECISION) / DECIMAL_PRECISION)
  }

  return color({r: rgb[0], g: rgb[1], b: rgb[2], a: rgb[3]}).rgbaString()
}