Skip to content

Commit 1a59bc8

Browse files
committed
bugfixes and rainbowcomet adjustments
1 parent c9c7e14 commit 1a59bc8

File tree

3 files changed

+54
-37
lines changed

3 files changed

+54
-37
lines changed

adafruit_led_animation/animation/comet.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ class Comet(Animation):
6464

6565
# pylint: disable=too-many-arguments
6666
def __init__(
67-
self,
68-
pixel_object,
69-
speed,
70-
color,
71-
tail_length=0,
72-
reverse=False,
73-
bounce=False,
74-
name=None,
67+
self,
68+
pixel_object,
69+
speed,
70+
color,
71+
tail_length=0,
72+
reverse=False,
73+
bounce=False,
74+
name=None,
7575
):
7676
if tail_length == 0:
7777
tail_length = len(pixel_object) // 4
@@ -98,9 +98,10 @@ def _recompute_color(self, color):
9898
def _comet_recompute_color(self, color):
9999
self._comet_colors = [BLACK]
100100
for n in range(self._tail_length):
101-
self._comet_colors.append(calculate_intensity(color, n * self._color_step + 0.05))
101+
self._comet_colors.append(
102+
calculate_intensity(color, n * self._color_step + 0.05)
103+
)
102104
self._computed_color = color
103-
print(self._comet_colors)
104105

105106
def draw(self):
106107
for pixel_no in range(self._tail_length + 1):
@@ -113,8 +114,10 @@ def draw(self):
113114

114115
if self._tail_start < self._left_side or self._tail_start > self._right_side:
115116
self.reverse = not self.reverse
116-
self._direction = - self._direction
117+
self._direction = -self._direction
117118
self._tail_start += self._direction
119+
if self.reverse == self._initial_reverse:
120+
self.cycle_complete = True
118121

119122
def reset(self):
120123
"""

adafruit_led_animation/animation/rainbowcomet.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"""
4646

4747
from adafruit_led_animation.animation.comet import Comet
48-
from adafruit_led_animation.color import colorwheel, BLACK
48+
from adafruit_led_animation.color import colorwheel, BLACK, calculate_intensity
4949

5050

5151
class RainbowComet(Comet):
@@ -60,6 +60,7 @@ class RainbowComet(Comet):
6060
:param bool reverse: Animates the comet in the reverse order. Defaults to ``False``.
6161
:param bool bounce: Comet will bounce back and forth. Defaults to ``True``.
6262
:param int colorwheel_offset: Offset from start of colorwheel (0-255).
63+
:param int step: Colorwheel step (defaults to automatic).
6364
"""
6465

6566
# pylint: disable=too-many-arguments
@@ -71,30 +72,27 @@ def __init__(
7172
reverse=False,
7273
bounce=False,
7374
colorwheel_offset=0,
75+
step=0,
7476
name=None,
7577
):
76-
self._colorwheel_is_tuple = isinstance(colorwheel(0), tuple)
78+
if step == 0:
79+
self._colorwheel_step = int(256 / tail_length)
80+
else:
81+
self._colorwheel_step = step
7782
self._colorwheel_offset = colorwheel_offset
78-
7983
super().__init__(pixel_object, speed, 0, tail_length, reverse, bounce, name)
8084

81-
def _calc_brightness(self, n, color):
82-
brightness = (n * self._color_step) + self._color_offset
83-
if not self._colorwheel_is_tuple:
84-
color = (color & 0xFF, ((color & 0xFF00) >> 8), (color >> 16))
85-
return [int(i * brightness) for i in color]
86-
8785
def _comet_recompute_color(self, color):
88-
factor = int(256 / self._tail_length)
89-
self._comet_colors = [BLACK] + [
90-
self._calc_brightness(
91-
n,
92-
colorwheel(
93-
int((n * factor) + self._color_offset + self._colorwheel_offset)
94-
% 256
95-
),
86+
self._comet_colors = [BLACK]
87+
for n in range(self._tail_length):
88+
invert = self._tail_length - n - 1
89+
self._comet_colors.append(
90+
calculate_intensity(
91+
colorwheel(
92+
int((invert * self._colorwheel_step) + self._colorwheel_offset)
93+
% 256
94+
),
95+
n * self._color_step + 0.05,
96+
)
9697
)
97-
for n in range(self._tail_length - 1)
98-
]
99-
self._reverse_comet_colors = list(reversed(self._comet_colors))
10098
self._computed_color = color

adafruit_led_animation/color.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,28 @@ def calculate_intensity(color, intensity=1.0):
7474
:return: color
7575
"""
7676
if isinstance(color, int):
77-
return ((int((color & 0xff0000) * intensity) & 0xff0000) |
78-
(int((color & 0xff00) * intensity) & 0xff00) |
79-
(int((color & 0xff) * intensity) & 0xff))
77+
return (
78+
(int((color & 0xFF0000) * intensity) & 0xFF0000)
79+
| (int((color & 0xFF00) * intensity) & 0xFF00)
80+
| (int((color & 0xFF) * intensity) & 0xFF)
81+
)
8082

8183
if len(color) == 3:
82-
return int(color[0] * intensity), int(color[1] * intensity), int(color[2] * intensity)
84+
return (
85+
int(color[0] * intensity),
86+
int(color[1] * intensity),
87+
int(color[2] * intensity),
88+
)
8389
elif len(color) == 4 and isinstance(color[3], float):
84-
return int(color[0] * intensity), int(color[1] * intensity), int(color[2] * intensity), color[3]
85-
return int(color[0] * intensity), int(color[1] * intensity), int(color[2] * intensity), int(color[3] * intensity)
90+
return (
91+
int(color[0] * intensity),
92+
int(color[1] * intensity),
93+
int(color[2] * intensity),
94+
color[3],
95+
)
96+
return (
97+
int(color[0] * intensity),
98+
int(color[1] * intensity),
99+
int(color[2] * intensity),
100+
int(color[3] * intensity),
101+
)

0 commit comments

Comments
 (0)