Skip to content

Commit

Permalink
Merge pull request #173 from k0nze/master
Browse files Browse the repository at this point in the history
Added RGB to XY conversion example
  • Loading branch information
natcl authored Jul 17, 2020
2 parents e44f61c + 3e146fa commit 6882829
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions examples/rgb_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/python
from phue import Bridge

def rgb_to_xy(red, green, blue):
""" conversion of RGB colors to CIE1931 XY colors
Formulas implemented from: https://gist.github.com/popcorn245/30afa0f98eea1c2fd34d
Args:
red (float): a number between 0.0 and 1.0 representing red in the RGB space
green (float): a number between 0.0 and 1.0 representing green in the RGB space
blue (float): a number between 0.0 and 1.0 representing blue in the RGB space
Returns:
xy (list): x and y
"""

# gamma correction
red = pow((red + 0.055) / (1.0 + 0.055), 2.4) if red > 0.04045 else (red / 12.92)
green = pow((green + 0.055) / (1.0 + 0.055), 2.4) if green > 0.04045 else (green / 12.92)
blue = pow((blue + 0.055) / (1.0 + 0.055), 2.4) if blue > 0.04045 else (blue / 12.92)

# convert rgb to xyz
x = red * 0.649926 + green * 0.103455 + blue * 0.197109
y = red * 0.234327 + green * 0.743075 + blue * 0.022598
z = green * 0.053077 + blue * 1.035763

# convert xyz to xy
x = x / (x + y + z)
y = y / (x + y + z)

# TODO check color gamut if known

return [x, y]


b = Bridge() # Enter bridge IP here.

#If running for the first time, press button on bridge and run with b.connect() uncommented
#b.connect()

# RGB colors to XY
xy = rgb_to_xy(1.0, 0.28627, 0.95686)

lights = b.get_light_objects()

for light in lights:
# y might be used as brightness value, however, dark colors will turn the lights off
#brightness = int(xy[1]*255)
brightness = 255
light.xy = xy

0 comments on commit 6882829

Please sign in to comment.