Skip to content

Commit af185be

Browse files
authored
Merge pull request #10032 from dstansby/same-color
Add method for comparing two colors
2 parents fb2c15b + b06cae8 commit af185be

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/matplotlib/colors.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,38 @@ def is_color_like(c):
112112
return True
113113

114114

115+
def same_color(c1, c2):
116+
"""
117+
Compare two colors to see if they are the same.
118+
119+
Parameters
120+
----------
121+
c1, c2 : Matplotlib colors
122+
123+
Returns
124+
-------
125+
bool
126+
``True`` if *c1* and *c2* are the same color, otherwise ``False``.
127+
"""
128+
return (to_rgba_array(c1) == to_rgba_array(c2)).all()
129+
130+
115131
def to_rgba(c, alpha=None):
116-
"""Convert *c* to an RGBA color.
132+
"""
133+
Convert *c* to an RGBA color.
117134
118-
If *alpha* is not *None*, it forces the alpha value, except if *c* is
119-
``"none"`` (case-insensitive), which always maps to ``(0, 0, 0, 0)``.
135+
Parameters
136+
----------
137+
c : Matplotlib color
138+
139+
alpha : scalar, optional
140+
If *alpha* is not ``None``, it forces the alpha value, except if *c* is
141+
``"none"`` (case-insensitive), which always maps to ``(0, 0, 0, 0)``.
142+
143+
Returns
144+
-------
145+
tuple
146+
Tuple of ``(r, g, b, a)`` scalars.
120147
"""
121148
# Special-case nth color syntax because it should not be cached.
122149
if _is_nth_color(c):

lib/matplotlib/tests/test_colors.py

+5
Original file line numberDiff line numberDiff line change
@@ -715,3 +715,8 @@ def __add__(self, other):
715715
else:
716716
assert len(recwarn) == 0
717717
recwarn.clear()
718+
719+
720+
def test_same_color():
721+
assert mcolors.same_color('k', (0, 0, 0))
722+
assert not mcolors.same_color('w', (1, 1, 0))

0 commit comments

Comments
 (0)