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

Skip to content

Update for checking whether colors have an alpha channel #27327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,35 @@ def is_color_like(c):


def _has_alpha_channel(c):
"""Return whether *c* is a color with an alpha channel."""
# 4-element sequences are interpreted as r, g, b, a
return not isinstance(c, str) and len(c) == 4
"""
Return whether *c* is a color with an alpha channel.

If *c* is not a valid color specifier, then the result is undefined.
"""
# The following logic uses the assumption that c is a valid color spec.
# For speed and simplicity, we intentionally don't care about other inputs.
# Anything can happen with them.

# if c is a hex, it has an alpha channel when it has 4 (or 8) digits after '#'
if isinstance(c, str):
if c[0] == '#' and (len(c) == 5 or len(c) == 9):
# example: '#fff8' or '#0f0f0f80'
return True
else:
# if c isn't a string, it can be an RGB(A) or a color-alpha tuple
# if it has length 4, it has an alpha channel
if len(c) == 4:
# example: [0.5, 0.5, 0.5, 0.5]
return True

# if it has length 2, it's a color/alpha tuple
# if the second element isn't None or the first element has length = 4
if len(c) == 2 and (c[1] is not None or _has_alpha_channel(c[0])):
# example: ([0.5, 0.5, 0.5, 0.5], None) or ('r', 0.5)
return True

# otherwise it doesn't have an alpha channel
return False


def _check_color_like(**kwargs):
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,10 +1231,18 @@ def test_colormap_reversing(name):
def test_has_alpha_channel():
assert mcolors._has_alpha_channel((0, 0, 0, 0))
assert mcolors._has_alpha_channel([1, 1, 1, 1])
assert mcolors._has_alpha_channel('#fff8')
assert mcolors._has_alpha_channel('#0f0f0f80')
assert mcolors._has_alpha_channel(('r', 0.5))
assert mcolors._has_alpha_channel(([1, 1, 1, 1], None))
assert not mcolors._has_alpha_channel('blue') # 4-char string!
assert not mcolors._has_alpha_channel('0.25')
assert not mcolors._has_alpha_channel('r')
assert not mcolors._has_alpha_channel((1, 0, 0))
assert not mcolors._has_alpha_channel('#fff')
assert not mcolors._has_alpha_channel('#0f0f0f')
assert not mcolors._has_alpha_channel(('r', None))
assert not mcolors._has_alpha_channel(([1, 1, 1], None))


def test_cn():
Expand Down
Loading