14
14
from matplotlib import rcParams
15
15
from .internals import ic # noqa: F401
16
16
from .internals import docstring , warnings , _not_none
17
- from .utils import to_rgb , to_xyz
17
+ from .utils import to_rgb , to_rgba , to_xyz , to_xyza
18
18
if hasattr (mcm , '_cmap_registry' ):
19
19
_cmap_database_attr = '_cmap_registry'
20
20
else :
@@ -113,7 +113,7 @@ def _get_channel(color, channel, space='hcl'):
113
113
Parameters
114
114
----------
115
115
color : color-spec
116
- The color. Sanitized with `to_rgb `.
116
+ The color. Sanitized with `to_rgba `.
117
117
channel : {'hue', 'chroma', 'saturation', 'luminance'}
118
118
The HCL channel to be retrieved.
119
119
space : {'hcl', 'hpl', 'hsl', 'hsv', 'rgb'}, optional
@@ -622,7 +622,7 @@ def append(self, *args, ratios=None, name=None, N=None, **kwargs):
622
622
Relative extent of each component colormap in the merged colormap.
623
623
Length must equal ``len(args) + 1``.
624
624
625
- For example, ``cmap1.append(cmap2, ratios=[2,1] )`` generates
625
+ For example, ``cmap1.append(cmap2, ratios=(2, 1) )`` generates
626
626
a colormap with the left two-thrids containing colors from
627
627
``cmap1`` and the right one-third containing colors from ``cmap2``.
628
628
name : str, optional
@@ -898,16 +898,27 @@ def save(self, path=None, alpha=True):
898
898
fh .write (data )
899
899
print (f'Saved colormap to { filename !r} .' )
900
900
901
- def set_alpha (self , alpha ):
901
+ def set_alpha (self , alpha , coords = None , ratios = None ):
902
902
"""
903
- Set the opacity for the entire colormap.
903
+ Set the opacity for the entire colormap or set up an opacity gradation .
904
904
905
905
Parameters
906
906
----------
907
- alpha : float
908
- The opacity.
907
+ alpha : float or list of float
908
+ If float, this is the opacity for the entire colormap. If list of
909
+ float, the colormap traverses these opacity values.
910
+ coords : list of float, optional
911
+ Colormap coordinates for the opacity values. The first and last
912
+ coordinates must be ``0`` and ``1``. If `alpha` is not scalar, the
913
+ default coordinates are ``np.linspace(0, 1, len(alpha))``.
914
+ ratios : list of float, optional
915
+ Relative extent of each opacity transition segment. Length should
916
+ equal ``len(alpha) + 1``. For example
917
+ ``cmap.set_alpha((1, 1, 0), ratios=(2, 1))`` creates a transtion from
918
+ 100 percent to 0 percent opacity in the right *third* of the colormap.
909
919
"""
910
- self ._segmentdata ['alpha' ] = [(0 , alpha , alpha ), (1 , alpha , alpha )]
920
+ alpha = _make_segmentdata_array (alpha , coords = coords , ratios = ratios )
921
+ self ._segmentdata ['alpha' ] = alpha
911
922
self ._isinit = False
912
923
913
924
def set_cyclic (self , b ):
@@ -1155,7 +1166,7 @@ def from_list(cls, name, colors, ratios=None, **kwargs):
1155
1166
and not isinstance (colors [0 ], str )
1156
1167
):
1157
1168
coords , colors = zip (* colors )
1158
- colors = [to_rgb (color , alpha = True ) for color in colors ]
1169
+ colors = [to_rgba (color ) for color in colors ]
1159
1170
1160
1171
# Build segmentdata
1161
1172
keys = ('red' , 'green' , 'blue' , 'alpha' )
@@ -1568,9 +1579,9 @@ def from_color(cls, name, color, fade=None, space='hsl', **kwargs):
1568
1579
side of the colormap (default is ``100``), and the saturation
1569
1580
channel is held constant throughout the colormap.
1570
1581
1571
- If RGB tuple, hex string, or named color string, the luminance and
1572
- saturation (but *not* the hue) from this color are used for the
1573
- left-hand side of the colormap.
1582
+ If RGB[A] tuple, hex string, or named color string, the luminance,
1583
+ saturation, and opacity (but *not* the hue) from this color are used
1584
+ for the left-hand side of the colormap.
1574
1585
space : {'hsl', 'hpl', 'hcl'}, optional
1575
1586
The colorspace in which the luminance is varied.
1576
1587
@@ -1584,15 +1595,16 @@ def from_color(cls, name, color, fade=None, space='hsl', **kwargs):
1584
1595
`PerceptuallyUniformColormap`
1585
1596
The colormap.
1586
1597
"""
1587
- hue , saturation , luminance , alpha = to_xyz (color , space , alpha = True )
1598
+ hue , saturation , luminance , alpha = to_xyza (color , space )
1588
1599
if fade is None :
1589
1600
fade = 100
1590
1601
if isinstance (fade , Number ):
1591
- saturation_fade , luminance_fade = saturation , fade
1602
+ alpha_fade , saturation_fade , luminance_fade = alpha , saturation , fade
1592
1603
else :
1593
- _ , saturation_fade , luminance_fade = to_xyz (fade , space )
1604
+ _ , saturation_fade , luminance_fade , alpha_fade = to_xyza (fade , space )
1594
1605
return cls .from_hsl (
1595
- name , hue = hue , alpha = alpha , space = space ,
1606
+ name , hue = hue , space = space ,
1607
+ alpha = (alpha_fade , alpha ),
1596
1608
saturation = (saturation_fade , saturation ),
1597
1609
luminance = (luminance_fade , luminance ),
1598
1610
** kwargs
@@ -1629,7 +1641,7 @@ def from_hsl(
1629
1641
``len(colors) - 1``. Larger numbers indicate a slower
1630
1642
transition, smaller numbers indicate a faster transition.
1631
1643
1632
- For example, ``luminance=[ 100,50,0] `` with ``ratios=[2,1] ``
1644
+ For example, ``luminance=( 100, 50, 0) `` with ``ratios=(2, 1) ``
1633
1645
results in a colormap with the transition from luminance ``100``
1634
1646
to ``50`` taking *twice as long* as the transition from luminance
1635
1647
``50`` to ``0``.
@@ -1675,7 +1687,7 @@ def from_list(cls, name, colors, ratios=None, **kwargs):
1675
1687
``len(colors) - 1``. Larger numbers indicate a slower
1676
1688
transition, smaller numbers indicate a faster transition.
1677
1689
1678
- For example, ``red=[1, 0.5,0] `` with ``ratios=[2,1] ``
1690
+ For example, ``red=(1, 0.5, 0) `` with ``ratios=(2, 1) ``
1679
1691
results in a colormap with the transition from red ``1``
1680
1692
to ``0.5`` taking *twice as long* as the transition from red
1681
1693
``0.5`` to ``0``.
@@ -1695,10 +1707,12 @@ def from_list(cls, name, colors, ratios=None, **kwargs):
1695
1707
space = kwargs .get ('space' , 'hsl' ) # use the builtin default
1696
1708
if not np .iterable (colors ):
1697
1709
raise ValueError (f'Colors must be iterable, got colors={ colors !r} ' )
1698
- if (np .iterable (colors [0 ]) and len (colors [0 ]) == 2
1699
- and not isinstance (colors [0 ], str )):
1710
+ if (
1711
+ np .iterable (colors [0 ]) and len (colors [0 ]) == 2
1712
+ and not isinstance (colors [0 ], str )
1713
+ ):
1700
1714
coords , colors = zip (* colors )
1701
- colors = [to_xyz (color , space , alpha = True ) for color in colors ]
1715
+ colors = [to_xyza (color , space ) for color in colors ]
1702
1716
1703
1717
# Build segmentdata
1704
1718
keys = ('hue' , 'saturation' , 'luminance' , 'alpha' )
0 commit comments