Skip to content

Fix is_color_like for 2-tuple of strings and fix to_rgba for (nth_color, alpha) - #28436

Merged
story645 merged 1 commit into
matplotlib:mainfrom
rcomer:color-tuple
Jun 23, 2024
Merged

story645 merged 1 commit into
matplotlib:mainfrom
rcomer:color-tuple

Conversation

@rcomer

@rcomer rcomer commented Jun 22, 2024 •

Copy link
Copy Markdown
Member

PR summary

Currently if we do

is_color_like(('red', 'blue'))

we get the unhelpful error shown in #28434. I couldn't find any existing tests for is_color_like so added a few. In the process of adding tests, found that

to_rgba(('C3', 0.5))

is also broken (see #28436 (comment)), so fixed that too.

Closes #28434, since this fixes the reported bug. There was a suggestion there about supporting color sequences in plot but it seems like that would be difficult to do. If we do want to discuss it further it might make sense to open a separate feature request issue.

PR checklist

@rcomer rcomer added the PR: bugfix Pull requests that fix identified bugs label Jun 22, 2024
@rcomer rcomer added this to the v3.9.1 milestone Jun 22, 2024

@timhoffm timhoffm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally add more color variants to the is_color_like test.

@rcomer

rcomer commented Jun 22, 2024

Copy link
Copy Markdown
Member Author

Optionally add more color variants to the is_color_like test.

But if I do that I might find more bugs! 😆

This should work I think?

In [6]: to_rgba(('C3', 0.5))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 to_rgba(('C3', 0.5))

File ~/git_stuff/matplotlib/lib/matplotlib/colors.py:309, in to_rgba(c, alpha)
    307     rgba = None
    308 if rgba is None:  # Suppress exception chaining of cache lookup failure.
--> 309     rgba = _to_rgba_no_colorcycle(c, alpha)
    310     try:
    311         _colors_full_map.cache[c, alpha] = rgba

File ~/git_stuff/matplotlib/lib/matplotlib/colors.py:391, in _to_rgba_no_colorcycle(c, alpha)
    387             raise ValueError(
    388                 f"Invalid string grayscale value {orig_c!r}. "
    389                 f"Value must be within 0-1 range")
    390         return c, c, c, alpha if alpha is not None else 1.
--> 391     raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
    392 # turn 2-D array into 1-D array
    393 if isinstance(c, np.ndarray):

ValueError: Invalid RGBA argument: 'C3'

and fix to_rgba for (nth_color, alpha)
@rcomer rcomer changed the title Fix is_color_like for 2-tuple of strings Fix is_color_like for 2-tuple of strings and fix to_rgba for (nth_color, alpha) Jun 22, 2024
@rcomer
rcomer requested a review from timhoffm June 22, 2024 09:55
@story645
story645 merged commit 81756c3 into matplotlib:main Jun 23, 2024
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jun 23, 2024
…ngs and fix `to_rgba` for `(nth_color, alpha)`
@rcomer
rcomer deleted the color-tuple branch June 23, 2024 09:43
greglucas pushed a commit that referenced this pull request Jun 24, 2024
…x `to_rgba` for `(nth_color, alpha)` (#28438)

Co-authored-by: hannah <story645@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: bugfix Pull requests that fix identified bugs topic: color/color & colormaps

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Setting exactly 2 colors with tuple in plot method gives confusing error

3 participants