feat(03-02): implement animation library — 7 types with registry and factory
- AnimationBase ABC with render(t, led_count) and from_params() contract - SolidColorAnimation: time-independent uniform fill - ChaseAnimation: moving lit segment with forward/reverse support - PulseAnimation: sine-wave breathing effect (speed maps to period) - RainbowAnimation: HSV hue cycle across strip via colorsys.hsv_to_rgb - StrobeAnimation: on/off flashing at 1-20 Hz with configurable duty cycle - ColorWipeAnimation: progressive fill from start or end - FireAnimation: Fire2012 algorithm with cooling/sparking/heat-to-color mapping - ANIMATION_REGISTRY + create_animation() factory function - All 24 tests passing
This commit is contained in:
38
lightsync/animations/rainbow.py
Normal file
38
lightsync/animations/rainbow.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""Rainbow animation — full HSV hue cycle across the strip."""
|
||||
import colorsys
|
||||
from lightsync.animations.base import AnimationBase
|
||||
|
||||
|
||||
class RainbowAnimation(AnimationBase):
|
||||
"""Hue-shifting rainbow across the entire strip.
|
||||
|
||||
period=1.0 means one full rainbow cycle fits on the strip.
|
||||
speed controls how fast the hues shift over time.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
speed: float = 0.5,
|
||||
period: float = 1.0,
|
||||
) -> None:
|
||||
self.speed = speed
|
||||
self.period = period
|
||||
|
||||
def render(self, t: float, led_count: int) -> list[tuple]:
|
||||
pixels = []
|
||||
for i in range(led_count):
|
||||
hue = (i / led_count / self.period + t * self.speed) % 1.0
|
||||
r_f, g_f, b_f = colorsys.hsv_to_rgb(hue, 1.0, 1.0)
|
||||
pixels.append((
|
||||
int(r_f * 255),
|
||||
int(g_f * 255),
|
||||
int(b_f * 255),
|
||||
))
|
||||
return pixels
|
||||
|
||||
@classmethod
|
||||
def from_params(cls, params: dict) -> "RainbowAnimation":
|
||||
return cls(
|
||||
speed=params.get("speed", 0.5),
|
||||
period=params.get("period", 1.0),
|
||||
)
|
||||
Reference in New Issue
Block a user