|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019-2020 Roy Hooper |
| 4 | +# Copyright (c) 2020 Kattni Rembor for Adafruit Industries |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in |
| 14 | +# all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +# THE SOFTWARE. |
| 23 | +""" |
| 24 | +`adafruit_led_animation.animation.rainbowcomet` |
| 25 | +================================================================================ |
| 26 | +
|
| 27 | +TODO |
| 28 | +
|
| 29 | +* Author(s): Roy Hooper, Kattni Rembor |
| 30 | +
|
| 31 | +""" |
| 32 | + |
| 33 | +import random |
| 34 | + |
| 35 | +from adafruit_led_animation.animation.rainbow import Rainbow |
| 36 | + |
| 37 | + |
| 38 | +class RainbowSparkle(Rainbow): |
| 39 | + |
| 40 | + def __init__(self, pixel_object, speed, period=5, num_sparkles=None, step=1, name=None, |
| 41 | + bg_brightness=0.2): |
| 42 | + self._num_sparkles = num_sparkles |
| 43 | + if num_sparkles is None: |
| 44 | + self._num_sparkles = max(1, int(len(pixel_object) / 20)) |
| 45 | + self._sparkle_duration = 2 |
| 46 | + self._bg_brightness = bg_brightness |
| 47 | + self._bright_colors = None |
| 48 | + super().__init__(pixel_object=pixel_object, speed=speed, period=period, step=step, name=name, |
| 49 | + precompute_rainbow=True) |
| 50 | + |
| 51 | + def generate_rainbow(self, step=1): |
| 52 | + super().generate_rainbow() |
| 53 | + self._bright_colors = self.colors[:] |
| 54 | + for i, color in enumerate(self.colors): |
| 55 | + if isinstance(self.colors[i], int): |
| 56 | + self.colors[i] = ( |
| 57 | + int(self._bg_brightness * ((color & 0xff0000) >> 16)), |
| 58 | + int(self._bg_brightness * ((color & 0xff00) >> 8)), |
| 59 | + int(self._bg_brightness * (color & 0xff)) |
| 60 | + ) |
| 61 | + else: |
| 62 | + self.colors[i] = ( |
| 63 | + int(self._bg_brightness * color[0]), |
| 64 | + int(self._bg_brightness * color[1]), |
| 65 | + int(self._bg_brightness * color[2]) |
| 66 | + ) |
| 67 | + |
| 68 | + def show(self): |
| 69 | + pixels = [ |
| 70 | + random.randint(0, len(self.pixel_object)-1) |
| 71 | + for n in range(self._num_sparkles) |
| 72 | + ] |
| 73 | + for pixel in pixels: |
| 74 | + bc = (self._wheel_index + pixel) % len(self._bright_colors) |
| 75 | + self.pixel_object[pixel] = self._bright_colors[bc] |
| 76 | + super().show() |
0 commit comments