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

Skip to content

Commit 364277b

Browse files
committed
Tweak John's change to handling of rgba arrays
svn path=/trunk/matplotlib/; revision=7189
1 parent bbbb267 commit 364277b

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,27 +361,35 @@ def to_rgba_array(self, c, alpha=None):
361361
then an empty array will be returned. Same for an empty list.
362362
"""
363363
try:
364-
if c.lower() == 'none':
365-
return np.zeros((0,4), dtype=np.float_)
364+
nc = len(c)
365+
except TypeError:
366+
raise ValueError(
367+
"Cannot convert argument type %s to rgba array" % type(c))
368+
try:
369+
if nc == 0 or c.lower() == 'none':
370+
return np.zeros((0,4), dtype=np.float)
366371
except AttributeError:
367372
pass
368-
if len(c) == 0:
369-
return np.zeros((0,4), dtype=np.float_)
370373
try:
371-
result = np.array([self.to_rgba(c, alpha)], dtype=np.float_)
374+
# Single value? Put it in an array with a single row.
375+
return np.array([self.to_rgba(c, alpha)], dtype=np.float)
372376
except ValueError:
373377
if isinstance(c, np.ndarray):
374378
if c.ndim != 2 and c.dtype.kind not in 'SU':
375379
raise ValueError("Color array must be two-dimensional")
376-
if len(c.shape)==2 and c.shape[-1]==4:
380+
if (c.ndim == 2 and c.shape[1] == 4 and c.dtype.kind == 'f'):
381+
if (c.ravel() > 1).any() or (c.ravel() < 0).any():
382+
raise ValueError(
383+
"number in rgba sequence is outside 0-1 range")
377384
# looks like rgba already, nothing to be done; do
378385
# we want to apply alpha here if
379386
# (c[:,3]==1).all() ?
380-
return c
381-
result = np.zeros((len(c), 4))
387+
return np.asarray(c, np.float)
388+
# It must be some other sequence of color specs.
389+
result = np.zeros((nc, 4), dtype=np.float)
382390
for i, cc in enumerate(c):
383-
result[i] = self.to_rgba(cc, alpha) # change in place
384-
return np.asarray(result, np.float_)
391+
result[i] = self.to_rgba(cc, alpha)
392+
return result
385393

386394
colorConverter = ColorConverter()
387395

0 commit comments

Comments
 (0)