From 93b61b65931f268e7856200432932ed82ba11a20 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 17 Dec 2017 12:47:01 +0000 Subject: [PATCH 1/3] Add method for comparing two colors --- lib/matplotlib/colors.py | 33 ++++++++++++++++++++++++++--- lib/matplotlib/tests/test_colors.py | 5 +++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 6da028e81b4f..9316495ea295 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 ``Fase``. + """ + return to_rgba(c1) == to_rgba(c2) + + 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)) From acd94e14e1932d287c5b415043b021d937386ddb Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 17 Dec 2017 12:48:31 +0000 Subject: [PATCH 2/3] Spelling mistake --- lib/matplotlib/colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 9316495ea295..15796b9d25b9 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -123,7 +123,7 @@ def same_color(c1, c2): Returns ------- bool - ``True`` if *c1* and *c2* are the same color, otherwise ``Fase``. + ``True`` if *c1* and *c2* are the same color, otherwise ``False``. """ return to_rgba(c1) == to_rgba(c2) From b06cae8fb86b217e1ff0742106d33f50065dab06 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 17 Dec 2017 15:57:14 +0000 Subject: [PATCH 3/3] Add array compatability --- lib/matplotlib/colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 15796b9d25b9..d37055fc05ed 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -125,7 +125,7 @@ def same_color(c1, c2): bool ``True`` if *c1* and *c2* are the same color, otherwise ``False``. """ - return to_rgba(c1) == to_rgba(c2) + return (to_rgba_array(c1) == to_rgba_array(c2)).all() def to_rgba(c, alpha=None):