from manim import *
import numpy as np
def head(cn, en):
    t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
    s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
    return VGroup(t, s)
def foot(cn):
    return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
class RotateAndColor(Animation):
    """自定义动画：旋转的同时做颜色插值。"""
    def __init__(self, mobject, angle=TAU, start_color=BLUE,
                 end_color=RED, about_point=None, **kwargs):
        self.base = mobject.copy()
        self.angle = angle
        self.start_color = start_color
        self.end_color = end_color
        self.about_point = about_point
        super().__init__(mobject, **kwargs)
    def interpolate_mobject(self, alpha):
        self.mobject.become(self.base)
        self.mobject.rotate(alpha * self.angle, about_point=self.about_point or self.base.get_center())
        col = interpolate_color(self.start_color, self.end_color, alpha)
        self.mobject.set_stroke(col, width=7)
        if hasattr(self.mobject, "set_fill"):
            self.mobject.set_fill(col, opacity=0.35)
class RotateColorBasic(Scene):
    def construct(self):
        self.add(head("旋转变色：自定义 Animation", "RotateAndColor / interpolate_mobject"))
        sq = Square(side_length=2.2, color=BLUE).set_fill(BLUE, opacity=0.3).shift(LEFT * 2.6)
        tri = RegularPolygon(n=5, color=BLUE).scale(1.1).shift(RIGHT * 2.6)
        self.add(sq, tri)
        self.play(RotateAndColor(sq, angle=TAU, start_color=BLUE, end_color=RED, run_time=3))
        self.play(RotateAndColor(tri, angle=-TAU, start_color=GREEN, end_color=YELLOW, run_time=3))
        self.play(FadeIn(foot("become 还原初始状态，避免逐帧累积旋转")), run_time=0.4)
        self.wait(1.0)
