Correctly resample colorcet colormaps
@@ -522,6 +522,20 @@ def map_colors(arr, crange, cmap, hex=True):
return arr
+def resample_palette(palette, ncolors, categorical, cmap_categorical):
+ """
+ Resample the number of colors in a palette to the selected number.
+ """
+ if len(palette) != ncolors:
+ if categorical and cmap_categorical:
+ palette = [palette[i%len(palette)] for i in range(ncolors)]
+ else:
+ lpad, rpad = -0.5, 0.49999999999
+ indexes = np.linspace(lpad, (len(palette)-1)+rpad, ncolors)
+ palette = [palette[int(np.round(v))] for v in indexes]
+ return palette
+
+
def mplcmap_to_palette(cmap, ncolors=None, categorical=False):
"""
Converts a matplotlib colormap to palette of RGB hex strings."
@@ -550,6 +564,22 @@ def mplcmap_to_palette(cmap, ncolors=None, categorical=False):
return [rgb2hex(c) for c in cmap(np.linspace(0, 1, ncolors))]
+def colorcet_cmap_to_palette(cmap, ncolors=None, categorical=False):
+ from colorcet import palette
+
+ categories = ['glasbey']
+
+ ncolors = ncolors or 256
+ cmap_categorical = any(c in cmap for c in categories)
+
+ if cmap.endswith('_r'):
+ palette = list(reversed(palette[cmap[:-2]]))
+ else:
+ palette = palette[cmap]
+
+ return resample_palette(palette, ncolors, categorical, cmap_categorical)
+
+
def bokeh_palette_to_palette(cmap, ncolors=None, categorical=False):
from bokeh import palettes
@@ -589,14 +619,7 @@ def bokeh_palette_to_palette(cmap, ncolors=None, categorical=False):
palette = palette(ncolors)
if reverse: palette = palette[::-1]
- if len(palette) != ncolors:
- if categorical and cmap_categorical:
- palette = [palette[i%len(palette)] for i in range(ncolors)]
- else:
- lpad, rpad = -0.5, 0.49999999999
- indexes = np.linspace(lpad, (len(palette)-1)+rpad, ncolors)
- palette = [palette[int(np.round(v))] for v in indexes]
- return palette
+ return resample_palette(palette, ncolors, categorical, cmap_categorical)
def linear_gradient(start_hex, finish_hex, n=10):
@@ -870,11 +893,7 @@ def process_cmap(cmap, ncolors=None, provider=None, categorical=False):
elif provider == 'bokeh' or (provider is None and (cmap in bk_cmaps or cmap.capitalize() in bk_cmaps)):
palette = bokeh_palette_to_palette(cmap, ncolors, categorical)
elif provider == 'colorcet' or (provider is None and cmap in cet_cmaps):
- from colorcet import palette
- if cmap.endswith('_r'):
- palette = list(reversed(palette[cmap[:-2]]))
- else:
- palette = palette[cmap]
+ palette = colorcet_cmap_to_palette(cmap, ncolors, categorical)
else:
raise ValueError("Supplied cmap %s not found among %s colormaps." %
(cmap,providers_checked))
