4545"""
4646
4747from adafruit_led_animation .animation import Animation
48- from adafruit_led_animation .color import BLACK
48+ from adafruit_led_animation .color import BLACK , calculate_intensity
4949
5050
5151class Comet (Animation ):
@@ -62,7 +62,7 @@ class Comet(Animation):
6262 :param bool bounce: Comet will bounce back and forth. Defaults to ``True``.
6363 """
6464
65- # pylint: disable=too-many-arguments
65+ # pylint: disable=too-many-arguments,too-many-instance-attributes
6666 def __init__ (
6767 self ,
6868 pixel_object ,
@@ -75,75 +75,59 @@ def __init__(
7575 ):
7676 if tail_length == 0 :
7777 tail_length = len (pixel_object ) // 4
78- else :
79- tail_length = max (2 , min (tail_length , len (pixel_object )))
80- self ._tail_length = tail_length
81- self ._color_step = 0.9 / tail_length
82- self ._color_offset = 0.1
83- self ._comet_colors = None
84- self ._reverse_comet_colors = None
85- self ._initial_reverse = reverse
8678 self .reverse = reverse
8779 self .bounce = bounce
80+ self ._initial_reverse = reverse
81+ self ._tail_length = tail_length
82+ self ._color_step = 0.95 / tail_length
83+ self ._comet_colors = None
8884 self ._computed_color = color
89- self ._generator = self ._comet_generator ()
85+ self ._num_pixels = len (pixel_object )
86+ self ._direction = - 1 if reverse else 1
87+ self ._left_side = - self ._tail_length
88+ self ._right_side = self ._num_pixels
89+ self ._tail_start = 0
90+ self .reset ()
9091 super ().__init__ (pixel_object , speed , color , name = name )
9192
9293 on_cycle_complete_supported = True
9394
9495 def _recompute_color (self , color ):
95- pass
96+ self . _comet_recompute_color ( color )
9697
9798 def _comet_recompute_color (self , color ):
98- self ._comet_colors = [BLACK ] + [
99- [
100- int (color [rgb ] * ((n * self ._color_step ) + self ._color_offset ))
101- for rgb in range (len (color ))
102- ]
103- for n in range (self ._tail_length - 1 )
104- ]
105- self ._reverse_comet_colors = list (reversed (self ._comet_colors ))
99+ self ._comet_colors = [BLACK ]
100+ for n in range (self ._tail_length ):
101+ self ._comet_colors .append (
102+ calculate_intensity (color , n * self ._color_step + 0.05 )
103+ )
106104 self ._computed_color = color
107105
108- def _get_range (self , num_pixels ):
106+ def draw (self ):
107+ colors = self ._comet_colors
109108 if self .reverse :
110- return range (num_pixels , - self ._tail_length - 1 , - 1 )
111- return range (- self ._tail_length , num_pixels + 1 )
112-
113- def _comet_generator (self ):
114- num_pixels = len (self .pixel_object )
115- cycle_passes = 0
116- while True :
117- if self ._color != self ._computed_color or not self ._comet_colors :
118- self ._comet_recompute_color (self ._color )
119- colors = self ._reverse_comet_colors if self .reverse else self ._comet_colors
120- for start in self ._get_range (num_pixels ):
121-
122- if start + self ._tail_length < num_pixels :
123- end = self ._tail_length
124- else :
125- end = num_pixels - start
126- if start <= 0 :
127- num_visible = self ._tail_length + start
128- self .pixel_object [0 :num_visible ] = colors [
129- self ._tail_length - num_visible :
130- ]
131- else :
132- self .pixel_object [start : start + end ] = colors [0 :end ]
133- yield
134- cycle_passes += 1
109+ colors = reversed (colors )
110+ for pixel_no , color in enumerate (colors ):
111+ draw_at = self ._tail_start + pixel_no
112+ if draw_at < 0 or draw_at >= self ._num_pixels :
113+ continue
114+ self .pixel_object [draw_at ] = color
115+
116+ self ._tail_start += self ._direction
117+
118+ if self ._tail_start < self ._left_side or self ._tail_start >= self ._right_side :
135119 if self .bounce :
136120 self .reverse = not self .reverse
137- if not self .bounce or cycle_passes == 2 :
121+ self ._direction = - self ._direction
122+ if self .reverse == self ._initial_reverse :
138123 self .cycle_complete = True
139- cycle_passes = 0
140-
141- def draw (self ):
142- next (self ._generator )
143124
144125 def reset (self ):
145126 """
146127 Resets to the first color.
147128 """
148- self ._generator = self ._comet_generator ()
149129 self .reverse = self ._initial_reverse
130+ if self .reverse :
131+ self ._tail_start = self ._num_pixels + self ._tail_length + 1
132+ else :
133+ self ._tail_start = - self ._tail_length - 1
0 commit comments