manim
manim copied to clipboard
Add __hash__ to ManimColor
Overview: What does this pull request change?
Adds a hash method to ManimColor
Motivation and Explanation: Why and how do your changes improve the library?
I wanted to use the Color in a dictionary and it didn't work and I thought other people might want the same.
Links to added or changed documentation pages
Further Information and Comments
Reviewer Checklist
- [ ] The PR title is descriptive enough for the changelog, and the PR is labeled correctly
- [ ] If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
- [ ] If applicable: newly added functions and classes are tested
ManimColor does not have a .copy() method. Maybe, instead of WHITE.copy(), the test could use ManimColor(WHITE), but this would only generate a reference to the same internal RGBA array in the current implementation. Another option is ManimColor([1.0, 1.0, 1.0, 1.0]), which represents the color white and generates a new RGBA array.
def test_color_hash():
assert hash(WHITE) == hash(ManimColor([1.0, 1.0, 1.0, 1.0]))
assert hash(WHITE) != hash(RED)
Because WHITE.to_hex(with_alpha=True) returns "#FFFFFFFF", you can also compare it directly with the hash of this string.
def test_color_hash():
assert hash(WHITE) == hash("#FFFFFFFF")
assert hash(WHITE) != hash(RED)