1 # The MIT License (MIT)
 
   3 # Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
 
   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:
 
  12 # The above copyright notice and this permission notice shall be included in
 
  13 # all copies or substantial portions of the Software.
 
  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
 
  24 `displayio.colorconverter`
 
  25 ================================================================================
 
  29 **Software and Dependencies:**
 
  32   https://github.com/adafruit/Adafruit_Blinka/releases
 
  34 * Author(s): Melissa LeBlanc-Williams
 
  38 __version__ = "0.0.0-auto.0"
 
  39 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
 
  43     """Converts one color format to another. Color converter based on original displayio
 
  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
 
  56     # pylint: disable=no-self-use
 
  57     def _compute_rgb565(self, color):
 
  59         return (color[0] & 0xF8) << 8 | (color[1] & 0xFC) << 3 | color[2] >> 3
 
  61     def _compute_luma(self, color):
 
  63         green = (color >> 8) & 0xFF
 
  65         return (red * 19) / 255 + (green * 182) / 255 + (blue + 54) / 255
 
  67     def _compute_chroma(self, color):
 
  69         green = (color >> 8) & 0xFF
 
  71         return max(red, green, blue) - min(red, green, blue)
 
  73     def _compute_hue(self, color):
 
  75         green = (color >> 8) & 0xFF
 
  77         max_color = max(red, green, blue)
 
  78         chroma = self._compute_chroma(color)
 
  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
 
  93     def _dither_noise_1(self, noise):
 
  94         noise = (noise >> 13) ^ noise
 
  96             noise * (noise * noise * 60493 + 19990303) + 1376312589
 
  98         return (more_noise / (1073741824.0 * 2)) * 255
 
 100     def _dither_noise_2(self, x, y):
 
 101         return self._dither_noise_1(x + y * 0xFFFF)
 
 103     def _compute_tricolor(self):
 
 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):
 
 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")
 
 116             raise ValueError("Color must be an integer or 3 or 4 value tuple")
 
 119             return color  # To Do: return a dithered color
 
 122         return self._compute_rgb565(color)
 
 124     # pylint: enable=no-self-use
 
 128         """When true the color converter dithers the output by adding
 
 129         random noise when truncating to display bitdepth
 
 134     def dither(self, value):
 
 135         if not isinstance(value, bool):
 
 136             raise ValueError("Value should be boolean")