Skip to content

Commit 3284bed

Browse files
committed
Calculate under/over values with colorspacious
1 parent d600d26 commit 3284bed

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

sfs/plot2d.py

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

1010

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
1820
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
2025

2126

22-
def _register_cmap_clip(name, original_name, alpha):
27+
def _register_cmap_clip(name, original_name):
2328
"""Create a color map with "over" and "under" values."""
2429
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()))
2833
cmap.name = name
2934
_plt.colormaps.register(cmap=cmap)
3035

3136

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')
3647

3748
# The 'coolwarm' colormap is based on the paper
3849
# "Diverging Color Maps for Scientific Visualization" by Kenneth Moreland
3950
# 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')
4253

4354

4455
def _register_cmap_transparent(name, color):

0 commit comments

Comments
 (0)