You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Manim动画代码时遇“Failed to parse a color from red”错误求助

解决Manim颜色解析错误问题

问题原因

你导入了第三方colour库的Color类生成颜色,但Manim的颜色系统无法直接解析这个第三方库的Color对象,因此抛出ValueError: Failed to parse a color from red错误。同时原代码中polys变量存在缩进错误,也会影响代码执行。

解决方案

提供两种可行的修改方式:

方式1:使用Manim内置颜色生成方式

无需依赖第三方colour库,直接用Manim自带的Color类生成颜色,它支持直接传入HSL参数:

from manim import *

class BasicAnimations(Scene):
    def construct(self):
        polys = VGroup(
            *[RegularPolygon(5, radius=1, fill_opacity=0.5,
                          color=Color(hsl=(j/5, 1, 0.5))) for j in range(5)]
        ).arrange(RIGHT)
        self.play(DrawBorderThenFill(polys), run_time=2)
        self.play(
            Rotate(polys[0], PI, rate_func=lambda t: t), # rate_func=linear
            Rotate(polys[1], PI, rate_func=smooth), # default behavior
            Rotate(polys[2], PI, rate_func=lambda t: np.sin(t*PI)),
            Rotate(polys[3], PI, rate_func=there_and_back),
            Rotate(polys[4], PI, rate_func=lambda t: 1 - abs(1-2*t)),
            run_time=2
        )
        self.wait()

方式2:保留colour库并转换为兼容格式

如果一定要用colour库,只需将生成的Color对象转换为Manim能识别的十六进制字符串即可:

from manim import *
from colour import Color

class BasicAnimations(Scene):
    def construct(self):
        polys = VGroup(
            *[RegularPolygon(5, radius=1, fill_opacity=0.5,
                          color=Color(hue=j/5, saturation=1, luminance=0.5).get_hex()) for j in range(5)]
        ).arrange(RIGHT)
        self.play(DrawBorderThenFill(polys), run_time=2)
        self.play(
            Rotate(polys[0], PI, rate_func=lambda t: t), # rate_func=linear
            Rotate(polys[1], PI, rate_func=smooth), # default behavior
            Rotate(polys[2], PI, rate_func=lambda t: np.sin(t*PI)),
            Rotate(polys[3], PI, rate_func=there_and_back),
            Rotate(polys[4], PI, rate_func=lambda t: 1 - abs(1-2*t)),
            run_time=2
        )
        self.wait()

内容的提问来源于stack exchange,提问作者user23183160

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 05:35:17