Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
rcomer merged 16 commits intomatplotlib:mainfromlandoskape:alpha-channel-update
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from4 commits
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
0586383
Update _has_alpha_channel
landoskapeNov 15, 2023
9719b60
Update lib/matplotlib/colors.py
landoskapeNov 15, 2023
77e759f
Clarify comment explanation
landoskapeNov 15, 2023
5d7edca
Remove unnecessary check about is_color_like
landoskapeNov 21, 2023
e7220c8
update docstring for undefined case
landoskapeJan 3, 2025
0cf3612
Deprecate unused method
landoskapeJan 3, 2025
e2099e6
Remove unnecessary method
landoskapeJan 4, 2025
5ac2c0f
Flake formatting update
landoskapeJan 4, 2025
212363e
Don't specify what an undefined result does
landoskapeJan 4, 2025
73127eb
Recursive check which handles hex colors too
landoskapeJan 4, 2025
c3cf8d4
Whitespace
landoskapeJan 4, 2025
b81786c
Remove is_color_like check and explain
landoskapeJan 5, 2025
80d422d
Don't discuss undefined results
landoskapeJan 5, 2025
bbe6fa2
Remove test of undefined result
landoskapeJan 5, 2025
de5e42d
Spelling error
landoskapeJan 6, 2025
0813e13
improved grammar in comment
landoskapeJan 6, 2025
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
31 changes: 28 additions & 3 deletionslib/matplotlib/colors.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -232,9 +232,34 @@ 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"""
Copy link
Member

Choose a reason for hiding this comment

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

We should mention that the result is undefined ifc is not a valid color specifier.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This is now mentioned in the docstring and the comment for the final return line.

# 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 len(c[0]) == 4):
# 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
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
ContributorAuthor

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
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.

landoskape reacted with eyes emoji
"""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 deletionslib/matplotlib/tests/test_colors.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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


def test_cn():
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp