diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 575edf6e56c7..e859edd05dff 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -243,6 +243,18 @@ def _check_color_like(**kwargs): raise ValueError(f"{v!r} is not a valid value for {k}") +def _check_color_like_list(**kwargs): + """ + For each *key, lst* pair in *kwargs*, check that every + element *v* in *lst* is color-like. + """ + for k, lst in kwargs.items(): + invalid_col = [c for c in lst if not is_color_like(c)] + if invalid_col: + err_msg = f"{invalid_col!r} are not valid values for {k}" + raise ValueError(err_msg) + + def same_color(c1, c2): """ Return whether the colors *c1* and *c2* are the same. diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index a5809f0fa89f..6680581d49fb 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1,6 +1,7 @@ import copy import itertools import unittest.mock +from re import escape from io import BytesIO import numpy as np @@ -1592,3 +1593,20 @@ def test_cm_set_cmap_error(): bad_cmap = 'AardvarksAreAwkward' with pytest.raises(ValueError, match=bad_cmap): sm.set_cmap(bad_cmap) + + +def test_check_color_like(): + err_msg = "['abcd'] is not a valid value for c" + assert mcolors._check_color_like(colors1='yellow', colors2='red') is None + with pytest.raises(ValueError, match=err_msg): + mcolors._check_color_like(c='abcd') + + +def test_check_color_like_list(): + err_msg = escape("['abcd'] are not valid values for c") + assert mcolors._check_color_like_list(colors=['yellow', 'orange']) is None + assert mcolors._check_color_like_list(c1=['red'], c2=['blue']) is None + with pytest.raises(ValueError, match=err_msg): + mcolors._check_color_like_list(c=['abcd', 'red']) + with pytest.raises(ValueError, match=err_msg): + mcolors._check_color_like_list(c1=['red', 'blue'], c=['abcd'])