Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ff6673

Browse files
committed
Return builtin floats, not numpy floats.
1 parent 74fc270 commit 8ff6673

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

lib/matplotlib/colors.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def is_color_like(c):
126126
def to_rgba(c, alpha=None):
127127
"""Convert `c` to an RGBA color.
128128
129-
`alpha` provides a default alpha value.
129+
If `alpha` is not `None`, it forces the alpha value.
130130
"""
131131
# Special-case nth color syntax because it should not be cached.
132132
if _is_nth_color(c):
@@ -152,7 +152,7 @@ def to_rgba(c, alpha=None):
152152
def _to_rgba_no_colorcycle(c, alpha=None):
153153
"""Convert `c` to an RGBA color, with no support for color-cycle syntax.
154154
155-
`alpha` provides a default alpha value.
155+
If `alpha` is not `None`, it forces the alpha value.
156156
"""
157157
orig_c = c
158158
if isinstance(c, six.string_types) and c.lower() == "none":
@@ -187,28 +187,27 @@ def _to_rgba_no_colorcycle(c, alpha=None):
187187
pass
188188
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
189189
# tuple color.
190+
# Python 2.7 / numpy 1.6 apparently require this to return builtin floats,
191+
# not numpy floats.
190192
try:
191-
c = np.array(c, float)
193+
c = tuple(map(float, c))
192194
except TypeError:
193195
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
194-
if c.ndim != 1:
195-
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
196-
if len(c) == 3:
197-
c = np.append(c, alpha if alpha is not None else 1.)
198-
if len(c) == 4:
199-
if np.any((c < 0) | (c > 1)):
200-
raise ValueError("RGBA values should be within 0-1 range")
201-
if alpha is not None:
202-
c[-1] = alpha
203-
return tuple(c)
204-
else:
196+
if len(c) not in [3, 4]:
205197
raise ValueError("RGBA sequence should have length 3 or 4")
198+
if len(c) == 3 and alpha is None:
199+
alpha = 1
200+
if alpha is not None:
201+
c = c[:3] + (alpha,)
202+
if any(elem < 0 or elem > 1 for elem in c):
203+
raise ValueError("RGBA values should be within 0-1 range")
204+
return c
206205

207206

208207
def to_rgba_array(c, alpha=None):
209208
"""Convert `c` to a (n, 4) array of RGBA colors.
210209
211-
`alpha` provides a default alpha value. If `c` is "none"
210+
If `alpha` is not `None`, it forces the alpha value. If `c` is "none"
212211
(case-insensitive) or an empty list, an empty array is returned.
213212
"""
214213
# Single value?
@@ -243,7 +242,7 @@ def to_rgba_array(c, alpha=None):
243242
def to_hex(c, alpha=None):
244243
"""Convert `c` to a hex color.
245244
246-
`alpha` provides a default alpha value.
245+
If `alpha` is not `None`, it forces the alpha value.
247246
"""
248247
return "#" + "".join(format(int(np.round(val * 255)), "02x")
249248
for val in to_rgba(c, alpha=alpha))

0 commit comments

Comments
 (0)