Add Color comparison method (#3646)

* Add Color comparison method

* add changelog

* switch to public attribute

* compare rgb tuple

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
This commit is contained in:
Amin Alaee
2022-08-08 14:37:58 +02:00
committed by GitHub
parent 177ca2a6b2
commit 90a103ec3c
3 changed files with 13 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
Add comparison method for `Color` class.
+3
View File
@@ -198,6 +198,9 @@ class Color(Representation):
def __repr_args__(self) -> 'ReprArgs':
return [(None, self.as_named(fallback=True))] + [('rgb', self.as_rgb_tuple())] # type: ignore
def __eq__(self, other: Any) -> bool:
return isinstance(other, Color) and self.as_rgb_tuple() == other.as_rgb_tuple()
def parse_tuple(value: Tuple[Any, ...]) -> RGBA:
"""
+9
View File
@@ -184,3 +184,12 @@ def test_str_repr():
assert repr(Color('red')) == "Color('red', rgb=(255, 0, 0))"
assert str(Color((1, 2, 3))) == '#010203'
assert repr(Color((1, 2, 3))) == "Color('#010203', rgb=(1, 2, 3))"
def test_eq():
assert Color('red') == Color('red')
assert Color('red') != Color('blue')
assert Color('red') != 'red'
assert Color('red') == Color((255, 0, 0))
assert Color('red') != Color((0, 0, 255))