Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if isinstance(color, str) or (np.iterable(color) and len(color) == 2):
try:
color = mcolors.to_rgb(color) # ensure is valid color
except Exception:
raise ValueError(f'Invalid RGB argument "{color}".')
elif space == 'rgb':
color = color[:3] # trim alpha
try:
if any(c > 1 for c in color):
color = [c/255 for c in color] # scale to within 0-1
color = tuple(color)
except Exception:
raise ValueError(f'Invalid RGB argument {color}.')
# Translate from other colorspaces
elif space == 'hsv':
color = colormath.hsl_to_rgb(*color)
elif space == 'hpl':
color = colormath.hpluv_to_rgb(*color)
elif space == 'hsl':
color = colormath.hsluv_to_rgb(*color)
elif space == 'hcl':
color = colormath.hcl_to_rgb(*color)
else:
raise ValueError('Invalid color "{color}" for colorspace "{space}".')
return color
def to_xyz(color, space):
"""Translates from the RGB colorspace to colorspace `space`. Inverse
of `to_rgb`."""
# Run tuple conversions
# NOTE: Don't pass color tuple, because we may want to permit out-of-bounds RGB values to invert conversion
color = to_rgb(color)
if space == 'hsv':
color = colormath.rgb_to_hsl(*color) # rgb_to_hsv would also work
elif space == 'hpl':
color = colormath.rgb_to_hpluv(*color)
elif space == 'hsl':
color = colormath.rgb_to_hsluv(*color)
elif space == 'hcl':
color = colormath.rgb_to_hcl(*color)
elif space == 'rgb':
pass
else:
raise ValueError(f'Invalid colorspace {space}.')
return color
def to_xyz(color, space):
"""Translates from the RGB colorspace to colorspace `space`. Inverse
of `to_rgb`."""
# Run tuple conversions
# NOTE: Don't pass color tuple, because we may want to permit out-of-bounds RGB values to invert conversion
color = to_rgb(color)
if space == 'hsv':
color = colormath.rgb_to_hsl(*color) # rgb_to_hsv would also work
elif space == 'hpl':
color = colormath.rgb_to_hpluv(*color)
elif space == 'hsl':
color = colormath.rgb_to_hsluv(*color)
elif space == 'hcl':
color = colormath.rgb_to_hcl(*color)
elif space == 'rgb':
pass
else:
raise ValueError(f'Invalid colorspace {space}.')
return color
def shade(color, shade=0.5):
"""Changes the "shade" of a color by scaling its luminance channel by `shade`."""
try:
color = mcolors.to_rgb(color) # ensure is valid color
except Exception:
raise ValueError(f'Invalid RGBA argument {color}. Registered colors are: {", ".join(mcolors._colors_full_map.keys())}.')
color = [*colormath.rgb_to_hsl(*color)]
color[2] = max([0, min([color[2]*shade, 100])]) # multiply luminance by this value
color = [*colormath.hsl_to_rgb(*color)]
return tuple(color)
color = color[:3] # trim alpha
try:
if any(c > 1 for c in color):
color = [c/255 for c in color] # scale to within 0-1
color = tuple(color)
except Exception:
raise ValueError(f'Invalid RGB argument {color}.')
# Translate from other colorspaces
elif space == 'hsv':
color = colormath.hsl_to_rgb(*color)
elif space == 'hpl':
color = colormath.hpluv_to_rgb(*color)
elif space == 'hsl':
color = colormath.hsluv_to_rgb(*color)
elif space == 'hcl':
color = colormath.hcl_to_rgb(*color)
else:
raise ValueError('Invalid color "{color}" for colorspace "{space}".')
return color
strings to tuple. Inverse of `to_xyz`."""
# First the RGB input
# NOTE: Need isinstance here because strings stored in numpy arrays
# are actually subclasses thereof!
if isinstance(color, str):
try:
color = mcolors.to_rgb(color) # ensure is valid color
except Exception:
raise ValueError(f'Invalid RGBA argument {color}. Registered colors are: {", ".join(mcolors._colors_full_map.keys())}.')
elif space=='rgb':
color = color[:3] # trim alpha
if any(c>1 for c in color):
color = [c/255 for c in color] # scale to within 0-1
# Next the perceptually uniform versions
elif space=='hsv':
color = colormath.hsl_to_rgb(*color)
elif space=='hpl':
color = colormath.hpluv_to_rgb(*color)
elif space=='hsl':
color = colormath.hsluv_to_rgb(*color)
elif space=='hcl':
color = colormath.hcl_to_rgb(*color)
else:
raise ValueError('Invalid RGB value.')
return color
def to_xyz(color, space):
"""Translates from RGB space to colorspace `space`. Inverse of `to_rgb`."""
# Run tuple conversions
# NOTE: Don't pass color tuple, because we may want to permit out-of-bounds RGB values to invert conversion
if isinstance(color, str):
color = mcolors.to_rgb(color) # convert string
else:
color = color[:3]
if space=='hsv':
color = colormath.rgb_to_hsl(*color) # rgb_to_hsv would also work
elif space=='hpl':
color = colormath.rgb_to_hpluv(*color)
elif space=='hsl':
color = colormath.rgb_to_hsluv(*color)
elif space=='hcl':
color = colormath.rgb_to_hcl(*color)
elif space=='rgb':
pass
else:
raise ValueError(f'Invalid colorspace {space}.')
return color
def shade(color, scale=0.5):
"""Changes the "shade" of a color by scaling its luminance channel by `scale`."""
color = to_rgb(color) # ensure is valid color
color = [*colormath.rgb_to_hsl(*color)]
color[2] = max(0, min(color[2]*scale, 100)) # multiply luminance by this value
color = [*colormath.hsl_to_rgb(*color)]
return tuple(color)
# NOTE: Need isinstance here because strings stored in numpy arrays
# are actually subclasses thereof!
if isinstance(color, str):
try:
color = mcolors.to_rgb(color) # ensure is valid color
except Exception:
raise ValueError(f'Invalid RGBA argument {color}. Registered colors are: {", ".join(mcolors._colors_full_map.keys())}.')
elif space=='rgb':
color = color[:3] # trim alpha
if any(c>1 for c in color):
color = [c/255 for c in color] # scale to within 0-1
# Next the perceptually uniform versions
elif space=='hsv':
color = colormath.hsl_to_rgb(*color)
elif space=='hpl':
color = colormath.hpluv_to_rgb(*color)
elif space=='hsl':
color = colormath.hsluv_to_rgb(*color)
elif space=='hcl':
color = colormath.hcl_to_rgb(*color)
else:
raise ValueError('Invalid RGB value.')
return color
color = mcolors.to_rgb(color) # ensure is valid color
except Exception:
raise ValueError(f'Invalid RGBA argument {color}. Registered colors are: {", ".join(mcolors._colors_full_map.keys())}.')
elif space=='rgb':
color = color[:3] # trim alpha
if any(c>1 for c in color):
color = [c/255 for c in color] # scale to within 0-1
# Next the perceptually uniform versions
elif space=='hsv':
color = colormath.hsl_to_rgb(*color)
elif space=='hpl':
color = colormath.hpluv_to_rgb(*color)
elif space=='hsl':
color = colormath.hsluv_to_rgb(*color)
elif space=='hcl':
color = colormath.hcl_to_rgb(*color)
else:
raise ValueError('Invalid RGB value.')
return color