]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/colorconverter.py
f853653d082834fbcb20331e36d1ec99bcc7ad80
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / colorconverter.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.colorconverter`
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 ColorConverter:
43     """Converts one color format to another. Color converter based on original displayio
44     code for consistency.
45     """
46
47     def __init__(self, *, dither=False):
48         """Create a ColorConverter object to convert color formats.
49         Only supports rgb888 to RGB565 currently.
50         :param bool dither: Adds random noise to dither the output image
51         """
52         self._dither = dither
53         self._depth = 16
54         self._rgba = False
55
56     # pylint: disable=no-self-use
57     def _compute_rgb565(self, color):
58         self._depth = 16
59         return (color[0] & 0xF8) << 8 | (color[1] & 0xFC) << 3 | color[2] >> 3
60
61     def _compute_luma(self, color):
62         red = color >> 16
63         green = (color >> 8) & 0xFF
64         blue = color & 0xFF
65         return (red * 19) / 255 + (green * 182) / 255 + (blue + 54) / 255
66
67     def _compute_chroma(self, color):
68         red = color >> 16
69         green = (color >> 8) & 0xFF
70         blue = color & 0xFF
71         return max(red, green, blue) - min(red, green, blue)
72
73     def _compute_hue(self, color):
74         red = color >> 16
75         green = (color >> 8) & 0xFF
76         blue = color & 0xFF
77         max_color = max(red, green, blue)
78         chroma = self._compute_chroma(color)
79         if chroma == 0:
80             return 0
81         hue = 0
82         if max_color == red:
83             hue = (((green - blue) * 40) / chroma) % 240
84         elif max_color == green:
85             hue = (((blue - red) + (2 * chroma)) * 40) / chroma
86         elif max_color == blue:
87             hue = (((red - green) + (4 * chroma)) * 40) / chroma
88         if hue < 0:
89             hue += 240
90
91         return hue
92
93     def _dither_noise_1(self, noise):
94         noise = (noise >> 13) ^ noise
95         more_noise = (
96             noise * (noise * noise * 60493 + 19990303) + 1376312589
97         ) & 0x7FFFFFFF
98         return (more_noise / (1073741824.0 * 2)) * 255
99
100     def _dither_noise_2(self, x, y):
101         return self._dither_noise_1(x + y * 0xFFFF)
102
103     def _compute_tricolor(self):
104         pass
105
106     def convert(self, color):
107         "Converts the given rgb888 color to RGB565"
108         if isinstance(color, int):
109             color = ((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, 255)
110         elif isinstance(color, tuple):
111             if len(color) == 3:
112                 color = (color[0], color[1], color[2], 255)
113             elif len(color) != 4:
114                 raise ValueError("Color must be a 3 or 4 value tuple")
115         else:
116             raise ValueError("Color must be an integer or 3 or 4 value tuple")
117
118         if self._dither:
119             return color  # To Do: return a dithered color
120         if self._rgba:
121             return color
122         return self._compute_rgb565(color)
123
124     # pylint: enable=no-self-use
125
126     @property
127     def dither(self):
128         """When true the color converter dithers the output by adding
129         random noise when truncating to display bitdepth
130         """
131         return self._dither
132
133     @dither.setter
134     def dither(self, value):
135         if not isinstance(value, bool):
136             raise ValueError("Value should be boolean")
137         self._dither = value