En3rGy / 14100_Hue

Gira HS logic module to control Philips Hue
MIT License
7 stars 0 forks source link

Color not correct #27

Open En3rGy opened 1 year ago

En3rGy commented 1 year ago

RGB to xy conversion seems buggy. rgb = (0, 255, 0) results in some kind of yellow

En3rGy commented 8 months ago

Potential solution:

def setHsv(self, data):
  import json
  hex = self.decimal_to_hex(int(data)).zfill(6)
  xy = self.hex_to_xy(hex)
  rgb = self.color.hex_to_rgb(hex)
  hsv = self.rgb2hsv(rgb[0], rgb[1], rgb[2])
  hue = int(182 * hsv[0])
  sat = int(round(2.54 * hsv[1]))
  bri = int(round(2.54 * hsv[2]))
  payload = json.dumps({ "hue" : hue, "sat": sat, "bri" : bri, "xy" : xy })
  url = "{0}/{1}".format(self.url, self.mode)
  ret = self.doRequest(url, "PUT", payload)

def decimal_to_hex(self, decNum):
  digits = "0123456789ABCDEF"
  x = (decNum % 16)
  rest_part = decNum // 16
  if (rest_part == 0):
    return digits[x]
  return

self.decimal_to_hex(rest_part) + digits[x]
def rgb2hsv(self, r, g, b):
  r, g, b = r/255.0, g/255.0, b/255.0
  mx = max(r, g, b)
  mn = min(r, g, b)
  df = mx-mn
  if mx == mn:
    h = 0
  elif mx == r:
    h = (60 * ((g-b)/df) + 360) % 360
  elif mx == g:
    h = (60 * ((b-r)/df) + 120) % 360
  elif mx == b:
    h = (60 * ((r-g)/df) + 240) % 360
  if mx == 0:
    s = 0
  else:
    s = round((df/mx) * 100, 1)
  v = mx * 100
  return round(h), s, v​  

and for

def get_rgb_from_xy_and_brightness(...)
  ...
  r = X * 3.2406 - Y * 1.5372 - Z * 0.4986;
  g = -X * 0.9689 + Y * 1.8758 + Z * 0.0415;
  b = X * 0.0557 - Y * 0.2040 + Z * 1.0570;​  
  ...