Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __hash__ to ManimColor #4051

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

sophiawisdom
Copy link

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

Copy link
Member

@JasonGrace2282 JasonGrace2282 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Left one comment, otherwise it should be ready to go.

It would also be nice to have a test for this, maybe something like

def test_color_hash():
    assert hash(WHITE) == hash(WHITE.copy())
    assert hash(WHITE) != hash(RED)

@@ -1021,6 +1021,9 @@ def __xor__(self, other: Self) -> Self:
self._internal_from_integer(self.to_integer() ^ int(other), 1.0)
)

def __hash__(self) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python docs recommend that __hash__ returns an integer.
Can you just return hash(self.to_hex(with_alpha=True))?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to change the return typehint to int.

@chopan050
Copy link
Contributor

chopan050 commented Dec 12, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants