diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 6da028e81b4f..d37055fc05ed 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -112,11 +112,38 @@ def is_color_like(c): return True +def same_color(c1, c2): + """ + Compare two colors to see if they are the same. + + Parameters + ---------- + c1, c2 : Matplotlib colors + + Returns + ------- + bool + ``True`` if *c1* and *c2* are the same color, otherwise ``False``. + """ + return (to_rgba_array(c1) == to_rgba_array(c2)).all() + + def to_rgba(c, alpha=None): - """Convert *c* to an RGBA color. + """ + Convert *c* to an RGBA color. - If *alpha* is not *None*, it forces the alpha value, except if *c* is - ``"none"`` (case-insensitive), which always maps to ``(0, 0, 0, 0)``. + Parameters + ---------- + c : Matplotlib color + + alpha : scalar, optional + If *alpha* is not ``None``, it forces the alpha value, except if *c* is + ``"none"`` (case-insensitive), which always maps to ``(0, 0, 0, 0)``. + + Returns + ------- + tuple + Tuple of ``(r, g, b, a)`` scalars. """ # Special-case nth color syntax because it should not be cached. if _is_nth_color(c): diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 264d75ebe57a..a6e3002b0735 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -715,3 +715,8 @@ def __add__(self, other): else: assert len(recwarn) == 0 recwarn.clear() + + +def test_same_color(): + assert mcolors.same_color('k', (0, 0, 0)) + assert not mcolors.same_color('w', (1, 1, 0))