Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
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
Uh oh!
There was an error while loading.Please reload this page.
Changes from4 commits
05863839719b6077e759f5d7edcae7220c80cf3612e2099e65ac2c0f212363e73127ebc3cf8d4b81786c80d422dbbe6fa2de5e42d0813e13File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -232,9 +232,34 @@ def is_color_like(c): | ||
| def _has_alpha_channel(c): | ||
| """Return whether *c* is a color with an alpha channel""" | ||
| ||
| # 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): | ||
landoskape marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| # 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): | ||
| ||
| """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): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
landoskape marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| def test_cn(): | ||