]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/colorconverter.py
Fixed docs errors
[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
55     # pylint: disable=no-self-use
56     def _compute_rgb565(self, color):
57         self._depth = 16
58         return (color >> 19) << 11 | ((color >> 10) & 0x3F) << 5 | (color >> 3) & 0x1F
59
60     def _compute_luma(self, color):
61         red = color >> 16
62         green = (color >> 8) & 0xFF
63         blue = color & 0xFF
64         return (red * 19) / 255 + (green * 182) / 255 + (blue + 54) / 255
65
66     def _compute_chroma(self, color):
67         red = color >> 16
68         green = (color >> 8) & 0xFF
69         blue = color & 0xFF
70         return max(red, green, blue) - min(red, green, blue)
71
72     def _compute_hue(self, color):
73         red = color >> 16
74         green = (color >> 8) & 0xFF
75         blue = color & 0xFF
76         max_color = max(red, green, blue)
77         chroma = self._compute_chroma(color)
78         if chroma == 0:
79             return 0
80         hue = 0
81         if max_color == red:
82             hue = (((green - blue) * 40) / chroma) % 240
83         elif max_color == green:
84             hue = (((blue - red) + (2 * chroma)) * 40) / chroma
85         elif max_color == blue:
86             hue = (((red - green) + (4 * chroma)) * 40) / chroma
87         if hue < 0:
88             hue += 240
89
90         return hue
91
92     def _dither_noise_1(self, noise):
93         noise = (noise >> 13) ^ noise
94         more_noise = (
95             noise * (noise * noise * 60493 + 19990303) + 1376312589
96         ) & 0x7FFFFFFF
97         return (more_noise / (1073741824.0 * 2)) * 255
98
99     def _dither_noise_2(self, x, y):
100         return self._dither_noise_1(x + y * 0xFFFF)
101
102     def _compute_tricolor(self):
103         pass
104
105     def convert(self, color):
106         "Converts the given rgb888 color to RGB565"
107         if self._dither:
108             return color  # To Do: return a dithered color
109         return self._compute_rgb565(color)
110
111     # pylint: enable=no-self-use
112
113     @property
114     def dither(self):
115         """When true the color converter dithers the output by adding
116         random noise when truncating to display bitdepth
117         """
118         return self._dither
119
120     @dither.setter
121     def dither(self, value):
122         if not isinstance(value, bool):
123             raise ValueError("Value should be boolean")
124         self._dither = value