Skip to content

Instantly share code, notes, and snippets.

@votti
Created June 14, 2021 17:07
Show Gist options
  • Save votti/eb00a84c89d91febb34fe38cb2dfb0ba to your computer and use it in GitHub Desktop.
Save votti/eb00a84c89d91febb34fe38cb2dfb0ba to your computer and use it in GitHub Desktop.
Convert a colormap to BW based on the lightness component of LAB
from skimage import color
def cmap_to_bw(cmap, minlight = 60):
"""
Converts a hex colormap to a colormap of contrasting black and white colors.
Args:
cmap: a colormap as iterable of hex values, eg bokeh.palettes.plasma(10)
minlight: a threshold for the lightness
"""
cols = [tuple(int(h[i+1:i+3], 16)/256 for i in (0, 2, 4)) for h in cmap]
lightness = color.rgb2lab(cols)
return ["#000000"
if col[0] > minlight
else "#ffffff"
for col in lightness]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment