]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/_colorconverter.py
Added typing and missing CP7 functions
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / _colorconverter.py
1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 `displayio.colorconverter`
7 ================================================================================
8
9 displayio for Blinka
10
11 **Software and Dependencies:**
12
13 * Adafruit Blinka:
14   https://github.com/adafruit/Adafruit_Blinka/releases
15
16 * Author(s): Melissa LeBlanc-Williams
17
18 """
19
20 __version__ = "0.0.0-auto.0"
21 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
22
23 from ._colorspace import Colorspace
24
25
26 class ColorConverter:
27     """Converts one color format to another. Color converter based on original displayio
28     code for consistency.
29     """
30
31     def __init__(
32         self, *, input_colorspace: Colorspace = Colorspace.RGB888, dither: bool = False
33     ):
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
37         """
38         self._dither = dither
39         self._depth = 16
40         self._transparent_color = None
41         self._rgba = False
42
43     def _compute_rgb565(self, color: int):
44         self._depth = 16
45         return (color[0] & 0xF8) << 8 | (color[1] & 0xFC) << 3 | color[2] >> 3
46
47     def _compute_luma(self, color: int):
48         red = color >> 16
49         green = (color >> 8) & 0xFF
50         blue = color & 0xFF
51         return (red * 19) / 255 + (green * 182) / 255 + (blue + 54) / 255
52
53     def _compute_chroma(self, color: int):
54         red = color >> 16
55         green = (color >> 8) & 0xFF
56         blue = color & 0xFF
57         return max(red, green, blue) - min(red, green, blue)
58
59     def _compute_hue(self, color: int):
60         red = color >> 16
61         green = (color >> 8) & 0xFF
62         blue = color & 0xFF
63         max_color = max(red, green, blue)
64         chroma = self._compute_chroma(color)
65         if chroma == 0:
66             return 0
67         hue = 0
68         if max_color == red:
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
74         if hue < 0:
75             hue += 240
76
77         return hue
78
79     def _dither_noise_1(self, noise):
80         noise = (noise >> 13) ^ noise
81         more_noise = (
82             noise * (noise * noise * 60493 + 19990303) + 1376312589
83         ) & 0x7FFFFFFF
84         return (more_noise / (1073741824.0 * 2)) * 255
85
86     def _dither_noise_2(self, x, y):
87         return self._dither_noise_1(x + y * 0xFFFF)
88
89     def _compute_tricolor(self):
90         pass
91
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):
97             if len(color) == 3:
98                 color = (color[0], color[1], color[2], 255)
99             elif len(color) != 4:
100                 raise ValueError("Color must be a 3 or 4 value tuple")
101         else:
102             raise ValueError("Color must be an integer or 3 or 4 value tuple")
103
104         if self._dither:
105             return color  # To Do: return a dithered color
106         if self._rgba:
107             return color
108         return self._compute_rgb565(color)
109
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.
113         """
114         self._transparent_color = color
115
116     def make_opaque(self, color: int) -> None:
117         """Make the ColorConverter be opaque and have no transparent pixels."""
118         self._transparent_color = None
119
120     @property
121     def dither(self) -> bool:
122         """When true the color converter dithers the output by adding
123         random noise when truncating to display bitdepth
124         """
125         return self._dither
126
127     @dither.setter
128     def dither(self, value: bool):
129         if not isinstance(value, bool):
130             raise ValueError("Value should be boolean")
131         self._dither = value