diff options
Diffstat (limited to 'NeoRuntime/Runtime')
-rw-r--r-- | NeoRuntime/Runtime/luxcena_neo/color_utils.py | 65 | ||||
-rw-r--r-- | NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py | 2 |
2 files changed, 64 insertions, 3 deletions
diff --git a/NeoRuntime/Runtime/luxcena_neo/color_utils.py b/NeoRuntime/Runtime/luxcena_neo/color_utils.py index a17d8e4..ab29092 100644 --- a/NeoRuntime/Runtime/luxcena_neo/color_utils.py +++ b/NeoRuntime/Runtime/luxcena_neo/color_utils.py @@ -8,7 +8,7 @@ def rgb_to_hex(rgb: tuple) -> str: """ Convert rgb color to hex string. """ return '#%02x%02x%02x' % rgb -def rgb_from_24bit(color: int) -> tuple: +def rgb_from_twentyfour_bit(color: int) -> tuple: """ Convert 24-bit color value into a tuple representing the rgb values. """ # w = (color & 0xFF000000) >> 24 r = (color & 0x00FF0000) >> 16 @@ -16,6 +16,67 @@ def rgb_from_24bit(color: int) -> tuple: b = (color & 0x000000FF) return (r, g, b) -def hex_from_24bit(color: int) -> str: +def hex_from_twentyfour_bit(color: int) -> str: """ Convert 24-bit color value into hex str. """ rgb_to_hex(rgb_from_24bit(color)) + +def twentyfour_bit_from_rgb(red, green, blue, white=0): + """ + Convert the provided red, green, blue color to a 24-bit color value. + Each color component should be a value 0-255 + where 0 is the lowest intensity and 255 is the highest intensity. + """ + return (white << 24) | (red << 16) | (green << 8) | blue + + +def twentyfour_bit_from_hex(hex_color: str): + """ Convert the provided hex code to a 24-bit color value. """ + value = hex_color.lstrip('#') + lv = len(value) + rgb = tuple(int(value[i:i+lv//3], 16) for i in range(0, lv, lv//3)) + return twentyfour_bit_from_rgb(red=rgb[1], green=rgb[0], blue=rgb[2]) + + +def detect_format_convert_color(*color) -> int: + """ + Detect format of a color and return its 24-bit color value. + + If parameter is only a str, it will be treated as a hex value. + If parameter is a tuple, the first three items in that tuple will be treated as a rgb value. + If parameter is a int, it will be treated as a 24-bit color value. + If there are 3 parameters, these will be treated as a rgb value. + """ + if (len(color) == 1) and (isinstance(color[0], str)): + return twentyfour_bit_from_hex(color[0]) + if (len(color) == 1) and (isinstance(color[0], tuple)): + return twentyfour_bit_from_rgb(*(color[0])) + if (len(color) == 1) and (isinstance(color[0], int)): + return color[0] + if (len(color) == 3): + return twentyfour_bit_from_rgb(*color) + raise ValueError("Invalid parameters provided, check documentation.") + + +class Color: + + def __init__(self, *color): + self.__color = detect_format_convert_color(*color) + + @property + def hex(self): return hex_from_twentyfour_bit(self.__color) + + @property + def rgb(self): return rgb_from_twentyfour_bit(self.__color) + + def __repr__(self): + return self.__color + + def __str__(self): + return str(repr(self)) + + def __int__(self): + return self.__color + + def __invert__(self): + rgb_color = self.rgb + return Color((255-rgb_color[0], 255-rgb_color[1], 255-rgb_color[2]))
\ No newline at end of file diff --git a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py index dc4609c..5c89ca0 100644 --- a/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py +++ b/NeoRuntime/Runtime/luxcena_neo/neo_behaviour.py @@ -2,7 +2,7 @@ import json from os import path from enum import Enum from .strip import detect_format_convert_color -from .color_utils import rgb_from_24bit, hex_from_24bit +from .color_utils import rgb_from_twentyfour_bit, hex_from_twentyfour_bit class NeoBehaviour: """ |