Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color not correct #27

Open
En3rGy opened this issue Mar 18, 2023 · 1 comment
Open

Color not correct #27

En3rGy opened this issue Mar 18, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@En3rGy
Copy link
Owner

En3rGy commented Mar 18, 2023

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

@En3rGy En3rGy self-assigned this Mar 27, 2023
En3rGy added a commit that referenced this issue Mar 27, 2023
@En3rGy En3rGy added the bug Something isn't working label Nov 1, 2023
@En3rGy
Copy link
Owner Author

En3rGy commented Mar 12, 2024

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;​  
  ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant