Skip to content

Commit

Permalink
add support for cmap
Browse files Browse the repository at this point in the history
  • Loading branch information
guiwitz committed Dec 16, 2024
1 parent 8d823d2 commit 35df38e
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions microfilm/colorify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from matplotlib.colors import ListedColormap
from skimage.color import hsv2rgb
from skimage.exposure import rescale_intensity
from cmap import Colormap

def cmaps_def(cmap_name, num_colors=256, flip_map=False):
"""
Expand All @@ -28,27 +29,29 @@ def cmaps_def(cmap_name, num_colors=256, flip_map=False):
"""

if cmap_name in plt.colormaps():
cmap = plt.get_cmap(cmap_name, num_colors)
elif cmap_name == 'pure_red':
cmap = ListedColormap(np.c_[np.linspace(0,1,num_colors), np.zeros(num_colors), np.zeros(num_colors)])
elif cmap_name == 'pure_green':
cmap = ListedColormap(np.c_[np.zeros(num_colors), np.linspace(0,1,num_colors), np.zeros(num_colors)])
elif cmap_name == 'pure_blue':
cmap = ListedColormap(np.c_[np.zeros(num_colors), np.zeros(num_colors), np.linspace(0,1,num_colors)])
elif cmap_name == 'pure_cyan':
cmap = ListedColormap(np.c_[np.zeros(num_colors), np.linspace(0,1,num_colors), np.linspace(0,1,num_colors)])
elif cmap_name == 'pure_magenta':
cmap = ListedColormap(np.c_[np.linspace(0,1,num_colors), np.zeros(num_colors), np.linspace(0,1,num_colors)])
elif cmap_name == 'pure_yellow':
cmap = ListedColormap(np.c_[np.linspace(0,1,num_colors), np.linspace(0,1,num_colors), np.zeros(num_colors)])
elif cmap_name == 'segmentation':
cmap = random_cmap(num_colors=num_colors)
elif cmap_name == 'ran_gradient':
cmap = random_grandient_cmap(num_colors=num_colors)
else:
raise Exception(f"Your colormap {cmap_name} doesn't exist either in Matplotlib or microfilm.")

try:
cmap = Colormap(cmap_name)
cmap = cmap.to_matplotlib(N=num_colors)
except ValueError:
if cmap_name == 'pure_red':
cmap = ListedColormap(np.c_[np.linspace(0,1,num_colors), np.zeros(num_colors), np.zeros(num_colors)])
elif cmap_name == 'pure_green':
cmap = ListedColormap(np.c_[np.zeros(num_colors), np.linspace(0,1,num_colors), np.zeros(num_colors)])
elif cmap_name == 'pure_blue':
cmap = ListedColormap(np.c_[np.zeros(num_colors), np.zeros(num_colors), np.linspace(0,1,num_colors)])
elif cmap_name == 'pure_cyan':
cmap = ListedColormap(np.c_[np.zeros(num_colors), np.linspace(0,1,num_colors), np.linspace(0,1,num_colors)])
elif cmap_name == 'pure_magenta':
cmap = ListedColormap(np.c_[np.linspace(0,1,num_colors), np.zeros(num_colors), np.linspace(0,1,num_colors)])
elif cmap_name == 'pure_yellow':
cmap = ListedColormap(np.c_[np.linspace(0,1,num_colors), np.linspace(0,1,num_colors), np.zeros(num_colors)])
elif cmap_name == 'segmentation':
cmap = random_cmap(num_colors=num_colors)
elif cmap_name == 'ran_gradient':
cmap = random_grandient_cmap(num_colors=num_colors)
else:
raise Exception(f"Your colormap {cmap_name} doesn't exist either in Matplotlib or microfilm.")

if flip_map:
cmap = cmap.reversed()

Expand Down

0 comments on commit 35df38e

Please sign in to comment.