1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
6 `displayio.colorconverter`
7 ================================================================================
11 **Software and Dependencies:**
14 https://github.com/adafruit/Adafruit_Blinka/releases
16 * Author(s): Melissa LeBlanc-Williams
20 __version__ = "0.0.0-auto.0"
21 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
23 from ._colorspace import Colorspace
27 """Converts one color format to another. Color converter based on original displayio
32 self, *, input_colorspace: Colorspace = Colorspace.RGB888, dither: bool = False
34 """Create a ColorConverter object to convert color formats.
35 Only supports rgb888 to RGB565 currently.
36 :param bool dither: Adds random noise to dither the output image
40 self._transparent_color = None
43 def _compute_rgb565(self, color: int):
45 return (color[0] & 0xF8) << 8 | (color[1] & 0xFC) << 3 | color[2] >> 3
47 def _compute_luma(self, color: int):
49 green = (color >> 8) & 0xFF
51 return (red * 19) / 255 + (green * 182) / 255 + (blue + 54) / 255
53 def _compute_chroma(self, color: int):
55 green = (color >> 8) & 0xFF
57 return max(red, green, blue) - min(red, green, blue)
59 def _compute_hue(self, color: int):
61 green = (color >> 8) & 0xFF
63 max_color = max(red, green, blue)
64 chroma = self._compute_chroma(color)
69 hue = (((green - blue) * 40) / chroma) % 240
70 elif max_color == green:
71 hue = (((blue - red) + (2 * chroma)) * 40) / chroma
72 elif max_color == blue:
73 hue = (((red - green) + (4 * chroma)) * 40) / chroma
79 def _dither_noise_1(self, noise):
80 noise = (noise >> 13) ^ noise
82 noise * (noise * noise * 60493 + 19990303) + 1376312589
84 return (more_noise / (1073741824.0 * 2)) * 255
86 def _dither_noise_2(self, x, y):
87 return self._dither_noise_1(x + y * 0xFFFF)
89 def _compute_tricolor(self):
92 def convert(self, color: int) -> int:
93 "Converts the given rgb888 color to RGB565"
94 if isinstance(color, int):
95 color = ((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, 255)
96 elif isinstance(color, tuple):
98 color = (color[0], color[1], color[2], 255)
100 raise ValueError("Color must be a 3 or 4 value tuple")
102 raise ValueError("Color must be an integer or 3 or 4 value tuple")
105 return color # To Do: return a dithered color
108 return self._compute_rgb565(color)
110 def make_transparent(self, color: int) -> None:
111 """Set the transparent color or index for the ColorConverter. This will
112 raise an Exception if there is already a selected transparent index.
114 self._transparent_color = color
116 def make_opaque(self, color: int) -> None:
117 """Make the ColorConverter be opaque and have no transparent pixels."""
118 self._transparent_color = None
121 def dither(self) -> bool:
122 """When true the color converter dithers the output by adding
123 random noise when truncating to display bitdepth
128 def dither(self, value: bool):
129 if not isinstance(value, bool):
130 raise ValueError("Value should be boolean")