Skip to content

Make sure custom alpha param does not change 'none' colors in a list of colors #27845

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 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
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
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ def to_rgba_array(c, alpha=None):

if alpha is not None:
rgba[:, 3] = alpha
if isinstance(c, Sequence):
# ensure that an explicit alpha does not overwrite full transparency
# for "none"
none_mask = [cbook._str_equal(cc, "none") for cc in c]
rgba[:, 3][none_mask] = 0
return rgba


Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import matplotlib.scale as mscale
from matplotlib.rcsetup import cycler
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from matplotlib.colors import to_rgba_array


@pytest.mark.parametrize('N, result', [
Expand Down Expand Up @@ -1660,3 +1661,13 @@ def test_set_cmap_mismatched_name():
cmap_returned = plt.get_cmap("wrong-cmap")
assert cmap_returned == cmap
assert cmap_returned.name == "wrong-cmap"


def test_to_rgba_array_none_color_with_alpha_param():
# effective alpha for color "none" must always be 0 to achieve a vanishing color
# even explicit alpha must be ignored
c = ["blue", "none"]
alpha = [1, 1]
assert_array_equal(
to_rgba_array(c, alpha), [[0., 0., 1., 1.], [0., 0., 0., 0.]]
)