]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blobdiff - displayio/_colorconverter.py
convert tuple to hex color first
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / _colorconverter.py
index d97eae015c69e454488117f65160ef82c408f043..7fd12bb998bdea9e15788f46c72be5738f41afae 100644 (file)
@@ -85,7 +85,7 @@ class ColorConverter:
         red8 = color_rgb888 >> 16
         grn8 = (color_rgb888 >> 8) & 0xFF
         blu8 = color_rgb888 & 0xFF
-        return (red8 * 19 + grn8 * 182 + blu8 + 54) // 255
+        return (red8 * 19 + grn8 * 182 + blu8 * 54) // 255
 
     @staticmethod
     def _compute_chroma(color_rgb888: int):
@@ -144,9 +144,7 @@ class ColorConverter:
         return 0x0  # Black
 
     @staticmethod
-    def _compute_tricolor(
-        colorspace: ColorspaceStruct, pixel_hue: int, color: int
-    ) -> int:
+    def _compute_tricolor(colorspace: ColorspaceStruct, pixel_hue: int) -> int:
         hue_diff = colorspace.tricolor_hue - pixel_hue
         if -10 <= hue_diff <= 10 or hue_diff <= -220 or hue_diff >= 220:
             if colorspace.grayscale:
@@ -169,7 +167,7 @@ class ColorConverter:
         else:
             raise ValueError("Color must be an integer or 3 or 4 value tuple")
 
-        input_pixel = InputPixelStruct(color)
+        input_pixel = InputPixelStruct(pixel=color)
         output_pixel = OutputPixelStruct()
 
         self._convert(self._output_colorspace, input_pixel, output_pixel)
@@ -193,7 +191,7 @@ class ColorConverter:
             and self._cached_colorspace == colorspace
             and self._cached_input_pixel == input_pixel.pixel
         ):
-            output_color = self._cached_output_color
+            output_color.pixel = self._cached_output_color
             return
 
         rgb888_pixel = input_pixel
@@ -207,8 +205,15 @@ class ColorConverter:
             self._cached_input_pixel = input_pixel.pixel
             self._cached_output_color = output_color.pixel
 
+    @staticmethod
+    def _rgbtuple_to_hex(color_tuple):
+        """Convert rgb tuple with 0-255 values to hex color value"""
+        return color_tuple[0] << 16 | color_tuple[1] << 8 | color_tuple[2]
+
     @staticmethod
     def _convert_pixel(colorspace: Colorspace, pixel: int) -> int:
+        if isinstance(pixel, tuple):
+            pixel = ColorConverter._rgbtuple_to_hex(pixel)
         pixel = clamp(pixel, 0, 0xFFFFFFFF)
         if colorspace in (
             Colorspace.RGB565_SWAPPED,
@@ -251,9 +256,15 @@ class ColorConverter:
         # pylint: disable=too-many-return-statements, too-many-branches, too-many-statements
         pixel = input_pixel.pixel
         if dither:
-            rand_red = ColorConverter._dither_noise_2(input_pixel.x, input_pixel.y)
-            rand_grn = ColorConverter._dither_noise_2(input_pixel.x + 33, input_pixel.y)
-            rand_blu = ColorConverter._dither_noise_2(input_pixel.x, input_pixel.y + 33)
+            rand_red = ColorConverter._dither_noise_2(
+                input_pixel.tile_x, input_pixel.tile_y
+            )
+            rand_grn = ColorConverter._dither_noise_2(
+                input_pixel.tile_x + 33, input_pixel.tile_y
+            )
+            rand_blu = ColorConverter._dither_noise_2(
+                input_pixel.tile_x, input_pixel.tile_y + 33
+            )
 
             red8 = pixel >> 16
             grn8 = (pixel >> 8) & 0xFF
@@ -287,9 +298,7 @@ class ColorConverter:
                 output_color.opaque = True
                 return
             pixel_hue = ColorConverter._compute_hue(pixel)
-            output_color.pixel = ColorConverter._compute_tricolor(
-                colorspace, pixel_hue, output_color.pixel
-            )
+            output_color.pixel = ColorConverter._compute_tricolor(colorspace, pixel_hue)
             return
         if colorspace.grayscale and colorspace.depth <= 8:
             bitmask = (1 << colorspace.depth) - 1
@@ -327,6 +336,9 @@ class ColorConverter:
         """Make the ColorConverter be opaque and have no transparent pixels."""
         self._transparent_color = None
 
+    def _finish_refresh(self) -> None:
+        pass
+
     @property
     def dither(self) -> bool:
         """When true the color converter dithers the output by adding