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

Commit9bb047d

Browse files
landoskapeoscargustimhoffmrcomer
authored
Update for checking whether colors have an alpha channel (#27327)
* Update _has_alpha_channel* Update lib/matplotlib/colors.pyCo-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>* Clarify comment explanation* Remove unnecessary check about is_color_like* update docstring for undefined case* Deprecate unused method* Remove unnecessary method* Flake formatting updateCo-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>* Don't specify what an undefined result doesCo-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>* Recursive check which handles hex colors tooCo-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>* Whitespace* Remove is_color_like check and explainCo-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>* Don't discuss undefined resultsCo-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>* Remove test of undefined resultCo-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>* Spelling errorCo-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>* improved grammar in comment---------Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>
1 parentcb2d928 commit9bb047d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

‎lib/matplotlib/colors.py

Lines changed: 29 additions & 3 deletions
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-
returnnotisinstance(c,str)andlen(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+
ifisinstance(c,str):
248+
ifc[0]=='#'and (len(c)==5orlen(c)==9):
249+
# example: '#fff8' or '#0f0f0f80'
250+
returnTrue
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+
iflen(c)==4:
255+
# example: [0.5, 0.5, 0.5, 0.5]
256+
returnTrue
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+
iflen(c)==2and (c[1]isnotNoneor_has_alpha_channel(c[0])):
261+
# example: ([0.5, 0.5, 0.5, 0.5], None) or ('r', 0.5)
262+
returnTrue
263+
264+
# otherwise it doesn't have an alpha channel
265+
returnFalse
240266

241267

242268
def_check_color_like(**kwargs):

‎lib/matplotlib/tests/test_colors.py

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

11991207

12001208
deftest_cn():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp