颜色效果
概述
颜色效果 API 提供生成动态灯光效果的实用函数,如彩虹、呼吸、闪烁等基于时间和相位偏移的颜色调制效果。通过 ColorEffects 访问这些函数。
Python 颜色效果 API 实现位于 Applications/Python/PikaPython/MatrixOS_ColorEffects.py,类型提示位于 Applications/Python/PikaPython/_MatrixOS_ColorEffects.pyi。
颜色生成效果
ColorEffects.Rainbow
def Rainbow(period: int = 1000, offset: int = 0) -> Color
生成彩虹色效果,在指定周期内循环遍历色相光谱。
参数:
period(int, 可选):完整颜色循环的持续时间(毫秒),默认为1000msoffset(int, 可选):调整起始色相的偏移量,默认为0
返回值:
Color:彩虹循环中的当前颜色
示例:
# 基础彩虹效果
rainbow_color = ColorEffects.Rainbow()
MatrixOS.LED.Fill(rainbow_color)
MatrixOS.LED.Update()
# 更快的彩虹,带偏移
fast_rainbow = ColorEffects.Rainbow(500, 100)
MatrixOS.LED.SetColor(Point(0, 0), fast_rainbow)
亮度调制效果
ColorEffects.Breath
def Breath(period: int = 1000, offset: int = 0) -> int
生成以正弦波模式平滑过渡的亮度值(呼吸效果)。
参数:
period(int, 可选):呼吸循环的持续时间(毫秒),默认为1000msoffset(int, 可选):调整呼吸相位的偏移量,默认为0
返回值:
int:亮度值(0-255)
示例:
# 创建呼吸效果
brightness = ColorEffects.Breath(2000) # 2秒循环
MatrixOS.LED.Fill(Color(255, 0, 0), brightness)
MatrixOS.LED.Update(brightness)
ColorEffects.BreathLowBound
def BreathLowBound(low_bound: int = 64, period: int = 1000, offset: int = 0) -> int
生成具有最低亮度值的呼吸效果。
参数:
low_bound(int, 可选):最低亮度值,默认为64period(int, 可选):呼吸循环的持续时间(毫秒),默认为1000msoffset(int, 可选):调整呼吸相位的偏移量,默认为0
返回值:
int:亮度值(low_bound 到 255)
示例:
# 永远不会完全变暗的呼吸效果
brightness = ColorEffects.BreathLowBound(100, 1500)
MatrixOS.LED.Fill(Color(0, 255, 0), brightness)
ColorEffects.Strobe
def Strobe(period: int = 1000, offset: int = 0) -> int
通过在满亮度和关闭状态之间交替来生成闪烁效果。
参数:
period(int, 可选):一个闪烁循环的持续时间(毫秒),默认为1000msoffset(int, 可选):调整闪烁相位的偏移量,默认为0
返回值:
int:亮度值(0 或 255)
示例:
# 快速闪烁效果
brightness = ColorEffects.Strobe(200) # 200ms 循环
MatrixOS.LED.Fill(Color(255, 255, 255), brightness)
ColorEffects.Saw
def Saw(period: int = 1000, offset: int = 0) -> int
生成用于亮度的锯齿波形,从 0 到 255 线性循环。
参数:
period(int, 可选):一个锯齿循环的持续时间(毫秒),默认为1000msoffset(int, 可选):调整相位的偏移量,默认为0
返回值:
int:亮度值(0-255)
示例:
# 锯齿亮度渐变
brightness = ColorEffects.Saw(3000) # 3秒渐亮
MatrixOS.LED.Fill(Color(0, 0, 255), brightness)
ColorEffects.Triangle
def Triangle(period: int = 1000, offset: int = 0) -> int
生成用于亮度的三角波形,在 0 和 255 之间上下循环。
参数:
period(int, 可选):一个三角循环的持续时间(毫秒),默认为1000msoffset(int, 可选):调整相位的偏移量,默认为0
返回值:
int:亮度值(0-255)
示例:
# 三角波亮度
brightness = ColorEffects.Triangle(2000)
MatrixOS.LED.Fill(Color(255, 255, 0), brightness)