]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blobdiff - displayio/colorconverter.py
Add pre-commit support.
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / colorconverter.py
index 3c2041f8fb99a237c6f4901552937fe4dc262284..8dc0e7ab4e0dbe18fb925a93e781b4c80ec32f71 100644 (file)
@@ -1,27 +1,9 @@
-# The MIT License (MIT)
+# SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
 #
-# Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
+# SPDX-License-Identifier: MIT
 
 """
-`displayio`
+`displayio.colorconverter`
 ================================================================================
 
 displayio for Blinka
@@ -51,11 +33,12 @@ class ColorConverter:
         """
         self._dither = dither
         self._depth = 16
+        self._rgba = False
 
     # pylint: disable=no-self-use
     def _compute_rgb565(self, color):
         self._depth = 16
-        return (color >> 19) << 11 | ((color >> 10) & 0x3F) << 5 | (color >> 3) & 0x1F
+        return (color[0] & 0xF8) << 8 | (color[1] & 0xFC) << 3 | color[2] >> 3
 
     def _compute_luma(self, color):
         red = color >> 16
@@ -104,8 +87,20 @@ class ColorConverter:
 
     def convert(self, color):
         "Converts the given rgb888 color to RGB565"
+        if isinstance(color, int):
+            color = ((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, 255)
+        elif isinstance(color, tuple):
+            if len(color) == 3:
+                color = (color[0], color[1], color[2], 255)
+            elif len(color) != 4:
+                raise ValueError("Color must be a 3 or 4 value tuple")
+        else:
+            raise ValueError("Color must be an integer or 3 or 4 value tuple")
+
         if self._dither:
             return color  # To Do: return a dithered color
+        if self._rgba:
+            return color
         return self._compute_rgb565(color)
 
     # pylint: enable=no-self-use