]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/rainbowio.py
fix generic except
[Adafruit_Blinka-hackapet.git] / src / rainbowio.py
1 """
2 `rainbowio` - Provides the `colorwheel()` function
3 ===========================================================
4 See `CircuitPython:rainbowio` in CircuitPython for more details.
5 Not supported by all boards.
6
7 * Author(s): Kattni Rembor, Carter Nelson
8 """
9
10
11 def colorwheel(color_value):
12     """
13     A colorwheel. ``0`` and ``255`` are red, ``85`` is green, and ``170`` is blue, with the values
14     between being the rest of the rainbow.
15
16     :param int color_value: 0-255 of color value to return
17     :return: tuple of RGB values
18     """
19     color_value = int(color_value)
20     if color_value < 0 or color_value > 255:
21         r = 0
22         g = 0
23         b = 0
24     elif color_value < 85:
25         r = int(255 - color_value * 3)
26         g = int(color_value * 3)
27         b = 0
28     elif color_value < 170:
29         color_value -= 85
30         r = 0
31         g = int(255 - color_value * 3)
32         b = int(color_value * 3)
33     else:
34         color_value -= 170
35         r = int(color_value * 3)
36         g = 0
37         b = int(255 - color_value * 3)
38     return r << 16 | g << 8 | b