Created
June 14, 2021 17:07
-
-
Save votti/eb00a84c89d91febb34fe38cb2dfb0ba to your computer and use it in GitHub Desktop.
Convert a colormap to BW based on the lightness component of LAB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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