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

Fixed _has_alpha_channel function to work with hex and tuple colors#27463

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

Closed
Changes fromall 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
58 changes: 56 additions & 2 deletionslib/matplotlib/colors.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -231,10 +231,64 @@ def is_color_like(c):
return True


def is_hex_color(c):
"""
(Helper function) Determines if the input string is a
hexadecimal number in color format. String must start
with # or 0x and have 8 bytes following, 2 for each color
and alpha channel.
"""
if not isinstance(c, str):
return False
if c.startswith('#'):
if len(c) != 9:
return False
c = c[1:]
elif c.startswith('0x'):
if len(c) != 10:
return False
c = c[2:]
else:
return False

for x in c:
if x not in '0123456789abcdefABCDEF':
return False

return True


def is_tuple_color(c):
"""
(Helper function) Determines if the input string is a
tuple in color format. Tuple must have two values: the
first representing the color and the second representing
the alpha value.
"""
if not isinstance(c, tuple):
return False
if len(c) != 2:
return False

color = c[0]
if not isinstance(color, str):
return False
if c != 'r' and c != 'g' and c != 'b':
return False

alpha = c[1]
if not isinstance(alpha, float):
return False
if alpha < 0 or alpha > 1:
return False

return True


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
returnnot isinstance(c, str) and len(c) == 4
is_color = not isinstance(c, str) and len(c) == 4
returnis_color or is_hex_color(c) or is_tuple_color(c)


def _check_color_like(**kwargs):
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp