def xyzFromRGB(red, green=None, blue=None): """ RGB转成xyz """ if isinstance(red, basestring): # assume a hex string is passed if red[0] == "#": colorString = red[1:] else: colorString = red red = int(colorString[0:2], 16) green = int(colorString[2:4], 16) blue = int(colorString[4:], 16) # 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)) xyz = colormodels.irgb_color(red, green, blue) xyz = colormodels.xyz_from_rgb(xyz) xyz = colormodels.xyz_normalize(xyz) return xyz
def get_normalized_spectral_line_colors_annotated ( brightness = 1.0, num_purples = 0, dwl_angstroms = 10): '''Get an array of xyz colors covering the visible spectrum. Optionally add a number of 'purples', which are colors interpolated between the color of the lowest wavelength (violet) and the highest (red). A text string describing the color is supplied for each color. brightness - Desired maximum rgb component of each color. Default 1.0. (Maxiumum displayable brightness) num_purples - Number of colors to interpolate in the 'purple' range. Default 0. (No purples) dwl_angstroms - Wavelength separation, in angstroms (0.1 nm). Default 10 A. (1 nm spacing) ''' # get range of wavelengths, in angstroms, so that we can have finer resolution than 1 nm wl_angstrom_range = range (10*start_wl_nm, 10*(end_wl_nm + 1), dwl_angstroms) # get total point count num_spectral = len (wl_angstrom_range) num_points = num_spectral + num_purples xyzs = numpy.empty ((num_points, 3)) names = [] # build list of normalized color x,y values proceeding along each wavelength i = 0 for wl_A in wl_angstrom_range: wl_nm = wl_A * 0.1 xyz = xyz_from_wavelength (wl_nm) colormodels.xyz_normalize (xyz) xyzs [i] = xyz name = '%.1f nm' % wl_nm names.append (name) i += 1 # interpolate from end point to start point (filling in the purples) first_xyz = xyzs [0] last_xyz = xyzs [num_spectral - 1] for ipurple in range (0, num_purples): t = float (ipurple) / float (num_purples - 1) omt = 1.0 - t xyz = t * first_xyz + omt * last_xyz colormodels.xyz_normalize (xyz) xyzs [i] = xyz name = '%03d purple' % math.floor (1000.0 * t + 0.5) names.append (name) i += 1 # scale each color to have the max rgb component equal to the desired brightness for i in range (0, num_points): rgb = colormodels.brightest_rgb_from_xyz (xyzs [i], brightness) xyzs [i] = colormodels.xyz_from_rgb (rgb) # done return (xyzs, names)
def check_xyz_rgb(self, xyz0, verbose): ''' Check that the xyz color, converted to rgb then back, remains the same. ''' 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)) tolerance = 1.0e-10 self.assertLess(error_xyz, tolerance) self.assertLess(error_rgb, tolerance) msg = 'xyz: %s rgb: %s xyz2: %s rgb2: %s' % ( str(xyz0), str(rgb0), str(xyz1), str(rgb1)) if verbose: print (msg)
def get_normalized_spectral_line_colors(brightness=1.0, num_purples=0, dwl_angstroms=10): '''Get an array of xyz colors covering the visible spectrum. Optionally add a number of 'purples', which are colors interpolated between the color of the lowest wavelength (violet) and the highest (red). brightness - Desired maximum rgb component of each color. Default 1.0. (Maxiumum displayable brightness) num_purples - Number of colors to interpolate in the 'purple' range. Default 0. (No purples) dwl_angstroms - Wavelength separation, in angstroms (0.1 nm). Default 10 A. (1 nm spacing) ''' # get range of wavelengths, in angstroms, so that we can have finer resolution than 1 nm wl_angstrom_range = xrange(10 * start_wl_nm, 10 * (end_wl_nm + 1), dwl_angstroms) # get total point count num_spectral = len(wl_angstrom_range) num_points = num_spectral + num_purples xyzs = numpy.empty((num_points, 3)) # build list of normalized color x,y values proceeding along each wavelength i = 0 for wl_A in wl_angstrom_range: wl_nm = wl_A * 0.1 xyz = xyz_from_wavelength(wl_nm) colormodels.xyz_normalize(xyz) xyzs[i] = xyz i += 1 # interpolate from end point to start point (filling in the purples) first_xyz = xyzs[0] last_xyz = xyzs[num_spectral - 1] for ipurple in xrange(0, num_purples): t = float(ipurple) / float(num_purples - 1) omt = 1.0 - t xyz = t * first_xyz + omt * last_xyz colormodels.xyz_normalize(xyz) xyzs[i] = xyz i += 1 # scale each color to have the max rgb component equal to the desired brightness for i in xrange(0, num_points): rgb = colormodels.rgb_from_xyz(xyzs[i]) max_rgb = max(rgb) if max_rgb != 0.0: scale = brightness / max_rgb rgb *= scale xyzs[i] = colormodels.xyz_from_rgb(rgb) # done return xyzs
def check_xyz_rgb(self, xyz0, verbose): ''' Check that the xyz color, converted to rgb then back, remains the same. ''' 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)) tolerance = 1.0e-10 self.assertLess(error_xyz, tolerance) self.assertLess(error_rgb, tolerance) msg = 'xyz: %s rgb: %s xyz2: %s rgb2: %s' % ( str(xyz0), str(rgb0), str(xyz1), str(rgb1)) if verbose: print(msg)
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
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