1 # The MIT License (MIT)
3 # Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
25 ================================================================================
29 **Software and Dependencies:**
32 https://github.com/adafruit/Adafruit_Blinka/releases
34 * Author(s): Melissa LeBlanc-Williams
38 __version__ = "0.0.0-auto.0"
39 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
43 """Map a pixel palette_index to a full color. Colors are transformed to the display’s
44 format internally to save memory.
47 def __init__(self, color_count):
48 """Create a Palette object to store a set number of colors."""
49 self._needs_refresh = False
52 for _ in range(color_count):
53 self._colors.append(self._make_color(0))
55 def _make_color(self, value, transparent=False):
57 "transparent": transparent,
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")
66 raise TypeError("Color buffer must be a buffer, tuple, list, or int")
67 color["rgb888"] = value
68 self._needs_refresh = True
73 """Returns the number of colors in a Palette"""
74 return len(self._colors)
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.
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.
84 if self._colors[index]["rgb888"] != value:
85 self._colors[index] = self._make_color(value)
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]
92 def make_transparent(self, palette_index):
93 """Set the palette index to be a transparent color"""
94 self._colors[palette_index]["transparent"] = True
96 def make_opaque(self, palette_index):
97 """Set the palette index to be an opaque color"""
98 self._colors[palette_index]["transparent"] = False