from manim import *
import numpy as np
import sympy as sp
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)
def bg_image(h=60, w=100, mode=0):
    a = np.zeros((h, w, 4), dtype=np.uint8)
    a[:, :, 3] = 255
    if mode == 0:
        a[:, :, 0] = np.linspace(10, 90, w, dtype=np.uint8)[None, :]
        a[:, :, 2] = np.linspace(60, 160, w, dtype=np.uint8)[None, :]
    elif mode == 1:
        a[:, :, 1] = np.linspace(20, 110, h, dtype=np.uint8)[:, None]
        a[:, :, 2] = np.linspace(40, 130, h, dtype=np.uint8)[:, None]
    else:
        a[:, :, 0] = 120
        a[:, :, 1] = 70
        a[:, :, 2] = np.linspace(30, 140, w, dtype=np.uint8)[None, :]
    return ImageMobject(a)
class BgMask(Scene):
    def construct(self):
        bg = bg_image(mode=2).scale_to_fit_width(config.frame_width).scale_to_fit_height(config.frame_height)
        self.add(bg)
        mask = Rectangle(width=config.frame_width, height=config.frame_height,
                         color=BLACK).set_fill(BLACK, opacity=0.55).set_stroke(opacity=0)
        self.add(mask)
        self.add(head("背景图：叠一层暗色遮罩", "set_fill / opacity"))
        txt = Text("遮罩之后文字更好读", font="STLiti", font_size=42, color=WHITE)
        self.play(Write(txt), run_time=2.4)
        self.play(txt.animate.shift(UP * 0.8), run_time=1.2)
        self.play(FadeIn(foot("矩形遮罩是最简单也最可控的背景处理方式")), run_time=0.4)
        self.wait(1.6)
