4040"""
4141
4242import base64
43- from collections .abc import Sized , Sequence , Mapping
43+ from collections .abc import Sequence , Mapping
4444import functools
4545import importlib
4646import inspect
@@ -1180,7 +1180,8 @@ def from_list(name, colors, N=256, gamma=1.0, *, bad=None, under=None, over=None
11801180 range :math:`[0, 1]`; i.e. 0 maps to ``colors[0]`` and 1 maps to
11811181 ``colors[-1]``.
11821182 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.
11841185 N : int
11851186 The number of RGB quantization levels.
11861187 gamma : float
@@ -1195,14 +1196,26 @@ def from_list(name, colors, N=256, gamma=1.0, *, bad=None, under=None, over=None
11951196 if not np .iterable (colors ):
11961197 raise ValueError ('colors must be iterable' )
11971198
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
12031203 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
12041218
1205- r , g , b , a = to_rgba_array (colors ).T
12061219 cdict = {
12071220 "red" : np .column_stack ([vals , r , r ]),
12081221 "green" : np .column_stack ([vals , g , g ]),
0 commit comments