Skip to content

Commit

Permalink
Add __hash__ to ManimColor (#4051)
Browse files Browse the repository at this point in the history
* add __hash__ to ManimColor

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update core.py

* add test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: correct typehint

* fix test

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Benjamin Hackl <[email protected]>
  • Loading branch information
3 people authored Jan 19, 2025
1 parent 97f818e commit 86a9719
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions manim/utils/color/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,9 @@ def __xor__(self, other: Self) -> Self:
self._internal_from_integer(self.to_integer() ^ int(other), 1.0)
)

def __hash__(self) -> int:
return hash(self.to_hex(with_alpha=True))


RGBA = ManimColor
"""RGBA Color Space"""
Expand Down
8 changes: 7 additions & 1 deletion tests/module/utils/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from manim import BLACK, Mobject, Scene, VMobject
from manim import BLACK, RED, WHITE, ManimColor, Mobject, Scene, VMobject


def test_import_color():
Expand Down Expand Up @@ -49,3 +49,9 @@ def test_set_color():
assert m.color.to_hex() == "#FFFFFF"
m.set_color(BLACK)
assert m.color.to_hex() == "#000000"


def test_color_hash():
assert hash(WHITE) == hash(ManimColor([1.0, 1.0, 1.0, 1.0]))
assert hash(WHITE) == hash("#FFFFFFFF")
assert hash(WHITE) != hash(RED)

0 comments on commit 86a9719

Please sign in to comment.