Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update for checking whether colors have an alpha channel #27327

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 6, 2025
Merged

Update for checking whether colors have an alpha channel #27327

merged 16 commits into from
Jan 6, 2025

Conversation

landoskape
Copy link
Contributor

@landoskape landoskape commented Nov 15, 2023

PR summary

  • Update check of whether a color has an alpha channel to include hexadecimal and color/alpha tuple formats.
  • Add test of the new formats
  • Add method to check whether a sequence of colors has an alpha channel

For more explanation see the matplotlib page on specifying colors.

PR checklist

@landoskape
Copy link
Contributor Author

I'm not sure how to add the check for whether c is indicating a color in a color cycle. I think it requires checking whether the first element of the string is 'C' and if the next elements represent a digit.

Also: I'm not sure why those tests are failing, but it seems like the test server isn't connected to the internet..?

Copy link
Member

@oscargus oscargus left a comment

Choose a reason for hiding this comment

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

I think it makes sense to make this more general. Some comments to consider.

return not isinstance(c, str) and len(c) == 4
# If c is not a color, it doesn't have an alpha channel
if not is_color_like(c):
return False
Copy link
Member

Choose a reason for hiding this comment

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

One should probably check the code if this has already been called. Since this is a private method one can require that it is checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, I'm sure I know how to do that or what you mean. Would you explain a little more?

(also, thanks for the comments, as always)

@ksunden
Copy link
Member

ksunden commented Nov 21, 2023

Looks like the CI failures were largely due to network problems on GHA/Azure so cycling to rerun

@ksunden ksunden closed this Nov 21, 2023
@ksunden ksunden reopened this Nov 21, 2023
@landoskape
Copy link
Contributor Author

Background

I opened this PR because of my work on PR #27304, which I though would benefit from a check on whether the user provided a color with an alpha value to determine how the violinplot function handles alpha values. However, after @story645 explained, the point of allowing alpha to be included in a color was to remove unnecessary arguments, which I agree with, so that sort of makes my work here less relevant (except for the fact that the previous _has_alpha_channel() was out of date).

Current use of _has_alpha_channel()

The _has_alpha_channel() method is only used in one place throughout the entire library: lib/matplotlib/axis.Tick. In that location, it just checks whether the provided grid color has an alpha channel specified, and if not, uses mpl.rcParams["grid.alpha"] instead.

Tick() is the parent of XTick() and YTick(), which are the parents of SkewXTick(). However, nowhere in the entire library do those methods set the kwargs grid_color or grid_alpha.

Proposal

It seems like _has_alpha_channel() is obsolete, and should be removed from the library. I propose:

  1. Removing it (and it's tests in test_colors.py).
  2. In Tick(), removing grid_alpha as an argument and letting grid_color specify the alpha setting.

@timhoffm
Copy link
Member

timhoffm commented Dec 4, 2023

I'm +0.5 on removing grid_alpha. There's value in reducing the number of parameters an dependency between them. While we should not remove primary alpha parameters from functions because they are used too widely, removing *_alpha parameters for secondary color attributes (typically pass through) seems viable.

Before we can remove grid_alpha, it has to go through our deprecation cycle.

It should also be ensured, that grid_color=(None, 0.5) works and only modifies the alpha. Otherwise, only modifying the alpha becomes cumbersome.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

I suggest to take the fixed _has_alpha_channel() as minimal bugfix and defer the discussion whether grid.alpha (the only use of _has_alpha_channel()) should be deprecated. Also defer the decision on whether we need _has_alpha_channel_array().

"""Return whether *c* is a color with an alpha channel."""
# 4-element sequences are interpreted as r, g, b, a
return not isinstance(c, str) and len(c) == 4
"""Return whether *c* is a color with an alpha channel"""
Copy link
Member

Choose a reason for hiding this comment

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

We should mention that the result is undefined if c is not a valid color specifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is now mentioned in the docstring and the comment for the final return line.

return False


def _has_alpha_channel_array(cseq):
Copy link
Member

Choose a reason for hiding this comment

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

This is not used anywhere right now. Therefore, we don't need it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is now deprecated (I added the standard _api.deprecated decorator). In addition I added a deprecation file in the doc/ folder.

Copy link
Member

Choose a reason for hiding this comment

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

Um, you've just introduced this function and immediately deprecated it?!? You should simply not add it to the PR.

@rcomer
Copy link
Member

rcomer commented Dec 28, 2024

@landoskape are you still interested in working on this?

@landoskape
Copy link
Contributor Author

Ah, yes! Lost track of it but I can continue in early January. Thanks for the reminder.

@landoskape
Copy link
Contributor Author

See my responses to @timhoffm. I believe this is all that is needed now since you suggested deferring gridalpha discussion to another time?

return False


def _has_alpha_channel_array(cseq):
Copy link
Member

Choose a reason for hiding this comment

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

Um, you've just introduced this function and immediately deprecated it?!? You should simply not add it to the PR.

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

I suggest starting by checking whether we have a valid color with is_color_like and if not return False. Then the remaining logic can be confident about how c is structured.

@landoskape
Copy link
Contributor Author

Ah - @timhoffm good point! The function was introduced because it would have helped the violinplot update that is in PR #27304. It's now removed entirely.

@landoskape
Copy link
Contributor Author

I suggest starting by checking whether we have a valid color with is_color_like and if not return False. Then the remaining logic can be confident about how c is structured.

Smart. I incorporated this.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Sorry, after going through the logic carefully, I've some additional remarks. (Could/should have been part of the previous review).

@landoskape
Copy link
Contributor Author

Of course, no worries. In agreement with all and suggestions all accepted.

@rcomer rcomer added this to the v3.11.0 milestone Jan 6, 2025
@rcomer
Copy link
Member

rcomer commented Jan 6, 2025

I do not know what the doc failure is about, but it cannot possibly be caused by this change since we are only modifying a private function.

@rcomer rcomer merged commit 9bb047d into matplotlib:main Jan 6, 2025
35 of 36 checks passed
@rcomer
Copy link
Member

rcomer commented Jan 6, 2025

Thanks @landoskape and congratulations on your first PR merged into Matplotlib! We hope to hear from you again.

@landoskape
Copy link
Contributor Author

Woohoo! Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for author
Development

Successfully merging this pull request may close these issues.

[Bug]: The method for checking whether a color has an alpha value is outdated
5 participants