Skip to content

Use ManimColor opacity in VMobjects along with object's opacity - #4940

Open
UnMolDeQuimica wants to merge 2 commits into
ManimCommunity:mainfrom
UnMolDeQuimica:manim_color_opacity_clash_with_vmobject_opacity_fix
Open

Use ManimColor opacity in VMobjects along with object's opacity#4940
UnMolDeQuimica wants to merge 2 commits into
ManimCommunity:mainfrom
UnMolDeQuimica:manim_color_opacity_clash_with_vmobject_opacity_fix

Conversation

@UnMolDeQuimica

Copy link
Copy Markdown

Overview: What does this pull request change?

Currently, when using a ManimColor with an alpha value, the VMobject opacity overwrites said value. With these changes the opacity of the ManimColor is taken in account.

It is done by multiplying the ManimColor's alpha value with the VMobject opacity, so it supports existing code and animations.

Motivation and Explanation: Why and how do your changes improve the library?

As mentioned, setting the alpha value of a ManimColor did not affect the opacity of the resulting VMobject. This sounds, at least, counterintuitive to me. What if I want to use a single color in multiple objects and let the color handle the opacity? Or what if I want a gradient that supports transparent colors?

Here's an example of what can be done with these changes:

from manim import *


class ManimColorWithOpacityChangesScene(Scene):
    def construct(self):
        colors = [
            ManimColor(value="#4c1680", alpha=0),
            ManimColor(value="#4c1680", alpha=1),
            ManimColor(value="#0000ff", alpha=0),
            ManimColor(value="#0000ff", alpha=1),
            ManimColor(value="#0099ff", alpha=0),
            ManimColor(value="#0099ff", alpha=1),
            ManimColor(value="#00ff99", alpha=0),
            ManimColor(value="#00ff99", alpha=1),
            ManimColor(value="#00ff00", alpha=0),
            ManimColor(value="#00ff00", alpha=1),
            ManimColor(value="#99ff00", alpha=0),
            ManimColor(value="#99ff00", alpha=1),
            ManimColor(value="#ffff00", alpha=0),
            ManimColor(value="#ffff00", alpha=1),
            ManimColor(value="#ff9900", alpha=0),
            ManimColor(value="#ff9900", alpha=1),
            ManimColor(value="#ff0000", alpha=0),
            ManimColor(value="#ff0000", alpha=1)
        ]
        colors_gradient = color_gradient(colors, 1000)
        rectangle = Rectangle(width=8, fill_opacity=1, fill_color=colors_gradient, stroke_opacity=0, sheen_direction=RIGHT)
        self.wait()
        self.play(FadeIn(rectangle))
ManimColorWithOpacityChangesScene_ManimCE_v0 21 0

Here is a video example:

from manim import *


class ManimColorWithOpacityChangesScene(Scene):
    def construct(self):
        colors = [
            ManimColor(value="#4c1680", alpha=0),
            ManimColor(value="#4c1680", alpha=1),
            ManimColor(value="#0000ff", alpha=0),
            ManimColor(value="#0000ff", alpha=1),
            ManimColor(value="#0099ff", alpha=0),
            ManimColor(value="#0099ff", alpha=1),
            ManimColor(value="#00ff99", alpha=0),
            ManimColor(value="#00ff99", alpha=1),
            ManimColor(value="#00ff00", alpha=0),
            ManimColor(value="#00ff00", alpha=1),
            ManimColor(value="#99ff00", alpha=0),
            ManimColor(value="#99ff00", alpha=1),
            ManimColor(value="#ffff00", alpha=0),
            ManimColor(value="#ffff00", alpha=1),
            ManimColor(value="#ff9900", alpha=0),
            ManimColor(value="#ff9900", alpha=1),
            ManimColor(value="#ff0000", alpha=0),
            ManimColor(value="#ff0000", alpha=1)
        ]
        colors_gradient = color_gradient(colors, 1000)
        rectangle = Rectangle(width=8, fill_opacity=1, fill_color=colors_gradient, stroke_opacity=0, sheen_direction=RIGHT)
        self.wait()
        self.play(FadeIn(rectangle))
        self.wait()
        self.play(rectangle.animate.shift(UP))
        self.play(rectangle.animate.shift(DOWN))
        self.play(rectangle.animate.set_sheen_direction(DOWN))
        self.play(rectangle.animate.set_sheen_direction(LEFT))
        self.play(Transform(rectangle, Square(fill_color=RED, fill_opacity=1)))
        self.wait()
ManimColorWithOpacityChangesScene.mp4

Links to added or changed documentation pages

No documentation changes needed (I think).

Further Information and Comments

All tests pass

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

@UnMolDeQuimica

Copy link
Copy Markdown
Author

I am aware of this PR but I do not think it is still relevant or useful after the addition of ManimColors so I gave a shot to a different approach. Hope you guys like it!

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.

1 participant