From c004e49f66531b1d6674cdd3d851cb984006002c Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 13 Feb 2025 10:21:37 +0100 Subject: [PATCH] Micro-optimize _to_rgba_no_colorcycle. This patch speeds up conversions of `#rgba`-type formats by between 25% and 40% (while shortening the implementation), although real benefits should be limited because of caching in to_rgba. --- lib/matplotlib/colors.py | 49 ++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index e3c3b39e8bb2..9bd808074c1f 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -372,40 +372,31 @@ def _to_rgba_no_colorcycle(c, alpha=None): # 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 + return tuple(color) + else: + raise ValueError(f"Invalid hex color specifier: {orig_c!r}") # string gray. try: c = float(c)