fix color setting so that animations like blink aren't reset on color setting

This commit is contained in:
Roy Hooper 2020-06-22 17:50:16 -04:00
parent 3dbe521a49
commit 7b5ba8222e
6 changed files with 13 additions and 16 deletions

View file

@ -68,7 +68,7 @@ class Animation:
self._time_left_at_pause = 0
self._also_notify = []
self.speed = speed # sets _speed_ns
self.color = color # Triggers _recompute_color
self.color = color # Triggers _set_color
self.name = name
self.cycle_complete = False
self.notify_cycles = 1
@ -185,10 +185,16 @@ class Animation:
def color(self, color):
if self._color == color:
return
self._set_color(color)
def _set_color(self, color):
"""
Called after the color is changed, which includes at initialization.
Override as needed.
"""
if isinstance(color, int):
color = (color >> 16 & 0xFF, color >> 8 & 0xFF, color & 0xFF)
self._color = color
self._recompute_color(color)
@property
def speed(self):
@ -201,12 +207,6 @@ class Animation:
def speed(self, seconds):
self._speed_ns = int(seconds * NANOS_PER_SECOND)
def _recompute_color(self, color):
"""
Called if the color is changed, which includes at initialization.
Override as needed.
"""
def on_cycle_complete(self):
"""
Called by some animations when they complete an animation cycle.

View file

@ -60,5 +60,5 @@ class Blink(ColorCycle):
def __init__(self, pixel_object, speed, color, name=None):
super().__init__(pixel_object, speed, [color, BLACK], name=name)
def _recompute_color(self, color):
def _set_color(self, color):
self.colors = [color, BLACK]

View file

@ -92,10 +92,7 @@ class Comet(Animation):
on_cycle_complete_supported = True
def _recompute_color(self, color):
self._comet_recompute_color(color)
def _comet_recompute_color(self, color):
def _set_color(self, color):
self._comet_colors = [BLACK]
for n in range(self._tail_length):
self._comet_colors.append(

View file

@ -82,7 +82,7 @@ class RainbowComet(Comet):
self._colorwheel_offset = colorwheel_offset
super().__init__(pixel_object, speed, 0, tail_length, reverse, bounce, name)
def _comet_recompute_color(self, color):
def _set_color(self, color):
self._comet_colors = [BLACK]
for n in range(self._tail_length):
invert = self._tail_length - n - 1

View file

@ -58,5 +58,5 @@ class Solid(ColorCycle):
def __init__(self, pixel_object, color, name=None):
super().__init__(pixel_object, speed=1, colors=[color], name=name)
def _recompute_color(self, color):
def _set_color(self, color):
self.colors = [color]

View file

@ -71,7 +71,7 @@ class Sparkle(Animation):
self._pixels = []
super().__init__(pixel_object, speed, color, name=name)
def _recompute_color(self, color):
def _set_color(self, color):
half_color = tuple(color[rgb] // 4 for rgb in range(len(color)))
dim_color = tuple(color[rgb] // 10 for rgb in range(len(color)))
for pixel in range(len(self.pixel_object)):