1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
7 ================================================================================
11 **Software and Dependencies:**
14 https://github.com/adafruit/Adafruit_Blinka/releases
16 * Author(s): Melissa LeBlanc-Williams
20 from typing import Optional, Union, Tuple
21 from circuitpython_typing import ReadableBuffer
22 from ._colorconverter import ColorConverter
23 from ._colorspace import Colorspace
24 from ._structs import InputPixelStruct, OutputPixelStruct, ColorStruct
26 __version__ = "0.0.0+auto.0"
27 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
31 """Map a pixel palette_index to a full color. Colors are transformed to the display's
32 format internally to save memory.
35 def __init__(self, color_count: int, *, dither: bool = False):
36 """Create a Palette object to store a set number of colors.
38 :param int color_count: The number of colors in the Palette
39 :param bool dither: When true, dither the RGB color before converting to the
42 self._needs_refresh = False
46 for _ in range(color_count):
47 self._colors.append(ColorStruct())
50 def _color_to_int(value):
51 if isinstance(value, (tuple, list, bytes, bytearray)):
52 value = (value[0] & 0xFF) << 16 | (value[1] & 0xFF) << 8 | value[2] & 0xFF
53 elif isinstance(value, int):
54 if not 0 <= value <= 0xFFFFFF:
55 raise ValueError("Color must be between 0x000000 and 0xFFFFFF")
57 raise TypeError("Color buffer must be a buffer, tuple, list, or int")
60 def __len__(self) -> int:
61 """Returns the number of colors in a Palette"""
62 return len(self._colors)
67 value: Union[int, ReadableBuffer, Tuple[int, int, int]],
69 """Sets the pixel color at the given index. The index should be
70 an integer in the range 0 to color_count-1.
72 The value argument represents a color, and can be from 0x000000 to 0xFFFFFF
73 (to represent an RGB value). Value can be an int, bytes (3 bytes (RGB) or
74 4 bytes (RGB + pad byte)), bytearray, or a tuple or list of 3 integers.
76 self._set_color(index, self._color_to_int(value))
78 def _set_color(self, palette_index: int, color: int):
79 if self._colors[palette_index].rgb888 == color:
81 self._colors[palette_index].rgb888 = color
82 self._colors[palette_index].cached_colorspace = None
83 self._needs_refresh = True
85 def __getitem__(self, index: int) -> Optional[int]:
86 if not 0 <= index < len(self._colors):
87 raise ValueError("Palette index out of range")
88 return self._colors[index].rgb888
90 def make_transparent(self, palette_index: int) -> None:
91 """Set the palette index to be a transparent color"""
92 self._colors[palette_index].transparent = True
93 self._needs_refresh = True
95 def make_opaque(self, palette_index: int) -> None:
96 """Set the palette index to be an opaque color"""
97 self._colors[palette_index].transparent = False
98 self._needs_refresh = True
102 colorspace: Colorspace,
103 input_pixel: InputPixelStruct,
104 output_color: OutputPixelStruct,
106 palette_index = input_pixel.pixel
107 if palette_index > len(self._colors) or self._colors[palette_index].transparent:
108 output_color.opaque = False
111 color = self._colors[palette_index]
114 and color.cached_colorspace == colorspace
115 and color.cached_colorspace_grayscale_bit == colorspace.grayscale_bit
116 and color.cached_colorspace_grayscale == colorspace.grayscale
118 output_color.pixel = self._colors[palette_index].cached_color
121 rgb888_pixel = input_pixel
122 rgb888_pixel.pixel = self._colors[palette_index].rgb888
123 ColorConverter._convert_color( # pylint: disable=protected-access
124 colorspace, self._dither, rgb888_pixel, output_color
127 color.cached_colorspace = colorspace
128 color.cached_color = output_color.pixel
129 color.cached_colorspace_grayscale = colorspace.grayscale
130 color.cached_colorspace_grayscale_bit = colorspace.grayscale_bit
132 def is_transparent(self, palette_index: int) -> bool:
133 """Returns True if the palette index is transparent. Returns False if opaque."""
134 return self._colors[palette_index].transparent
136 def _finish_refresh(self):
137 self._needs_refresh = False
140 def dither(self) -> bool:
141 """When true the palette dithers the output by adding
142 random noise when truncating to display bitdepth
147 def dither(self, value: bool):
148 if not isinstance(value, bool):
149 raise ValueError("Value should be boolean")