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

Skip to content

Commit 13b8718

Browse files
committed
Fix is_color_like for 2-tuple of strings
and fix to_rgba for (nth_color, alpha)
1 parent 53431a4 commit 13b8718

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/matplotlib/colors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def is_color_like(c):
225225
return True
226226
try:
227227
to_rgba(c)
228-
except ValueError:
228+
except (TypeError, ValueError):
229229
return False
230230
else:
231231
return True
@@ -296,6 +296,11 @@ def to_rgba(c, alpha=None):
296296
Tuple of floats ``(r, g, b, a)``, where each channel (red, green, blue,
297297
alpha) can assume values between 0 and 1.
298298
"""
299+
if isinstance(c, tuple) and len(c) == 2:
300+
if alpha is None:
301+
c, alpha = c
302+
else:
303+
c = c[0]
299304
# Special-case nth color syntax because it should not be cached.
300305
if _is_nth_color(c):
301306
prop_cycler = mpl.rcParams['axes.prop_cycle']
@@ -325,11 +330,6 @@ def _to_rgba_no_colorcycle(c, alpha=None):
325330
*alpha* is ignored for the color value ``"none"`` (case-insensitive),
326331
which always maps to ``(0, 0, 0, 0)``.
327332
"""
328-
if isinstance(c, tuple) and len(c) == 2:
329-
if alpha is None:
330-
c, alpha = c
331-
else:
332-
c = c[0]
333333
if alpha is not None and not 0 <= alpha <= 1:
334334
raise ValueError("'alpha' must be between 0 and 1, inclusive")
335335
orig_c = c

lib/matplotlib/tests/test_colors.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import matplotlib.scale as mscale
2020
from matplotlib.rcsetup import cycler
2121
from matplotlib.testing.decorators import image_comparison, check_figures_equal
22-
from matplotlib.colors import to_rgba_array
22+
from matplotlib.colors import is_color_like, to_rgba_array
2323

2424

2525
@pytest.mark.parametrize('N, result', [
@@ -1702,3 +1702,16 @@ def test_to_rgba_array_none_color_with_alpha_param():
17021702
assert_array_equal(
17031703
to_rgba_array(c, alpha), [[0., 0., 1., 1.], [0., 0., 0., 0.]]
17041704
)
1705+
1706+
1707+
@pytest.mark.parametrize('input, expected',
1708+
[('red', True),
1709+
(('red', 0.5), True),
1710+
(('red', 2), False),
1711+
(['red', 0.5], False),
1712+
(('red', 'blue'), False),
1713+
(['red', 'blue'], False),
1714+
('C3', True),
1715+
(('C3', 0.5), True)])
1716+
def test_is_color_like(input, expected):
1717+
assert is_color_like(input) is expected

0 commit comments

Comments
 (0)