10 examples of 'hex to rgb python' in Python

Every line of 'hex to rgb python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
418def hex2rgb(self, rgbhex):
419 rgbhex = rgbhex.lstrip('#')
420 lv = len(rgbhex)
421 return tuple(int(rgbhex[i:i + 2], 16) for i in range(0, lv, 2))
78def rgb_to_hex(rgb):
79 return '#' + ''.join(int_to_hex(x) for x in rgb)
11def hex_to_rgb(hex):
12
13
14 hex = hex.lstrip("#")
15 if len(hex) < 6:
16 hex += hex[-1] * (6-len(hex))
17
18 r, g, b = hex[0:2], hex[2:4], hex[4:]
19 r, g, b = [int(n, 16) for n in (r, g, b)]
20
21 return (r/255.0, g/255.0, b/255.0)
6def rgb_to_hex(rgb):
7 rgb_int = []
8 for elem in rgb:
9 rgb_int.append(int((elem + 0.002) * 255))
10 rgb_int = tuple(rgb_int)
11 hex_result = '%02x%02x%02x' % rgb_int
12 return hex_result
30def hex_to_rgb(h):
31 s = color_humanize(h)
32 return (int(s[1:3], 16), int(s[3:5], 16), int(s[5:7], 16))
32def rgb2hex(r, g, b):
33 return (r << 16) + (g << 8) + b
41def hex2rgb(color_hex):
42 """
43 Convert hex color code to rgb tuple.
44
45 >>> hex2rgb('#819a46')
46 (129, 154, 70)
47 """
48 code = color_hex[1:]
49 r = int(code[0:2], 16)
50 g = int(code[2:4], 16)
51 b = int(code[4:6], 16)
52 return r, g, b
159def RGB255hex(rgb):
160 """Make a color in the form #rrggbb in hex from r,g,b in 0-255"""
161 return "#{}".format("".join(["{:02x}".format(a) for a in rgb]))
3def hex2rgb(c, alpha=1.0):
4 c = c.lstrip("#");
5 assert(len(c) == 6);
6 return [int(c[0:2], 16) / 255.0,
7 int(c[2:4], 16) / 255.0,
8 int(c[4:6], 16) / 255.0,
9 alpha];
118def rgb_to_hex(rgb: Union[Tuple[int, int, int], Tuple[float, float, float]]) -> Color:
119 assert all(0 <= x <= 255 for x in rgb), f'invalid RGB color: {rgb}'
120 r, g, b = rgb
121 return f'#{r:02x}{g:02x}{b:02x}'

Related snippets