From 8673e794e9d7eeb96e7c1afdb6ecaa8482e8b001 Mon Sep 17 00:00:00 2001 From: joBr99 <29555657+joBr99@users.noreply.github.com> Date: Sat, 19 Mar 2022 20:39:11 +0100 Subject: [PATCH] move color stuff to seperate file --- apps/nspanel-lovelace-ui/color.py | 24 ++++++++++++++++ .../nspanel-lovelace-ui.py | 28 +++++-------------- 2 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 apps/nspanel-lovelace-ui/color.py diff --git a/apps/nspanel-lovelace-ui/color.py b/apps/nspanel-lovelace-ui/color.py new file mode 100644 index 00000000..50b7ac73 --- /dev/null +++ b/apps/nspanel-lovelace-ui/color.py @@ -0,0 +1,24 @@ +import colorsys + +def hsv2rgb(self, h, s, v): + hsv = colorsys.hsv_to_rgb(h,s,v) + return tuple(round(i * 255) for i in hsv) +def pos_to_color(self, x, y): + r = 160/2 + x = round((x - r) / r * 100) / 100 + y = round((r - y) / r * 100) / 100 + + r = math.sqrt(x*x + y*y) + sat = 0 + if (r > 1): + sat = 0 + else: + sat = r + hsv = (math.degrees(math.atan2(y, x))%360/360, sat, 1) + rgb = self.hsv2rgb(hsv[0],hsv[1],hsv[2]) + return rgb + +def rgb_dec565(red, green, blue): + # take in the red, green and blue values (0-255) as 8 bit values and then combine + # and shift them to make them a 16 bit dec value in 565 format. + print ((int(red / 255 * 31) << 11) | (int(green / 255 * 63) << 5) | (int(blue / 255 * 31))) \ No newline at end of file diff --git a/apps/nspanel-lovelace-ui/nspanel-lovelace-ui.py b/apps/nspanel-lovelace-ui/nspanel-lovelace-ui.py index 755f9dc3..4f177e18 100644 --- a/apps/nspanel-lovelace-ui/nspanel-lovelace-ui.py +++ b/apps/nspanel-lovelace-ui/nspanel-lovelace-ui.py @@ -2,7 +2,7 @@ import json import datetime import hassapi as hass import math -import colorsys +from color import hsv2rgb,pos_to_color # check Babel import importlib @@ -363,9 +363,13 @@ class NsPanelLovelaceUI: if item_type == "cover": return f",shutter,{item},0,17299,{name}," - if item_type == "light": + if item_type == "light": switch_val = 1 if entity.state == "on" else 0 - return f",{item_type},{item},1,17299,{name},{switch_val}" + icon_color = 17299 + + + + return f",{item_type},{item},1,{icon_color},{name},{switch_val}" if item_type == "switch" or item_type == "input_boolean": switch_val = 1 if entity.state == "on" else 0 @@ -538,21 +542,3 @@ class NsPanelLovelaceUI: # reverse position for slider pos = 100-pos self.send_mqtt_msg(f"entityUpdateDetail,{pos}") - - def hsv2rgb(self, h, s, v): - hsv = colorsys.hsv_to_rgb(h,s,v) - return tuple(round(i * 255) for i in hsv) - def pos_to_color(self, x, y): - r = 160/2 - x = round((x - r) / r * 100) / 100 - y = round((r - y) / r * 100) / 100 - - r = math.sqrt(x*x + y*y) - sat = 0 - if (r > 1): - sat = 0 - else: - sat = r - hsv = (math.degrees(math.atan2(y, x))%360/360, sat, 1) - rgb = self.hsv2rgb(hsv[0],hsv[1],hsv[2]) - return rgb \ No newline at end of file