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 rgb(self, red, green=None, blue=None, transitiontime=5): if isinstance(red, basestring): if not red.startswith('#'): raise ArgumentError('rgb strings must start with "#"') # 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) logger.debug("%d %d %d"%(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 logger.debug("%d %d %d"%(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 SpectrumToRGB(q,iq): #mask = np.logical_and(q > 300e-9,q<800e-9) x = np.arange(300,800,1) y = np.interp(x,q[::-1],iq[::-1]) #spec = np.vstack([2*np.pi/q[mask],np.log(iq[mask]*q[mask]**2)]) #spec = np.vstack([q[mask],iq[mask]]) spec = np.vstack([x,y]) xyz = xyz_from_spectrum(spec.T) return rgb_from_xyz(xyz_normalize(xyz))
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 test_book (verbose=1): '''Test that the computed chromaticities match an existing table, from Judd and Wyszecki.''' num_passed = 0 num_failed = 0 (num_rows, num_cols) = book_chrom_table.shape for i in range (0, num_rows): T = book_chrom_table [i][0] book_x = book_chrom_table [i][1] book_y = book_chrom_table [i][2] # calculate color for T xyz = blackbody.blackbody_color (T) colormodels.xyz_normalize (xyz) dx = xyz [0] - book_x dy = xyz [1] - book_y # did we match the tablulated result? tolerance = 2.0e-4 passed = (math.fabs (dx) <= tolerance) and (math.fabs (dy) <= tolerance) if passed: status = 'pass' else: status = 'FAILED' msg = 'test_book() : T = %g : calculated x,y = %g,%g : book values x,y = %g,%g : errors = %g,%g' % ( T, xyz [0], xyz [1], book_x, book_y, dx, dy) if verbose >= 1: print(msg) if not passed: pass raise ValueError(msg) if passed: num_passed += 1 else: num_failed += 1 msg = 'test_book() : %d tests passed, %d tests failed' % ( num_passed, num_failed) print(msg)
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 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 shark_fin_plot (): '''Draw the 'shark fin' CIE chromaticity diagram of the pure spectral lines (plus purples) in xy space.''' # get array of (approximate) colors for the boundary of the fin xyz_list = ciexyz.get_normalized_spectral_line_colors (brightness=1.0, num_purples=200, dwl_angstroms=2) # get normalized colors xy_list = xyz_list.copy() (num_colors, num_cols) = xy_list.shape for i in range (0, num_colors): colormodels.xyz_normalize (xy_list [i]) # get phosphor colors and normalize red = colormodels.PhosphorRed green = colormodels.PhosphorGreen blue = colormodels.PhosphorBlue white = colormodels.PhosphorWhite colormodels.xyz_normalize (red) colormodels.xyz_normalize (green) colormodels.xyz_normalize (blue) colormodels.xyz_normalize (white) def get_direc_to_white (xyz): '''Get unit vector (xy plane) in direction of the white point.''' direc = white - xyz mag = math.hypot (direc [0], direc [1]) if mag != 0.0: direc /= mag return (direc[0], direc[1]) # plot pylab.clf () # draw best attempt at pure spectral colors on inner edge of shark fin s = 0.025 # distance in xy plane towards white point for i in range (0, len (xy_list)-1): x0 = xy_list [i][0] y0 = xy_list [i][1] x1 = xy_list [i+1][0] y1 = xy_list [i+1][1] # get unit vectors in direction of white point (dir_x0, dir_y0) = get_direc_to_white (xy_list [i]) (dir_x1, dir_y1) = get_direc_to_white (xy_list [i+1]) # polygon vertices poly_x = [x0, x1, x1 + s*dir_x1, x0 + s*dir_x0] poly_y = [y0, y1, y1 + s*dir_y1, y0 + s*dir_y0] # draw (using full color, not normalized value) color_string = colormodels.irgb_string_from_rgb ( colormodels.rgb_from_xyz (xyz_list [i])) pylab.fill (poly_x, poly_y, color_string, edgecolor=color_string) # fill in the monitor gamut with true colors def get_brightest_irgb_string (xyz): '''Convert the xyz color to rgb, scale to maximum displayable brightness, and convert to a string.''' rgb = colormodels.brightest_rgb_from_xyz (xyz) color_string = colormodels.irgb_string_from_rgb (rgb) return color_string def fill_gamut_slice (v0, v1, v2): '''Fill in a slice of the monitor gamut with the correct colors.''' #num_s, num_t = 10, 10 #num_s, num_t = 25, 25 num_s, num_t = 50, 50 dv10 = v1 - v0 dv21 = v2 - v1 for i_s in range (num_s): s_a = float (i_s) / float (num_s) s_b = float (i_s+1) / float (num_s) for i_t in range (num_t): t_a = float (i_t) / float (num_t) t_b = float (i_t+1) / float (num_t) # vertex coords v_aa = v0 + t_a * (dv10 + s_a * dv21) v_ab = v0 + t_b * (dv10 + s_a * dv21) v_ba = v0 + t_a * (dv10 + s_b * dv21) v_bb = v0 + t_b * (dv10 + s_b * dv21) # poly coords poly_x = [v_aa [0], v_ba [0], v_bb [0], v_ab [0]] poly_y = [v_aa [1], v_ba [1], v_bb [1], v_ab [1]] # average color avg = 0.25 * (v_aa + v_ab + v_ba + v_bb) # convert to rgb and scale to maximum displayable brightness color_string = get_brightest_irgb_string (avg) pylab.fill (poly_x, poly_y, color_string, edgecolor=color_string) fill_gamut_slice (white, blue, green) fill_gamut_slice (white, green, red) fill_gamut_slice (white, red, blue) # draw the curve of the xy values of the spectral lines and purples pylab.plot (xy_list [:,0], xy_list [:,1], color='#808080', linewidth=3.0) # draw monitor gamut and white point pylab.plot ([red [0], green[0]], [red [1], green[1]], 'o-', color='k') pylab.plot ([green[0], blue [0]], [green[1], blue [1]], 'o-', color='k') pylab.plot ([blue [0], red [0]], [blue [1], red [1]], 'o-', color='k') pylab.plot ([white[0], white[0]], [white[1], white[1]], 'o-', color='k') # label phosphors dx = 0.01 dy = 0.01 pylab.text (red [0] + dx, red [1], 'Red', ha='left', va='center') pylab.text (green [0], green [1] + dy, 'Green', ha='center', va='bottom') pylab.text (blue [0] - dx, blue [1], 'Blue', ha='right', va='center') pylab.text (white [0], white [1] + dy, 'White', ha='center', va='bottom') # titles etc pylab.axis ([0.0, 0.85, 0.0, 0.85]) pylab.xlabel (r'CIE $x$') pylab.ylabel (r'CIE $y$') pylab.title (r'CIE Chromaticity Diagram') filename = 'ChromaticityDiagram' print ('Saving plot %s' % (str (filename))) pylab.savefig (filename)
def SpectrumToRGB(q, iq): x = np.arange(300, 800, 1) y = np.interp(x, q[::-1], iq[::-1]) spec = np.vstack([x, y]) xyz = xyz_from_spectrum(spec.T) return rgb_from_xyz(xyz_normalize(xyz))