Simple and small performance improvements

This commit is contained in:
Grant Sanderson
2018-08-18 20:13:49 -07:00
parent 6f57a8bc5e
commit 039f7ffe58
3 changed files with 29 additions and 5 deletions

View File

@ -11,9 +11,12 @@ from utils.simple_functions import clip_in_place
def color_to_rgb(color):
if not isinstance(color, Color):
color = Color(color)
return np.array(color.get_rgb())
if isinstance(color, str):
return hex_to_rgb(color)
elif isinstance(color, Color):
return np.array(color.get_rgb())
else:
raise Exception("Invalid color type")
def color_to_rgba(color, alpha=1):
@ -35,6 +38,16 @@ def rgb_to_hex(rgb):
return "#" + "".join('%02x' % int(255 * x) for x in rgb)
def hex_to_rgb(hex_code):
hex_part = hex_code[1:]
if len(hex_part) == 3:
"".join([2 * c for c in hex_part])
return np.array([
int(hex_part[i:i + 2], 16) / 255
for i in range(0, 6, 2)
])
def invert_color(color):
return rgb_to_color(1.0 - color_to_rgb(color))