Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 30 additions & 2 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,36 @@ 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
# If c is not a color, it doesn't have an alpha channel
if not is_color_like(c):
return False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One should probably check the code if this has already been called. Since this is a private method one can require that it is checked.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'm sure I know how to do that or what you mean. Would you explain a little more?

(also, thanks for the comments, as always)


# if c is a hex, it has an alpha channel when it has 4 (or 8) digits after '#'
if isinstance(c, str) and c[0] == '#' and (len(c) == 5 or len(c) == 9):
# example: '#fff8' or '#0f0f0f80'
return True

# if c isn't a string, it can be an RGB(A) or a color-alpha tuple
if not isinstance(c, str):
Comment thread
landoskape marked this conversation as resolved.
Resolved
# 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, the 2nd element isn't None, and the 1st element has len=3
Comment thread
landoskape marked this conversation as resolved.
Resolved
if len(c) == 2 and (c[1] is not None or len(c[0]) == 4):
Comment thread
landoskape marked this conversation as resolved.
Resolved
# 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 _has_alpha_channel_array(cseq):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used anywhere right now. Therefore, we don't need it here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now deprecated (I added the standard _api.deprecated decorator). In addition I added a deprecation file in the doc/ folder.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, you've just introduced this function and immediately deprecated it?!? You should simply not add it to the PR.

"""Return whether each element in *cseq* is a color with an alpha channel"""
if is_color_like(cseq):
cseq = [cseq] # force it to be a sequence
return [_has_alpha_channel(c) for c in cseq]


def _check_color_like(**kwargs):
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,10 +1231,19 @@ 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))
assert not mcolors._has_alpha_channel(1) # non-colors don't have alpha channels
Comment thread
landoskape marked this conversation as resolved.
Resolved


def test_cn():
Expand Down