Repository: matplotlib/matplotlib
File: lib/matplotlib/colors.py (Python)

Code before:
    to *alpha*. Otherwise, the alpha value from *c* is used, if it has alpha
    information, or defaults to 1.

    *alpha* is ignored for the color value ``"none"`` (case-insensitive),
    which always maps to ``(0, 0, 0, 0)``.
    """
    if alpha is not None and not 0 <= alpha <= 1:
        raise ValueError("'alpha' must be between 0 and 1, inclusive")
    orig_c = c
    if c is np.ma.masked:
        return (0., 0., 0., 0.)
    if isinstance(c, str):
        if c.lower() == "none":
            return (0., 0., 0., 0.)
        # Named color.
        try:
            # This may turn c into a non-string, so we check again below.
            c = _colors_full_map[c]
        except KeyError:
            if len(c) != 1:
                try:
                    c = _colors_full_map[c.lower()]
                except KeyError:
                    pass
    if isinstance(c, str):
        if re.match(r"\A#[a-fA-F0-9]+\Z", 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) * 0x11 / 0xff 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) * 0x11 / 0xff for n in c[1:]]
                if alpha is not None:
                    color[-1] = alpha
                return tuple(color)
        # string gray.
        try:
            c = float(c)
        except ValueError:
            pass
        else:
            if not (0 <= c <= 1):
                raise ValueError(
                    f"Invalid string grayscale value {orig_c!r}. "

Review comment (performance):
```suggestion
        if re.fullmatch("#[a-fA-F0-9]+", c):
```
Not sure if it has a performance impact, ut it's easier to read 😄.

Code after:
    to *alpha*. Otherwise, the alpha value from *c* is used, if it has alpha
    information, or defaults to 1.

    *alpha* is ignored for the color value ``"none"`` (case-insensitive),
    which always maps to ``(0, 0, 0, 0)``.
    """
    if alpha is not None and not 0 <= alpha <= 1:
        raise ValueError("'alpha' must be between 0 and 1, inclusive")
    orig_c = c
    if c is np.ma.masked:
        return (0., 0., 0., 0.)
    if isinstance(c, str):
        if c.lower() == "none":
            return (0., 0., 0., 0.)
        # Named color.
        try:
            # This may turn c into a non-string, so we check again below.
            c = _colors_full_map[c]
        except KeyError:
            if len(c) != 1:
                try:
                    c = _colors_full_map[c.lower()]
                except KeyError:
                    pass
    if isinstance(c, str):
        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)
        except ValueError:
            pass
        else:
            if not (0 <= c <= 1):