James Hao
James Hao

Reputation: 905

How to flash mobject between two colors

I want to define a Animation which change color of a mobject between two colors during specific time period.

E.g. a mobject with color RED, I want to set its color between RED and YELLOW back and forth 5 times during 2 seconds.

Below is my code (can change color, but the interval is not even), just want to confirm whether it is the correct way to do this, thanks!:

class ChangeColor(Animation):
    def interpolate_submobject(self, submobject, starting_sumobject, alpha):
        m = int(alpha * 10) % 2
        if m == 0:
            submobject.set_color(RED)
        else:
            submobject.set_color(YELLOW)

Upvotes: 0

Views: 287

Answers (1)

TheoremOfBeethoven
TheoremOfBeethoven

Reputation: 1969

By default, alpha increment is smooth, so you have to change it to linear so that it is displayed correctly.

class ChangeColor(Animation):
    CONFIG={
        "rate_func":linear
    }
    def interpolate_submobject(self, submobject, starting_sumobject, alpha):
        m = int(alpha * 10) % 2
        if m == 0:
            submobject.set_color(RED)
        else:
            submobject.set_color(YELLOW)

Upvotes: 1

Related Questions