def rgb(self, red, green=None, blue=None, transitiontime=5): if isinstance(red, basestring): # assume a hex string is passed rstring = red red = int(rstring[1:3], 16) green = int(rstring[3:5], 16) blue = int(rstring[5:], 16) print red, green, blue # We need to convert the RGB value to Yxy. redScale = float(red) / 255.0 greenScale = float(green) / 255.0 blueScale = float(blue) / 255.0 colormodels.init( phosphor_red=colormodels.xyz_color(0.64843, 0.33086), phosphor_green=colormodels.xyz_color(0.4091, 0.518), phosphor_blue=colormodels.xyz_color(0.167, 0.04)) # logger.debug(redScale, greenScale, blueScale) xyz = colormodels.irgb_color(red, green, blue) # logger.debug(xyz) xyz = colormodels.xyz_from_rgb(xyz) # logger.debug(xyz) xyz = colormodels.xyz_normalize(xyz) # logger.debug(xyz) return self.set_state( {"xy": [xyz[0], xyz[1]], "transitiontime": transitiontime})
def rgb(self, red, green=None, blue=None, transitiontime=5): if isinstance(red, basestring): # assume a hex string is passed rstring = red red = int(rstring[1:3], 16) green = int(rstring[3:5], 16) blue = int(rstring[5:], 16) print red, green, blue # We need to convert the RGB value to Yxy. redScale = float(red) / 255.0 greenScale = float(green) / 255.0 blueScale = float(blue) / 255.0 colormodels.init(phosphor_red=colormodels.xyz_color(0.64843, 0.33086), phosphor_green=colormodels.xyz_color(0.4091, 0.518), phosphor_blue=colormodels.xyz_color(0.167, 0.04)) logger.debug(redScale, greenScale, blueScale) xyz = colormodels.irgb_color(red, green, blue) logger.debug(xyz) xyz = colormodels.xyz_from_rgb(xyz) logger.debug(xyz) xyz = colormodels.xyz_normalize(xyz) logger.debug(xyz) return self.set_state({ "xy": [xyz[0], xyz[1]], "transitiontime": transitiontime })
def test_xyz_lab(verbose=1): '''Test that lab_from_xyz() and xyz_from_lab() are inverses.''' def test_A(xyz0, tolerance=1.0e-10, verbose=1): '''Test that lab_from_xyz() and xyz_from_lab() are inverses.''' lab0 = colormodels.lab_from_xyz(xyz0) xyz1 = colormodels.xyz_from_lab(lab0) lab1 = colormodels.lab_from_xyz(xyz1) # check errors dlab = lab1 - lab0 error_lab = math.sqrt(numpy.dot(dlab, dlab)) dxyz = xyz1 - xyz0 error_xyz = math.sqrt(numpy.dot(dxyz, dxyz)) passed = (error_lab <= tolerance) and (error_xyz <= tolerance) if passed: status = 'pass' else: status = 'FAILED' msg = 'test_xyz_lab.test_A() : xyz0 = %s, lab(xyz0) = %s, xyz(lab(xyz0)) = %s, lab(xyz(lab(xyz0))) = %s, errors = (%g, %g), %s' % ( str(xyz0), str(lab0), str(xyz1), str(lab1), error_lab, error_xyz, status) if verbose >= 1: print(msg) if not passed: pass raise ValueError(msg) return passed num_passed = 0 num_failed = 0 for i in range(0, 100): x0 = 10.0 * random.random() y0 = 10.0 * random.random() z0 = 10.0 * random.random() xyz0 = colormodels.xyz_color(x0, y0, z0) passed = test_A(xyz0, tolerance=1.0e-10, verbose=verbose) if passed: num_passed += 1 else: num_failed += 1 # Test black explicitly xyz0 = colormodels.xyz_color(0.0, 0.0, 0.0) passed = test_A(xyz0, tolerance=1.0e-10, verbose=verbose) if passed: num_passed += 1 else: num_failed += 1 msg = 'test_xyz_lab() : %d tests passed, %d tests failed' % (num_passed, num_failed) print(msg)
def color_adj(self, light_id): if light_id == 1: rgba = self.color_picker1.wheel.color elif light_id == 2: rgba = self.color_picker2.wheel.color elif light_id == 3: rgba = self.color_picker2.wheel.color red = rgba[0] green = rgba[1] blue = rgba[2] colormodels.init( phosphor_red=colormodels.xyz_color(0.64843, 0.33086), phosphor_green=colormodels.xyz_color(0.4091, 0.518), phosphor_blue=colormodels.xyz_color(0.167, 0.04)) xyz = colormodels.irgb_color(red, green, blue) xyz = colormodels.xyz_from_rgb(xyz) xyz = colormodels.xyz_normalize(xyz) #print xyz, '\n' xy = [xyz[0], xyz[1]] huehub = 'http://' + ip + '/api/'+ myhash + "/lights/" + str(light_id) reply = requests.get(huehub) a=json.loads(reply.text) #print bri_val payload = json.dumps({"xy":xy}) sethuehub = huehub + "/state" reply = requests.put(sethuehub, data=payload) #Update hue data printout reply= requests.get(huehub) a=json.loads(reply.text) if light_id == 1: self.hue_label1.text = 'Hue :'+str(a['state']['hue']) self.bri_label1.text = 'Brightness :'+str(a['state']['bri']) self.sat_label1.text = 'Saturation :'+str(a['state']['sat']) self.ct_label1.text = 'Colour Temp :'+str(a['state']['ct']) self.xy_label1.text = '(x, y) :'+str(a['state']['xy']) elif light_id == 2: self.hue_label2.text = 'Hue :'+str(a['state']['hue']) self.bri_label2.text = 'Brightness :'+str(a['state']['bri']) self.sat_label2.text = 'Saturation :'+str(a['state']['sat']) self.ct_label2.text = 'Colour Temp :'+str(a['state']['ct']) self.xy_label2.text = '(x, y) :'+str(a['state']['xy']) elif light_id == 3: self.hue_label3.text = 'Hue :'+str(a['state']['hue']) self.bri_label3.text = 'Brightness :'+str(a['state']['bri']) self.sat_label3.text = 'Saturation :'+str(a['state']['sat']) self.ct_label3.text = 'Colour Temp :'+str(a['state']['ct']) self.xy_label3.text = '(x, y) :'+str(a['state']['xy'])
def _rgb_transfer(self, red, green=None, blue=None): if isinstance(red, basestring): rstring = red red = int(rstring[1:3], 16) green = int(rstring[3:5], 16) blue = int(rstring[5:], 16) colormodels.init( phosphor_red=colormodels.xyz_color(0.64843, 0.33086), phosphor_green=colormodels.xyz_color(0.4091, 0.518), phosphor_blue=colormodels.xyz_color(0.167, 0.04)) xyz = colormodels.irgb_color(red, green, blue) xyz = colormodels.xyz_from_rgb(xyz) xyz = colormodels.xyz_normalize(xyz) d = dict(xy=[xyz[0], xyz[1]], transitiontime=self._conf.default_transitiontime) return self.set_state(d)
def test_xyz_rgb(verbose=1): '''Test that xyz_to_rgb() and rgb_to_xyz() are inverses.''' def test_A(xyz0, tolerance=1.0e-10, verbose=1): rgb0 = colormodels.rgb_from_xyz(xyz0) xyz1 = colormodels.xyz_from_rgb(rgb0) rgb1 = colormodels.rgb_from_xyz(xyz1) # check errors err_rgb = rgb1 - rgb0 error_rgb = math.sqrt(numpy.dot(err_rgb, err_rgb)) err_xyz = xyz1 - xyz0 error_xyz = math.sqrt(numpy.dot(err_xyz, err_xyz)) passed = (error_rgb <= tolerance) and (error_xyz <= tolerance) if passed: status = 'pass' else: status = 'FAILED' msg = 'test_xyz_rgb.test_A() : xyz0 = %s, rgb(xyz0) = %s, xyz(rgb(xyz0)) = %s, rgb(xyz(rgb(xyz0))) = %s, errors = (%g, %g), %s' % ( str(xyz0), str(rgb0), str(xyz1), str(rgb1), error_rgb, error_xyz, status) if verbose >= 1: print(msg) if not passed: pass raise ValueError(msg) return passed num_passed = 0 num_failed = 0 for i in range(0, 100): x0 = 10.0 * random.random() y0 = 10.0 * random.random() z0 = 10.0 * random.random() xyz0 = colormodels.xyz_color(x0, y0, z0) passed = test_A(xyz0, tolerance=1.0e-10, verbose=verbose) if passed: num_passed += 1 else: num_failed += 1 # Test that the conversion matrices are inverses test_eye0 = numpy.dot(colormodels.rgb_from_xyz_matrix, colormodels.xyz_from_rgb_matrix) test_eye1 = numpy.dot(colormodels.xyz_from_rgb_matrix, colormodels.rgb_from_xyz_matrix) passed = numpy.allclose(test_eye0, numpy.eye(3)) and numpy.allclose( test_eye1, numpy.eye(3)) if passed: num_passed += 1 else: num_failed += 1 msg = 'test_xyz_rgb() : %d tests passed, %d tests failed' % (num_passed, num_failed) print(msg)
def colour_select(self, light_id): try: (rgb, hx) = tkColorChooser.askcolor() if (rgb, hx) == (None, None): warnings.warn('ColorChooser Error: No RGB colour selected.') else: print '(rgb, hx):', rgb, hx red = rgb[0] green = rgb[1] blue = rgb[2] redScale = float(red) / 255.0 #rgb goes from 0-255, this changes it into the 0-1.0 that xy uses greenScale = float(green) / 255.0 blueScale = float(blue) / 255.0 # Initialization function for conversion between CIE XYZ and linear RGB spaces colormodels.init( phosphor_red=colormodels.xyz_color(0.64843, 0.33086), phosphor_green=colormodels.xyz_color(0.4091, 0.518), phosphor_blue=colormodels.xyz_color(0.167, 0.04)) xyz = colormodels.irgb_color(red, green, blue) xyz = colormodels.xyz_from_rgb(xyz) xyz = colormodels.xyz_normalize(xyz) print xyz, '\n' xy = [xyz[0], xyz[1]] #print 'rgb: ', red, green, blue #global huehub huehub = "http://192.168.0.100/api/"+ myhash + "/lights/" + str(light_id) reply = requests.get(huehub) a=json.loads(reply.text) #print bri_val payload = json.dumps({"xy":xy}) sethuehub = huehub + "/state" reply = requests.put(sethuehub, data=payload) except TypeError, e: print 'Error closing ColorChooser: variable referenced before assignment\n', e, '\n' pass
def rgb(self, red, green, blue, transitiontime=5): red = int(red) blue = int(blue) green = int(green) transitiontime = int(transitiontime) # We need to convert the RGB value to Yxy. redScale = float(red) / 255.0 greenScale = float(green) / 255.0 blueScale = float(blue) / 255.0 colormodels.init( phosphor_red=colormodels.xyz_color(0.64843, 0.33086), phosphor_green=colormodels.xyz_color(0.4091, 0.518), phosphor_blue=colormodels.xyz_color(0.167, 0.04)) logger.debug(redScale, greenScale, blueScale) xyz = colormodels.irgb_color(red, green, blue) logger.debug(xyz) xyz = colormodels.xyz_from_rgb(xyz) logger.debug(xyz) xyz = colormodels.xyz_normalize(xyz) logger.debug(xyz) return self.set_state( **{"xy": [xyz[0], xyz[1]], "transitiontime": transitiontime})
def test_xyz_irgb(verbose=1): '''Test the direct conversions from xyz to irgb.''' for i in range(0, 100): x0 = 10.0 * random.random() y0 = 10.0 * random.random() z0 = 10.0 * random.random() xyz0 = colormodels.xyz_color(x0, y0, z0) irgb0 = colormodels.irgb_from_rgb(colormodels.rgb_from_xyz(xyz0)) irgb1 = colormodels.irgb_from_xyz(xyz0) if (irgb0[0] != irgb1[0]) or (irgb0[1] != irgb1[1]) or (irgb0[2] != irgb1[2]): raise ValueError irgbs0 = colormodels.irgb_string_from_rgb( colormodels.rgb_from_xyz(xyz0)) irgbs1 = colormodels.irgb_string_from_xyz(xyz0) if irgbs0 != irgbs1: raise ValueError print('Passed test_xyz_irgb()')
self.state = state self.config = state['config'] self.schedules = state['schedules'] self.groups = state['groups'] for l in state['lights']: light = self._lights.get("l%s" % l, None) if not light: light = ExtendedColorLight(self, l) self._lights["l%s" % l] = light light.update_state_cache(state['lights'][l]) self.last_update_state = datetime.datetime.now() colormodels.init( phosphor_red=colormodels.xyz_color(0.64843, 0.33086), phosphor_green=colormodels.xyz_color(0.4091, 0.518), phosphor_blue=colormodels.xyz_color(0.167, 0.04)) class LightGroup(object): __slots__ = ['_lights'] def __init__(self, lights): super(LightGroup, self).__setattr__('_lights', lights) def __getattr__(self, name): def apply_to_all(*args, **kwargs): for l in self._lights.values(): getattr(l, name)(*args, **kwargs) return self origfunc = getattr(self._lights.itervalues().next(), name) functools.update_wrapper(apply_to_all, origfunc)
def test_uv_primes(verbose=1): '''Test that uv_primes() and uv_primes_inverse() are inverses.''' def test_A(xyz0, tolerance=0.0, verbose=1): (up0, vp0) = colormodels.uv_primes(xyz0) xyz1 = colormodels.uv_primes_inverse(up0, vp0, xyz0[1]) # check error dxyz = (xyz1 - xyz0) error = math.sqrt(numpy.dot(dxyz, dxyz)) passed = (error <= tolerance) if passed: status = 'pass' else: status = 'FAILED' msg = 'test_uv_primes.test_A() : xyz0 = %s, (up,vp) = (%g,%g), xyz(up,vp) = %s, error = %g, %s' % ( str(xyz0), up0, vp0, str(xyz1), error, status) if verbose >= 1: print(msg) if not passed: pass raise ValueError(msg) return passed def test_B(up0, vp0, y0, tolerance=0.0, verbose=1): xyz0 = colormodels.uv_primes_inverse(up0, vp0, y0) (up1, vp1) = colormodels.uv_primes(xyz0) # check error error_up = up1 - up0 error_vp = vp1 - vp0 error = numpy.hypot(error_up, error_vp) passed = (error <= tolerance) if passed: status = 'pass' else: status = 'FAILED' msg = 'test_uv_primes.test_B() : (up0,vp0,y0) = (%g,%g,%g), xyz (up0,vp0,y0) = %s, (up,vp)(xyz) = (%g,%g), error = %g, %s' % ( up0, vp0, y0, str(xyz0), up1, vp1, error, status) if verbose >= 1: print(msg) if not passed: pass raise ValueError(msg) return passed num_passed = 0 num_failed = 0 # Test A for i in range(0, 100): x0 = 10.0 * random.random() y0 = 10.0 * random.random() z0 = 10.0 * random.random() xyz0 = colormodels.xyz_color(x0, y0, z0) passed = test_A(xyz0, tolerance=1.0e-13, verbose=verbose) if passed: num_passed += 1 else: num_failed += 1 # Test black case explicitly xyz0 = colormodels.xyz_color(0.0, 0.0, 0.0) passed = test_A(xyz0, tolerance=0.0, verbose=verbose) if passed: num_passed += 1 else: num_failed += 1 # Test B for i in range(0, 100): up0 = 4.0 * (2.0 * random.random() - 1.0) vp0 = 9.0 * (2.0 * random.random() - 1.0) y0 = 10.0 * random.random() passed = test_B(up0, vp0, y0, tolerance=1.0e-13, verbose=verbose) if passed: num_passed += 1 else: num_failed += 1 # Test black case explicitly passed = test_B(0.0, 0.0, 0.0, tolerance=0.0, verbose=verbose) if passed: num_passed += 1 else: num_failed += 1 msg = 'test_uv_primes() : %d tests passed, %d tests failed' % (num_passed, num_failed) print(msg)