|
8 | 8 | from . import util as _util |
9 | 9 |
|
10 | 10 |
|
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): |
12 | 23 | """Create a color map with "over" and "under" values.""" |
13 | 24 | 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) |
17 | 28 | cmap.name = name |
18 | 29 | _plt.colormaps.register(cmap=cmap) |
19 | 30 |
|
20 | 31 |
|
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) |
29 | 36 |
|
30 | 37 | # The 'coolwarm' colormap is based on the paper |
31 | 38 | # "Diverging Color Maps for Scientific Visualization" by Kenneth Moreland |
32 | 39 | # 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) |
37 | 42 |
|
38 | 43 |
|
39 | 44 | def _register_cmap_transparent(name, color): |
|
0 commit comments