|
8 | 8 | from . import util as _util |
9 | 9 |
|
10 | 10 |
|
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] |
| 11 | +def _make_extreme(rgba): |
| 12 | + """Make bright colors darker, dark colors brighter, both less saturated.""" |
| 13 | + # The package `colorspacious` must be installed for this to work: |
| 14 | + from colorspacious import cspace_convert |
| 15 | + lightness_step = 25 |
| 16 | + chroma_factor = 0.7 |
| 17 | + j, c, h = cspace_convert(rgba[:3], 'sRGB1', 'JCh') |
| 18 | + if j > 50: |
| 19 | + j -= lightness_step |
18 | 20 | else: |
19 | | - return [alpha * c + 1 - alpha for c in [r, g, b]] + [a] |
| 21 | + j += lightness_step |
| 22 | + c *= chroma_factor |
| 23 | + rgba[:3] = _np.clip(cspace_convert([j, c, h], 'JCh', 'sRGB1'), 0, 1) |
| 24 | + return rgba |
20 | 25 |
|
21 | 26 |
|
22 | | -def _register_cmap_clip(name, original_name, alpha): |
| 27 | +def _register_cmap_clip(name, original_name): |
23 | 28 | """Create a color map with "over" and "under" values.""" |
24 | 29 | cmap = _plt.get_cmap(original_name) |
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) |
| 30 | + cmap = cmap.with_extremes( |
| 31 | + under=_make_extreme(cmap.get_under()), |
| 32 | + over=_make_extreme(cmap.get_over())) |
28 | 33 | cmap.name = name |
29 | 34 | _plt.colormaps.register(cmap=cmap) |
30 | 35 |
|
31 | 36 |
|
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) |
| 37 | +_register_cmap_clip('cividis_clip', 'cividis') |
| 38 | +_register_cmap_clip('cividis_r_clip', 'cividis_r') |
| 39 | +_register_cmap_clip('inferno_clip', 'inferno') |
| 40 | +_register_cmap_clip('inferno_r_clip', 'inferno_r') |
| 41 | +_register_cmap_clip('magma_clip', 'magma') |
| 42 | +_register_cmap_clip('magma_r_clip', 'magma_r') |
| 43 | +_register_cmap_clip('plasma_clip', 'plasma') |
| 44 | +_register_cmap_clip('plasma_r_clip', 'plasma_r') |
| 45 | +_register_cmap_clip('viridis_clip', 'viridis') |
| 46 | +_register_cmap_clip('viridis_r_clip', 'viridis_r') |
36 | 47 |
|
37 | 48 | # The 'coolwarm' colormap is based on the paper |
38 | 49 | # "Diverging Color Maps for Scientific Visualization" by Kenneth Moreland |
39 | 50 | # https://www.kennethmoreland.com/color-maps/ColorMapsExpanded.pdf |
40 | | -_register_cmap_clip('coolwarm_clip', 'coolwarm', 0.65) |
41 | | -_register_cmap_clip('coolwarm_r_clip', 'coolwarm_r', 0.65) |
| 51 | +_register_cmap_clip('coolwarm_clip', 'coolwarm') |
| 52 | +_register_cmap_clip('coolwarm_r_clip', 'coolwarm_r') |
42 | 53 |
|
43 | 54 |
|
44 | 55 | def _register_cmap_transparent(name, color): |
|
0 commit comments