How do I convert an RGB value to a XY value for the Phillips Hue Bulb
Asked Answered
S

1

4

How do I properly convert the pixel rgb values in a picture I have taken to th XY values needed to to send to the Phillips Hue device? My current code does the following: 1:Take a picture, find the most common colors. 2:Cycle throw them and then take another picture. 3:Send the value to a Phillips Hue Bulbs. If you need more of the code, do tell, I can share everything if it helps. I am particularly confused here as the conversion in colormath gives me an xyz and not just an xy. And if I give the converstion of rbg(0,0,0) it gives a while color on the hue bulb.

from beautifulhue.api import Bridge
from colormath.color_objects import RGBColor
from time import sleep

    while True:
    from pprint import pprint
    color_set = colors_from_cam()
    pprint(color_set)

    for item in color_set:
        print "Getting the color"
        pprint(item)
        time_length = item[0] # prominence
        red, green, blue = item[1] # colors
        #Set a lights attributes based on RGB colors.
        rgb_color = RGBColor(red,green,blue)
        xyz_color = rgb_color.convert_to('xyz', target_rgb='cie_rgb')
        xyz_x = xyz_color.xyz_x
        xyz_y = xyz_color.xyz_y
        xyz_z = xyz_color.xyz_z
        resource = {
            'which':3,
            'data':{
            'state':{'on':True,
                     'xy':[xyz_x, xyz_y],
                         'transitiontime': 1,
                     'bri': 255}
            }
        }
        bridge.light.update(resource)
        print "sleeping"
        sleep(1)
    sleep(2)
    print "taking another picture."
Spore answered 31/12, 2013 at 9:14 Comment(0)
C
3

Z is a required component of the XYZ values you received from colormath's conversion, so omitting Z is probably not what you meant to do when you sent only X and Y to hue. I believe you need to integrate Z into your X and Y values to normalize them to a 2D color matrix.

x = X / (X + Y + Z);

y = Y / (X + Y + Z);

See: CIE 1931 color space and more specifically Chromaticity Diagram for these formulas.

Collected answered 3/2, 2014 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.