From 16ee0c906392abc495b83a95995159d49e9f76e9 Mon Sep 17 00:00:00 2001 From: sqozz Date: Sat, 2 Mar 2024 02:01:13 +0100 Subject: [PATCH] Add handling of different color spaces --- example.py | 9 ++++++--- vu1.py | 46 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/example.py b/example.py index 90ef387..5b98f26 100644 --- a/example.py +++ b/example.py @@ -3,12 +3,15 @@ import vu1 API = vu1.DialServer("http://localhost:5340", "my_secret_api_key") dials = API.dials -# set image and value of specific dial by uid +# set color, image and value of specific dial by uid dial = dials[dials.index("my_dial_uid")] dial.image = "image_pack/gpu-load.png" dial.value = 50 +dial.color = vu1.VUColor(100, 100, 0) # as expected by the API (color between 0-100) +dial.color = vu1.RGBColor(1.0, 1.0, 0.0) # can be used to have colors as float +dial.color = vu1.HSVColor(0.16666666666666669, 1.0, 1.0) # same color as above (yellow) but written in HSV color space - userful for e.g. gradients + # set backlight to white on all available dials for dial in dials: - dial.color = vu1.Color(100, 100, 100) #full white - + dial.color = vu1.VUColor(100, 100, 100) # full white diff --git a/vu1.py b/vu1.py index 276a8ee..6afda7a 100644 --- a/vu1.py +++ b/vu1.py @@ -72,7 +72,7 @@ class Dial(): self._fw_version = s["fw_version"] self._hw_version = s["hw_version"] self._protocol_version = s["protocol_version"] - self._color = Color(*s["backlight"].values()) + self._color = VUColor(*s["backlight"].values()) self._image = s["image_file"] @property @@ -137,15 +137,41 @@ class Dial(): file = {'imgfile': (open(new_image, 'rb'))} self._server._req(f"{self.uid}/image/set", file, post=True) -class Color(): - def __init__(self, red, green, blue): - self.red = max(0, min(100, red)) - self.green = max(0, min(100, green)) - self.blue = max(0, min(100, blue)) - - def hsv(self): - return colorsys.rgb_to_hsv(self.red, self.green, self.blue) +class VUColor(): + def __init__(self, red=0, green=0, blue=0): + self._red = max(0, min(100, red)) + self._green = max(0, min(100, green)) + self._blue = max(0, min(100, blue)) def __iter__(self): for attr in ["red", "green", "blue"]: - yield (attr, self.__dict__[attr]) + yield (attr, self.__dict__[f"_{attr}"]) + + def to_rgb(self): + return RGBColor(self._red/100, self._green/100, self._blue/100) + + def to_hsv(self): + return self.to_rgb().to_hsv() + +class RGBColor(VUColor): + def __init__(self, red=0.0, green=0.0, blue=0.0): + self.red = max(0.0, min(1.0, red)) + self.green = max(0.0, min(1.0, green)) + self.blue = max(0.0, min(1.0, blue)) + super().__init__(int(red*100), int(green*100), int(blue*100)) + + def to_hsv(self): + h, s, v = colorsys.rgb_to_hsv(self.red, self.green, self.blue) + return HSVColor(h, s, v) + +class HSVColor(VUColor): + def __init__(self, hue=0.0, saturation=0.0, value=0.0): + self.hue = max(0.0, min(1.0, hue)) + self.saturation = max(0.0, min(1.0, saturation)) + self.value = max(0.0, min(1.0, value)) + r, g, b = colorsys.hsv_to_rgb(self.hue, self.saturation, self.value) + super().__init__(int(r*100), int(g*100), int(b*100)) + + def to_rgb(self): + r, g, b = colorsys.hsv_to_rgb(self.hue, self.saturation, self.value) + return RGBColor(r, g, b)