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

Fix of ImageMobject opacity bug #4089

Merged
merged 2 commits into from
Jan 19, 2025
Merged

Conversation

henrikmidtiby
Copy link
Contributor

@henrikmidtiby henrikmidtiby commented Jan 2, 2025

Overview: What does this pull request change?

Closes #4072
The issue was introduced in #3960 as part of an optimization.

Further Information and Comments

I would suggest to add a unittest that ensures that the bug is not reintroduced.

The problem was related to handling of unsigned integers.
The code below demonstrates the problem.

import numpy as np

def main():
    start = np.array(127, dtype=np.uint8)
    end = np.array(25, dtype=np.uint8)
    alpha = 0.09739

    val1 = start + alpha * (end - start)
    val2 = (1 - alpha) * start + alpha * end
    print(val1)
    print(val2)
    print(141)
    print(117)

main()

Which outputs

141.99806
117.06622
141
117

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
Contributor

@chopan050 chopan050 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 noticing this! As mentioned in Discord, this was caused by an underflow when the end value is less than the start value and both of them are unsigned integers. In your code, 25 - 127 = -102, but, because 0 is the lowest representable integer, it goes all the way around from 255 and down to 154 (which is -102 + 256) instead.

I'll think about a unit test. Any suggestions are well appreciated.

@henrikmidtiby
Copy link
Contributor Author

I have added two unittests for the interpolate function. See commit 580e027.

@behackl behackl added the pr:bugfix Bug fix for use in PRs solving a specific issue:bug label Jan 19, 2025
@behackl behackl added this to the v0.19.0 milestone Jan 19, 2025
Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

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

Thanks!

@behackl behackl merged commit 95b4625 into ManimCommunity:main Jan 19, 2025
21 checks passed
@henrikmidtiby henrikmidtiby deleted the Issue4072 branch January 21, 2025 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:bugfix Bug fix for use in PRs solving a specific issue:bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FadeOut and .animate.set_opacity() are not working correctly on ImageMobject
3 participants