Skip to content

Commit

Permalink
auto color model detection
Browse files Browse the repository at this point in the history
  • Loading branch information
hagne committed Mar 4, 2024
1 parent aeb7bd8 commit 37af852
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions atmPy/tools/plt_tool_kit/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,42 @@ def _rgb_to_hex(rgb):


class Color(object):
def __init__(self, color, model='hsv', color_scale = 1):
def __init__(self, color, model=None, color_scale = 1):
"""
Parameters
----------
color: depending ond model
model: (['hsv'], 'rgb', 'hex')
model: (['rgb'], 'hsv', 'hex', 'gray')
If left None it will try to infer. RGB and HSV can not be distinguished and rgb will be chosen by default.
color_scale:
ignored when model == 'hex'
"""
if type(color).__name__ in ['list', 'tuple']:
color = _np.array(color)
color = _np.array(color, dtype = float)

if isinstance(model, type(None)):
if isinstance(color, _np.ndarray):
model = 'rgb'
elif isinstance(color, str):
try:
color = float(color)
model = 'gray'
except ValueError:
if len(color) == 7:
model = 'hex'
else:
raise ValueError(f'{color} was neither recognized as gray nor as hex!')
else:
raise ValueError(f'Could not identify {color} as a particular color model.')


if model == 'gray':
model = 'rgb'
color = _np.array([color,]*3)


if model != 'hex':
color = color.astype(_np.float)
# color = color.astype(_np.float)
color /= color_scale

if len(color) == 4:
Expand Down

0 comments on commit 37af852

Please sign in to comment.