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
 
  55     # pylint: disable=no-self-use
 
  56     def _compute_rgb565(self, color):
 
  58         return (color >> 19) << 11 | ((color >> 10) & 0x3F) << 5 | (color >> 3) & 0x1F
 
  60     def _compute_luma(self, color):
 
  62         green = (color >> 8) & 0xFF
 
  64         return (red * 19) / 255 + (green * 182) / 255 + (blue + 54) / 255
 
  66     def _compute_chroma(self, color):
 
  68         green = (color >> 8) & 0xFF
 
  70         return max(red, green, blue) - min(red, green, blue)
 
  72     def _compute_hue(self, color):
 
  74         green = (color >> 8) & 0xFF
 
  76         max_color = max(red, green, blue)
 
  77         chroma = self._compute_chroma(color)
 
  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
 
  92     def _dither_noise_1(self, noise):
 
  93         noise = (noise >> 13) ^ noise
 
  95             noise * (noise * noise * 60493 + 19990303) + 1376312589
 
  97         return (more_noise / (1073741824.0 * 2)) * 255
 
  99     def _dither_noise_2(self, x, y):
 
 100         return self._dither_noise_1(x + y * 0xFFFF)
 
 102     def _compute_tricolor(self):
 
 105     def convert(self, color):
 
 106         "Converts the given rgb888 color to RGB565"
 
 108             return color  # To Do: return a dithered color
 
 109         return self._compute_rgb565(color)
 
 111     # pylint: enable=no-self-use
 
 115         """When true the color converter dithers the output by adding
 
 116         random noise when truncating to display bitdepth
 
 121     def dither(self, value):
 
 122         if not isinstance(value, bool):
 
 123             raise ValueError("Value should be boolean")