|
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
|
@@ -1091,22 +1091,35 @@ def from_list(name, colors, N=256, gamma=1.0):
|
1091 | 1091 | range :math:`[0, 1]`; i.e. 0 maps to ``colors[0]`` and 1 maps to
|
1092 | 1092 | ``colors[-1]``.
|
1093 | 1093 | If (value, color) pairs are given, the mapping is from *value*
|
1094 |
| - to *color*. This can be used to divide the range unevenly. |
| 1094 | + to *color*. This can be used to divide the range unevenly. The |
| 1095 | + values must increase monotonically from 0 to 1. |
1095 | 1096 | N : int
|
1096 | 1097 | The number of RGB quantization levels.
|
1097 | 1098 | gamma : float
|
1098 | 1099 | """
|
1099 | 1100 | if not np.iterable(colors):
|
1100 | 1101 | raise ValueError('colors must be iterable')
|
1101 | 1102 |
|
1102 |
| - if (isinstance(colors[0], Sized) and len(colors[0]) == 2 |
1103 |
| - and not isinstance(colors[0], str)): |
1104 |
| - # List of value, color pairs |
1105 |
| - vals, colors = zip(*colors) |
1106 |
| - else: |
| 1103 | + try: |
| 1104 | + # Assume the passed colors are a list of colors |
| 1105 | + # and not a (value, color) tuple. |
| 1106 | + r, g, b, a = to_rgba_array(colors).T |
1107 | 1107 | vals = np.linspace(0, 1, len(colors))
|
| 1108 | + except Exception as e: |
| 1109 | + # Assume the passed values are a list of |
| 1110 | + # (value, color) tuples. |
| 1111 | + try: |
| 1112 | + _vals, _colors = itertools.zip_longest(*colors) |
| 1113 | + except Exception as e2: |
| 1114 | + raise e2 from e |
| 1115 | + vals = np.asarray(_vals) |
| 1116 | + if np.min(vals) < 0 or np.max(vals) > 1 or np.any(np.diff(vals) <= 0): |
| 1117 | + raise ValueError( |
| 1118 | + "the values passed in the (value, color) pairs " |
| 1119 | + "must increase monotonically from 0 to 1." |
| 1120 | + ) |
| 1121 | + r, g, b, a = to_rgba_array(_colors).T |
1108 | 1122 |
|
1109 |
| - r, g, b, a = to_rgba_array(colors).T |
1110 | 1123 | cdict = {
|
1111 | 1124 | "red": np.column_stack([vals, r, r]),
|
1112 | 1125 | "green": np.column_stack([vals, g, g]),
|
|
0 commit comments