]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/palette.py
4fe63a2edfcc95fcd61611de517230b7d1bce86b
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / palette.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22
23 """
24 `displayio.palette`
25 ================================================================================
26
27 displayio for Blinka
28
29 **Software and Dependencies:**
30
31 * Adafruit Blinka:
32   https://github.com/adafruit/Adafruit_Blinka/releases
33
34 * Author(s): Melissa LeBlanc-Williams
35
36 """
37
38 __version__ = "0.0.0-auto.0"
39 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
40
41
42 class Palette:
43     """Map a pixel palette_index to a full color. Colors are transformed to the display’s
44     format internally to save memory.
45     """
46
47     def __init__(self, color_count):
48         """Create a Palette object to store a set number of colors."""
49         self._needs_refresh = False
50
51         self._colors = []
52         for _ in range(color_count):
53             self._colors.append(self._make_color(0))
54
55     def _make_color(self, value, transparent=False):
56         color = {
57             "transparent": transparent,
58             "rgb888": 0,
59         }
60         if isinstance(value, (tuple, list, bytes, bytearray)):
61             value = (value[0] & 0xFF) << 16 | (value[1] & 0xFF) << 8 | value[2] & 0xFF
62         elif isinstance(value, int):
63             if not 0 <= value <= 0xFFFFFF:
64                 raise ValueError("Color must be between 0x000000 and 0xFFFFFF")
65         else:
66             raise TypeError("Color buffer must be a buffer, tuple, list, or int")
67         color["rgb888"] = value
68         self._needs_refresh = True
69
70         return color
71
72     def __len__(self):
73         """Returns the number of colors in a Palette"""
74         return len(self._colors)
75
76     def __setitem__(self, index, value):
77         """Sets the pixel color at the given index. The index should be
78         an integer in the range 0 to color_count-1.
79
80         The value argument represents a color, and can be from 0x000000 to 0xFFFFFF
81         (to represent an RGB value). Value can be an int, bytes (3 bytes (RGB) or
82         4 bytes (RGB + pad byte)), bytearray, or a tuple or list of 3 integers.
83         """
84         if self._colors[index]["rgb888"] != value:
85             self._colors[index] = self._make_color(value)
86
87     def __getitem__(self, index):
88         if not 0 <= index < len(self._colors):
89             raise ValueError("Palette index out of range")
90         return self._colors[index]
91
92     def make_transparent(self, palette_index):
93         """Set the palette index to be a transparent color"""
94         self._colors[palette_index]["transparent"] = True
95
96     def make_opaque(self, palette_index):
97         """Set the palette index to be an opaque color"""
98         self._colors[palette_index]["transparent"] = False