Skip to content

Commit

Permalink
Factored out rgba_tuple helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Feb 23, 2017
1 parent 3947bd1 commit a8f0e1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 3 additions & 5 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
from ..util import dynamic_update, get_sources
from .plot import BokehPlot
from .util import (mpl_to_bokeh, convert_datetime, update_plot, get_tab_title,
bokeh_version, mplcmap_to_palette, py2js_tickformatter)
bokeh_version, mplcmap_to_palette, py2js_tickformatter,
rgba_tuple)

if bokeh_version >= '0.12':
from bokeh.models import FuncTickFormatter
Expand Down Expand Up @@ -891,10 +892,7 @@ def _get_colormapper(self, dim, element, ranges, style, factors=None):
ncolors = None if factors is None else len(factors)
low, high = ranges.get(dim.name, element.range(dim.name))
palette = mplcmap_to_palette(style.pop('cmap', 'viridis'), ncolors)

color_rgb = lambda color: [int(c*255) if i<3 else c for i, c in enumerate(color)]
colors = {k: color_rgb(v) if isinstance(v, tuple) else v
for k, v in self.clipping_colors.items()}
colors = {k: rgba_tuple(v) for k, v in self.clipping_colors.items()}

if factors is None:
colormapper = LogColorMapper if self.logz else LinearColorMapper
Expand Down
10 changes: 10 additions & 0 deletions holoviews/plotting/bokeh/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def rgb2hex(rgb):
return "#{0:02x}{1:02x}{2:02x}".format(*(int(v*255) for v in rgb))


def rgba_tuple(rgba):
"""
Ensures RGB(A) tuples in the range 0-1 are scaled to 0-255.
"""
if isinstance(rgba, tuple):
return [int(c*255) if i<3 else c for i, c in enumerate(rgba)]
else:
return rgba


def mplcmap_to_palette(cmap, ncolors=None):
"""
Converts a matplotlib colormap to palette of RGB hex strings."
Expand Down

0 comments on commit a8f0e1d

Please sign in to comment.