Skip to content

Commit 9bb047d

Browse files
landoskapeoscargustimhoffmrcomer
authored
Update for checking whether colors have an alpha channel (matplotlib#27327)
* Update _has_alpha_channel * Update lib/matplotlib/colors.py Co-authored-by: Oscar Gustafsson <[email protected]> * Clarify comment explanation * Remove unnecessary check about is_color_like * update docstring for undefined case * Deprecate unused method * Remove unnecessary method * Flake formatting update Co-authored-by: Tim Hoffmann <[email protected]> * Don't specify what an undefined result does Co-authored-by: Tim Hoffmann <[email protected]> * Recursive check which handles hex colors too Co-authored-by: Ruth Comer <[email protected]> * Whitespace * Remove is_color_like check and explain Co-authored-by: Tim Hoffmann <[email protected]> * Don't discuss undefined results Co-authored-by: Tim Hoffmann <[email protected]> * Remove test of undefined result Co-authored-by: Tim Hoffmann <[email protected]> * Spelling error Co-authored-by: Tim Hoffmann <[email protected]> * improved grammar in comment --------- Co-authored-by: Oscar Gustafsson <[email protected]> Co-authored-by: Tim Hoffmann <[email protected]> Co-authored-by: Ruth Comer <[email protected]>
1 parent cb2d928 commit 9bb047d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

lib/matplotlib/colors.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,35 @@ def is_color_like(c):
234234

235235

236236
def _has_alpha_channel(c):
237-
"""Return whether *c* is a color with an alpha channel."""
238-
# 4-element sequences are interpreted as r, g, b, a
239-
return not isinstance(c, str) and len(c) == 4
237+
"""
238+
Return whether *c* is a color with an alpha channel.
239+
240+
If *c* is not a valid color specifier, then the result is undefined.
241+
"""
242+
# The following logic uses the assumption that c is a valid color spec.
243+
# For speed and simplicity, we intentionally don't care about other inputs.
244+
# Anything can happen with them.
245+
246+
# if c is a hex, it has an alpha channel when it has 4 (or 8) digits after '#'
247+
if isinstance(c, str):
248+
if c[0] == '#' and (len(c) == 5 or len(c) == 9):
249+
# example: '#fff8' or '#0f0f0f80'
250+
return True
251+
else:
252+
# if c isn't a string, it can be an RGB(A) or a color-alpha tuple
253+
# if it has length 4, it has an alpha channel
254+
if len(c) == 4:
255+
# example: [0.5, 0.5, 0.5, 0.5]
256+
return True
257+
258+
# if it has length 2, it's a color/alpha tuple
259+
# if the second element isn't None or the first element has length = 4
260+
if len(c) == 2 and (c[1] is not None or _has_alpha_channel(c[0])):
261+
# example: ([0.5, 0.5, 0.5, 0.5], None) or ('r', 0.5)
262+
return True
263+
264+
# otherwise it doesn't have an alpha channel
265+
return False
240266

241267

242268
def _check_color_like(**kwargs):

lib/matplotlib/tests/test_colors.py

+8
Original file line numberDiff line numberDiff line change
@@ -1191,10 +1191,18 @@ def test_colormap_reversing(name):
11911191
def test_has_alpha_channel():
11921192
assert mcolors._has_alpha_channel((0, 0, 0, 0))
11931193
assert mcolors._has_alpha_channel([1, 1, 1, 1])
1194+
assert mcolors._has_alpha_channel('#fff8')
1195+
assert mcolors._has_alpha_channel('#0f0f0f80')
1196+
assert mcolors._has_alpha_channel(('r', 0.5))
1197+
assert mcolors._has_alpha_channel(([1, 1, 1, 1], None))
11941198
assert not mcolors._has_alpha_channel('blue') # 4-char string!
11951199
assert not mcolors._has_alpha_channel('0.25')
11961200
assert not mcolors._has_alpha_channel('r')
11971201
assert not mcolors._has_alpha_channel((1, 0, 0))
1202+
assert not mcolors._has_alpha_channel('#fff')
1203+
assert not mcolors._has_alpha_channel('#0f0f0f')
1204+
assert not mcolors._has_alpha_channel(('r', None))
1205+
assert not mcolors._has_alpha_channel(([1, 1, 1], None))
11981206

11991207

12001208
def test_cn():

0 commit comments

Comments
 (0)