-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from k0nze/master
Added RGB to XY conversion example
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |