polishing
casting numpy.bool_ to bool is a little bit annoying. Mabye using Union[bool, np.bool_] is the better solution, it does not require any changes to the actual code and there is some hope that it will get fixed in mypy https://github.com/numpy/numpy/issues/18876
This commit is contained in:
parent
bd908dc425
commit
4ba7c9e670
|
@ -3,7 +3,7 @@ import json
|
||||||
import functools
|
import functools
|
||||||
import colorsys
|
import colorsys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Sequence, Union, List, TextIO
|
from typing import Sequence, Union, TextIO
|
||||||
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
@ -128,18 +128,18 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
raise ValueError(f'Invalid color model: {model}.')
|
raise ValueError(f'Invalid color model: {model}.')
|
||||||
|
|
||||||
low_high = np.vstack((low,high))
|
low_high = np.vstack((low,high))
|
||||||
outofbounds = np.bool_(False)
|
out_of_bounds = np.bool_(False)
|
||||||
|
|
||||||
if model.lower() == 'rgb':
|
if model.lower() == 'rgb':
|
||||||
outofbounds = np.any(low_high<0) or np.any(low_high>1)
|
out_of_bounds = np.any(low_high<0) or np.any(low_high>1)
|
||||||
elif model.lower() == 'hsv':
|
elif model.lower() == 'hsv':
|
||||||
outofbounds = np.any(low_high<0) or np.any(low_high>[360,1,1])
|
out_of_bounds = np.any(low_high<0) or np.any(low_high>[360,1,1])
|
||||||
elif model.lower() == 'hsl':
|
elif model.lower() == 'hsl':
|
||||||
outofbounds = np.any(low_high<0) or np.any(low_high>[360,1,1])
|
out_of_bounds = np.any(low_high<0) or np.any(low_high>[360,1,1])
|
||||||
elif model.lower() == 'lab':
|
elif model.lower() == 'lab':
|
||||||
outofbounds = np.any(low_high[:,0]<0)
|
out_of_bounds = np.any(low_high[:,0]<0)
|
||||||
|
|
||||||
if outofbounds:
|
if out_of_bounds:
|
||||||
raise ValueError(f'{model.upper()} colors {low} | {high} are out of bounds.')
|
raise ValueError(f'{model.upper()} colors {low} | {high} are out of bounds.')
|
||||||
|
|
||||||
low_,high_ = map(toMsh[model.lower()],low_high)
|
low_,high_ = map(toMsh[model.lower()],low_high)
|
||||||
|
@ -162,9 +162,9 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
name : str
|
name : str
|
||||||
Name of the colormap.
|
Name of the colormap.
|
||||||
N : int, optional
|
N : int, optional
|
||||||
Number of color quantization levels. Defaults to 256.
|
Number of color quantization levels. Defaults to 256.
|
||||||
This parameter is not used for matplotlib colormaps
|
This parameter is not used for matplotlib colormaps
|
||||||
that are of type `ListedColormap`.
|
that are of type `ListedColormap`.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
|
@ -260,8 +260,8 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
|
|
||||||
|
|
||||||
def _get_file_handle(self,
|
def _get_file_handle(self,
|
||||||
fname: Union[TextIO, str, Path, None] = None,
|
fname: Union[TextIO, str, Path, None],
|
||||||
suffix: str = None) -> TextIO:
|
suffix: str) -> TextIO:
|
||||||
"""
|
"""
|
||||||
Provide filehandle.
|
Provide filehandle.
|
||||||
|
|
||||||
|
@ -270,7 +270,7 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
fname : file, str, pathlib.Path, or None
|
fname : file, str, pathlib.Path, or None
|
||||||
Filename or filehandle.
|
Filename or filehandle.
|
||||||
If None, colormap name + suffix.
|
If None, colormap name + suffix.
|
||||||
suffix: str, optional
|
suffix: str
|
||||||
Extension to use for colormap filename.
|
Extension to use for colormap filename.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
|
@ -298,7 +298,7 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
consist of the name of the colormap with extension '.json'.
|
consist of the name of the colormap with extension '.json'.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
colors: List = []
|
colors = []
|
||||||
for i,c in enumerate(np.round(self.colors,6).tolist()):
|
for i,c in enumerate(np.round(self.colors,6).tolist()):
|
||||||
colors+=[i]+c
|
colors+=[i]+c
|
||||||
|
|
||||||
|
@ -369,7 +369,7 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _interpolate_msh(frac: Union[float,int],
|
def _interpolate_msh(frac: float,
|
||||||
low: np.ndarray,
|
low: np.ndarray,
|
||||||
high: np.ndarray) -> np.ndarray:
|
high: np.ndarray) -> np.ndarray:
|
||||||
"""
|
"""
|
||||||
|
@ -449,24 +449,76 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _hsv2rgb(hsv: np.ndarray) -> np.ndarray:
|
def _hsv2rgb(hsv: np.ndarray) -> np.ndarray:
|
||||||
"""Hue Saturation Value to Red Green Blue."""
|
"""
|
||||||
|
Hue Saturation Value to Red Green Blue.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
hsv : numpy.ndarray of shape (3)
|
||||||
|
HSV values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
rgb : numpy.ndarray of shape (3)
|
||||||
|
RGB values.
|
||||||
|
|
||||||
|
"""
|
||||||
return np.array(colorsys.hsv_to_rgb(hsv[0]/360.,hsv[1],hsv[2]))
|
return np.array(colorsys.hsv_to_rgb(hsv[0]/360.,hsv[1],hsv[2]))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _rgb2hsv(rgb: np.ndarray) -> np.ndarray:
|
def _rgb2hsv(rgb: np.ndarray) -> np.ndarray:
|
||||||
"""Red Green Blue to Hue Saturation Value."""
|
"""
|
||||||
|
Red Green Blue to Hue Saturation Value.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
rgb : numpy.ndarray of shape (3)
|
||||||
|
RGB values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
hsv : numpy.ndarray of shape (3)
|
||||||
|
HSV values.
|
||||||
|
|
||||||
|
"""
|
||||||
h,s,v = colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2])
|
h,s,v = colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2])
|
||||||
return np.array([h*360,s,v])
|
return np.array([h*360,s,v])
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _hsl2rgb(hsl: np.ndarray) -> np.ndarray:
|
def _hsl2rgb(hsl: np.ndarray) -> np.ndarray:
|
||||||
"""Hue Saturation Luminance to Red Green Blue."""
|
"""
|
||||||
|
Hue Saturation Luminance to Red Green Blue.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
hsl : numpy.ndarray of shape (3)
|
||||||
|
HSL values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
rgb : numpy.ndarray of shape (3)
|
||||||
|
RGB values.
|
||||||
|
|
||||||
|
"""
|
||||||
return np.array(colorsys.hls_to_rgb(hsl[0]/360.,hsl[2],hsl[1]))
|
return np.array(colorsys.hls_to_rgb(hsl[0]/360.,hsl[2],hsl[1]))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _rgb2hsl(rgb: np.ndarray) -> np.ndarray:
|
def _rgb2hsl(rgb: np.ndarray) -> np.ndarray:
|
||||||
"""Red Green Blue to Hue Saturation Luminance."""
|
"""
|
||||||
|
Red Green Blue to Hue Saturation Luminance.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
rgb : numpy.ndarray of shape (3)
|
||||||
|
RGB values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
hsl : numpy.ndarray of shape (3)
|
||||||
|
HSL values.
|
||||||
|
|
||||||
|
"""
|
||||||
h,l,s = colorsys.rgb_to_hls(rgb[0],rgb[1],rgb[2])
|
h,l,s = colorsys.rgb_to_hls(rgb[0],rgb[1],rgb[2])
|
||||||
return np.array([h*360,s,l])
|
return np.array([h*360,s,l])
|
||||||
|
|
||||||
|
@ -476,6 +528,16 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
"""
|
"""
|
||||||
CIE Xyz to Red Green Blue.
|
CIE Xyz to Red Green Blue.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
xyz : numpy.ndarray of shape (3)
|
||||||
|
CIE Xyz values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
rgb : numpy.ndarray of shape (3)
|
||||||
|
RGB values.
|
||||||
|
|
||||||
References
|
References
|
||||||
----------
|
----------
|
||||||
https://www.easyrgb.com/en/math.php
|
https://www.easyrgb.com/en/math.php
|
||||||
|
@ -496,6 +558,16 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
"""
|
"""
|
||||||
Red Green Blue to CIE Xyz.
|
Red Green Blue to CIE Xyz.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
rgb : numpy.ndarray of shape (3)
|
||||||
|
RGB values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
xyz : numpy.ndarray of shape (3)
|
||||||
|
CIE Xyz values.
|
||||||
|
|
||||||
References
|
References
|
||||||
----------
|
----------
|
||||||
https://www.easyrgb.com/en/math.php
|
https://www.easyrgb.com/en/math.php
|
||||||
|
@ -514,6 +586,16 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
"""
|
"""
|
||||||
CIE Lab to CIE Xyz.
|
CIE Lab to CIE Xyz.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
lab : numpy.ndarray of shape (3)
|
||||||
|
CIE lab values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
xyz : numpy.ndarray of shape (3)
|
||||||
|
CIE Xyz values.
|
||||||
|
|
||||||
References
|
References
|
||||||
----------
|
----------
|
||||||
http://www.brucelindbloom.com/index.html?Eqn_Lab_to_XYZ.html
|
http://www.brucelindbloom.com/index.html?Eqn_Lab_to_XYZ.html
|
||||||
|
@ -533,6 +615,16 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
"""
|
"""
|
||||||
CIE Xyz to CIE Lab.
|
CIE Xyz to CIE Lab.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
xyz : numpy.ndarray of shape (3)
|
||||||
|
CIE Xyz values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
lab : numpy.ndarray of shape (3)
|
||||||
|
CIE lab values.
|
||||||
|
|
||||||
References
|
References
|
||||||
----------
|
----------
|
||||||
http://www.brucelindbloom.com/index.html?Eqn_Lab_to_XYZ.html
|
http://www.brucelindbloom.com/index.html?Eqn_Lab_to_XYZ.html
|
||||||
|
@ -553,6 +645,16 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
"""
|
"""
|
||||||
CIE Lab to Msh.
|
CIE Lab to Msh.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
lab : numpy.ndarray of shape (3)
|
||||||
|
CIE lab values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
msh : numpy.ndarray of shape (3)
|
||||||
|
Msh values.
|
||||||
|
|
||||||
References
|
References
|
||||||
----------
|
----------
|
||||||
https://www.kennethmoreland.com/color-maps/ColorMapsExpanded.pdf
|
https://www.kennethmoreland.com/color-maps/ColorMapsExpanded.pdf
|
||||||
|
@ -571,6 +673,16 @@ class Colormap(mpl.colors.ListedColormap):
|
||||||
"""
|
"""
|
||||||
Msh to CIE Lab.
|
Msh to CIE Lab.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
msh : numpy.ndarray of shape (3)
|
||||||
|
Msh values.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
lab : numpy.ndarray of shape (3)
|
||||||
|
CIE lab values.
|
||||||
|
|
||||||
References
|
References
|
||||||
----------
|
----------
|
||||||
https://www.kennethmoreland.com/color-maps/ColorMapsExpanded.pdf
|
https://www.kennethmoreland.com/color-maps/ColorMapsExpanded.pdf
|
||||||
|
|
Loading…
Reference in New Issue