Skip to content

Commit d600d26

Browse files
committed
Mix dark colors with white, bright colors with black
1 parent 4c18d0c commit d600d26

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

sfs/plot2d.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,37 @@
88
from . import util as _util
99

1010

11-
def _register_cmap_clip(name, original_name, col_alpha_u, col_apha_o):
11+
def _apply_alpha(rgba, alpha):
12+
"""Mix dark colors with white, bright colors with black."""
13+
from colorsys import rgb_to_hls
14+
r, g, b, a = rgba
15+
_, l, _ = rgb_to_hls(r, g, b)
16+
if l > 0.5:
17+
return [alpha * c for c in [r, g, b]] + [a]
18+
else:
19+
return [alpha * c + 1 - alpha for c in [r, g, b]] + [a]
20+
21+
22+
def _register_cmap_clip(name, original_name, alpha):
1223
"""Create a color map with "over" and "under" values."""
1324
cmap = _plt.get_cmap(original_name)
14-
cmap = cmap.with_extremes(
15-
under=cmap.get_under() * col_alpha_u,
16-
over=cmap.get_over() * col_apha_o)
25+
over = _apply_alpha(cmap.get_over(), alpha)
26+
under = _apply_alpha(cmap.get_under(), alpha)
27+
cmap = cmap.with_extremes(under=under, over=over)
1728
cmap.name = name
1829
_plt.colormaps.register(cmap=cmap)
1930

2031

21-
_register_cmap_clip('cividis_clip', 'cividis',
22-
[1, 1, 1, 0.6],[1, 1, 1, 0.1])
23-
_register_cmap_clip('cividis_r_clip', 'cividis_r',
24-
[1, 1, 1, 0.1], [1, 1, 1, 0.6])
25-
_register_cmap_clip('viridis_clip', 'viridis',
26-
[1, 1, 1, 0.6], [1, 1, 1, 0.1])
27-
_register_cmap_clip('viridis_r_clip', 'viridis_r',
28-
[1, 1, 1, 0.1], [1, 1, 1, 0.6])
32+
_register_cmap_clip('cividis_clip', 'cividis', 0.8)
33+
_register_cmap_clip('cividis_r_clip', 'cividis_r', 0.8)
34+
_register_cmap_clip('viridis_clip', 'viridis', 0.8)
35+
_register_cmap_clip('viridis_r_clip', 'viridis_r', 0.8)
2936

3037
# The 'coolwarm' colormap is based on the paper
3138
# "Diverging Color Maps for Scientific Visualization" by Kenneth Moreland
3239
# https://www.kennethmoreland.com/color-maps/ColorMapsExpanded.pdf
33-
_register_cmap_clip('coolwarm_clip', 'coolwarm',
34-
[1, 1, 1, 0.5], [1, 1, 1, 0.5])
35-
_register_cmap_clip('coolwarm_r_clip', 'coolwarm_r',
36-
[1, 1, 1, 0.5], [1, 1, 1, 0.5])
40+
_register_cmap_clip('coolwarm_clip', 'coolwarm', 0.65)
41+
_register_cmap_clip('coolwarm_r_clip', 'coolwarm_r', 0.65)
3742

3843

3944
def _register_cmap_transparent(name, color):

0 commit comments

Comments
 (0)