40
40
"""
41
41
42
42
import base64
43
- from collections .abc import Sized , Sequence , Mapping
43
+ from collections .abc import Sequence , Mapping
44
44
import functools
45
45
import importlib
46
46
import inspect
@@ -1180,7 +1180,8 @@ def from_list(name, colors, N=256, gamma=1.0, *, bad=None, under=None, over=None
1180
1180
range :math:`[0, 1]`; i.e. 0 maps to ``colors[0]`` and 1 maps to
1181
1181
``colors[-1]``.
1182
1182
If (value, color) pairs are given, the mapping is from *value*
1183
- to *color*. This can be used to divide the range unevenly.
1183
+ to *color*. This can be used to divide the range unevenly. The
1184
+ values must increase monotonically from 0 to 1.
1184
1185
N : int
1185
1186
The number of RGB quantization levels.
1186
1187
gamma : float
@@ -1195,14 +1196,26 @@ def from_list(name, colors, N=256, gamma=1.0, *, bad=None, under=None, over=None
1195
1196
if not np .iterable (colors ):
1196
1197
raise ValueError ('colors must be iterable' )
1197
1198
1198
- if (isinstance (colors [0 ], Sized ) and len (colors [0 ]) == 2
1199
- and not isinstance (colors [0 ], str )):
1200
- # List of value, color pairs
1201
- vals , colors = zip (* colors )
1202
- else :
1199
+ try :
1200
+ # Assume the passed colors are a list of colors
1201
+ # and not a (value, color) tuple.
1202
+ r , g , b , a = to_rgba_array (colors ).T
1203
1203
vals = np .linspace (0 , 1 , len (colors ))
1204
+ except Exception as e :
1205
+ # Assume the passed values are a list of
1206
+ # (value, color) tuples.
1207
+ try :
1208
+ _vals , _colors = itertools .zip_longest (* colors )
1209
+ except Exception as e2 :
1210
+ raise e2 from e
1211
+ vals = np .asarray (_vals )
1212
+ if np .min (vals ) < 0 or np .max (vals ) > 1 or np .any (np .diff (vals ) <= 0 ):
1213
+ raise ValueError (
1214
+ "the values passed in the (value, color) pairs "
1215
+ "must increase monotonically from 0 to 1."
1216
+ )
1217
+ r , g , b , a = to_rgba_array (_colors ).T
1204
1218
1205
- r , g , b , a = to_rgba_array (colors ).T
1206
1219
cdict = {
1207
1220
"red" : np .column_stack ([vals , r , r ]),
1208
1221
"green" : np .column_stack ([vals , g , g ]),
0 commit comments