10BC0 Micro-optimize _to_rgba_no_colorcycle. by anntzer · Pull Request #30020 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all 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
10BC0
Diff view
Diff view
49 changes: 20 additions & 29 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,40 +372,31 @@
# This may turn c into a non-string, so we check again below.
c = _colors_full_map[c]
except KeyError:
if len(orig_c) != 1:
if len(c) != 1:
try:
c = _colors_full_map[c.lower()]
except KeyError:
pass
if isinstance(c, str):
# hex color in #rrggbb format.
match = re.match(r"\A#[a-fA-F0-9]{6}\Z", c)
if match:
return (tuple(int(n, 16) / 255
for n in [c[1:3], c[3:5], c[5:7]])
+ (alpha if alpha is not None else 1.,))
# hex color in #rgb format, shorthand for #rrggbb.
match = re.match(r"\A#[a-fA-F0-9]{3}\Z", c)
if match:
return (tuple(int(n, 16) / 255
for n in [c[1]*2, c[2]*2, c[3]*2])
+ (alpha if alpha is not None else 1.,))
# hex color with alpha in #rrggbbaa format.
match = re.match(r"\A#[a-fA-F0-9]{8}\Z", c)
if match:
color = [int(n, 16) / 255
for n in [c[1:3], c[3:5], c[5:7], c[7:9]]]
if alpha is not None:
color[-1] = alpha
return tuple(color)
# hex color with alpha in #rgba format, shorthand for #rrggbbaa.
match = re.match(r"\A#[a-fA-F0-9]{4}\Z", c)
if match:
color = [int(n, 16) / 255
for n in [c[1]*2, c[2]*2, c[3]*2, c[4]*2]]
if alpha is not None:
color[-1] = alpha
return tuple(color)
if re.fullmatch("#[a-fA-F0-9]+", c):
if len(c) == 7: # #rrggbb hex format.
return (*[n / 0xff for n in bytes.fromhex(c[1:])],
alpha if alpha is not None else 1.)
elif len(c) == 4: # #rgb hex format, shorthand for #rrggbb.
return (*[int(n, 16) / 0xf for n in c[1:]],
alpha if alpha is not None else 1.)
elif len(c) == 9: # #rrggbbaa hex format.
color = [n / 0xff for n in bytes.fromhex(c[1:])]
if alpha is not None:
color[-1] = alpha
return tuple(color)
elif len(c) == 5: # #rgba hex format, shorthand for #rrggbbaa.
color = [int(n, 16) / 0xf for n in c[1:]]
if alpha is not None:
color[-1] = alpha

Check warning on line 396 in lib/matplotlib/colors.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/colors.py#L396

Added line #L396 was not covered by tests
return tuple(color)
else:
raise ValueError(f"Invalid hex color specifier: {orig_c!r}")

Check warning on line 399 in lib/matplotlib/colors.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/colors.py#L399

Added line #L399 was not covered by tests
# string gray.
try:
c = float(c)
Expand Down
Loading
0