def test_clipping (verbose=1): '''Test the various color clipping methods.''' xyz_colors = ciexyz.get_normalized_spectral_line_colors () #print 'xyz_colors', xyz_colors (num_wl, num_cols) = xyz_colors.shape # get rgb values for standard clipping colormodels.init_clipping (colormodels.CLIP_ADD_WHITE) rgb_add_white = [] for i in xrange (0, num_wl): color = colormodels.irgb_string_from_rgb ( colormodels.rgb_from_xyz (xyz_colors [i])) rgb_add_white.append (color) # get rgb values for clamp clipping colormodels.init_clipping (colormodels.CLIP_CLAMP_TO_ZERO) rgb_clamp = [] for i in xrange (0, num_wl): color = colormodels.irgb_string_from_rgb ( colormodels.rgb_from_xyz (xyz_colors [i])) rgb_clamp.append (color) # compare if verbose >= 1: print 'colors from add white, colors from clamp' for i in xrange (0, num_wl): print rgb_add_white [i], rgb_clamp [i] print 'Passed test_clipping()'
def test_clipping(verbose=1): '''Test the various color clipping methods.''' xyz_colors = ciexyz.get_normalized_spectral_line_colors() #print 'xyz_colors', xyz_colors (num_wl, num_cols) = xyz_colors.shape # get rgb values for standard clipping colormodels.init_clipping(colormodels.CLIP_ADD_WHITE) rgb_add_white = [] for i in xrange(0, num_wl): color = colormodels.irgb_string_from_rgb( colormodels.rgb_from_xyz(xyz_colors[i])) rgb_add_white.append(color) # get rgb values for clamp clipping colormodels.init_clipping(colormodels.CLIP_CLAMP_TO_ZERO) rgb_clamp = [] for i in xrange(0, num_wl): color = colormodels.irgb_string_from_rgb( colormodels.rgb_from_xyz(xyz_colors[i])) rgb_clamp.append(color) # compare if verbose >= 1: print('colors from add white, colors from clamp') for i in xrange(0, num_wl): print(rgb_add_white[i], rgb_clamp[i]) print('Passed test_clipping()')
def spectrum_subplot (spectrum): '''Plot a spectrum, with x-axis the wavelength, and y-axis the intensity. The curve is colored at that wavelength by the (approximate) color of a pure spectral color at that wavelength, with intensity constant over wavelength. (This means that dark looking colors here mean that wavelength is poorly viewed by the eye. This is not a complete plotting function, e.g. no file is saved, etc. It is assumed that this function is being called by one that handles those things.''' (num_wl, num_cols) = spectrum.shape # get rgb colors for each wavelength rgb_colors = numpy.empty ((num_wl, 3)) for i in xrange (0, num_wl): wl_nm = spectrum [i][0] xyz = ciexyz.xyz_from_wavelength (wl_nm) rgb_colors [i] = colormodels.rgb_from_xyz (xyz) # scale to make brightest rgb value = 1.0 rgb_max = numpy.max (rgb_colors) scaling = 1.0 / rgb_max rgb_colors *= scaling # draw color patches (thin vertical lines matching the spectrum curve) in color for i in xrange (0, num_wl-1): # skipping the last one here to stay in range x0 = spectrum [i][0] x1 = spectrum [i+1][0] y0 = spectrum [i][1] y1 = spectrum [i+1][1] poly_x = [x0, x1, x1, x0] poly_y = [0.0, 0.0, y1, y0] color_string = colormodels.irgb_string_from_rgb (rgb_colors [i]) pylab.fill (poly_x, poly_y, color_string, edgecolor=color_string) # plot intensity as a curve pylab.plot ( spectrum [:,0], spectrum [:,1], color='k', linewidth=2.0, antialiased=True)
def spectrum_subplot(spectrum): '''Plot a spectrum, with x-axis the wavelength, and y-axis the intensity. The curve is colored at that wavelength by the (approximate) color of a pure spectral color at that wavelength, with intensity constant over wavelength. (This means that dark looking colors here mean that wavelength is poorly viewed by the eye. This is not a complete plotting function, e.g. no file is saved, etc. It is assumed that this function is being called by one that handles those things.''' (num_wl, num_cols) = spectrum.shape # get rgb colors for each wavelength rgb_colors = numpy.empty((num_wl, 3)) for i in range(0, num_wl): wl_nm = spectrum[i][0] xyz = ciexyz.xyz_from_wavelength(wl_nm) rgb_colors[i] = colormodels.rgb_from_xyz(xyz) # scale to make brightest rgb value = 1.0 rgb_max = numpy.max(rgb_colors) scaling = 1.0 / rgb_max rgb_colors *= scaling # draw color patches (thin vertical lines matching the spectrum curve) in color for i in range(0, num_wl - 1): # skipping the last one here to stay in range x0 = spectrum[i][0] x1 = spectrum[i + 1][0] y0 = spectrum[i][1] y1 = spectrum[i + 1][1] poly_x = [x0, x1, x1, x0] poly_y = [0.0, 0.0, y1, y0] color_string = colormodels.irgb_string_from_rgb(rgb_colors[i]) pylab.fill(poly_x, poly_y, color_string, edgecolor=color_string) # plot intensity as a curve w = spectrum[:, 0] I = spectrum[:, 1] return w, I
def color_vs_param_plot ( param_list, rgb_colors, title, filename = None, tight = False, plotfunc = pylab.plot, xlabel = 'param', ylabel = 'RGB Color'): '''Plot for a color that varies with a parameter - In a two part figure, draw: top: color as it varies with parameter (x axis) low: r,g,b values, as linear 0.0-1.0 values, of the attempted color. param_list - list of parameters (x axis) rgb_colors - numpy array, one row for each param in param_list title - title for plot filename - filename to save plot to plotfunc - optional plot function to use (default pylab.plot) xlabel - label for x axis ylabel - label for y axis (default 'RGB Color') ''' pylab.clf () # draw color bars in upper plot pylab.subplot (2,1,1) pylab.title (title) # no xlabel, ylabel in upper plot num_points = len (param_list) for i in xrange (0, num_points-1): x0 = param_list [i] x1 = param_list [i+1] y0 = 0.0 y1 = 1.0 poly_x = [x0, x1, x1, x0] poly_y = [y0, y0, y1, y1] color_string = colormodels.irgb_string_from_rgb (rgb_colors [i]) pylab.fill (poly_x, poly_y, color_string, edgecolor=color_string) if tight: tighten_x_axis (param_list) # draw rgb curves in lower plot pylab.subplot (2,1,2) # no title in lower plot plotfunc (param_list, rgb_colors [:,0], color='r', label='Red') plotfunc (param_list, rgb_colors [:,1], color='g', label='Green') plotfunc (param_list, rgb_colors [:,2], color='b', label='Blue') if tight: tighten_x_axis (param_list) pylab.xlabel (xlabel) pylab.ylabel (ylabel) if filename is not None: print 'Saving plot %s' % str (filename) pylab.savefig (filename)
def test_clipping(self, verbose=False): ''' Test the various color clipping methods. ''' # This is just a coverage test. xyz_colors = ciexyz.get_normalized_spectral_line_colors() num_wl = xyz_colors.shape[0] for i in range(num_wl): # Get rgb values for standard add white clipping. colormodels.init_clipping(colormodels.CLIP_ADD_WHITE) rgb_white_color = colormodels.irgb_string_from_rgb( colormodels.rgb_from_xyz(xyz_colors[i])) # Get rgb values for clamp-to-zero clipping. colormodels.init_clipping(colormodels.CLIP_CLAMP_TO_ZERO) rgb_clamp_color = colormodels.irgb_string_from_rgb( colormodels.rgb_from_xyz(xyz_colors[i])) msg = 'Wavelength: %s White: %s Clamp: %s' % ( str(i), # FIXME: Put in Angstroms. rgb_white_color, rgb_clamp_color) if verbose: print(msg)
def test_clipping(self, verbose=False): ''' Test the various color clipping methods. ''' # This is just a coverage test. xyz_colors = ciexyz.get_normalized_spectral_line_colors () num_wl = xyz_colors.shape[0] for i in range (num_wl): # Get rgb values for standard add white clipping. colormodels.init_clipping (colormodels.CLIP_ADD_WHITE) rgb_white_color = colormodels.irgb_string_from_rgb ( colormodels.rgb_from_xyz (xyz_colors [i])) # Get rgb values for clamp-to-zero clipping. colormodels.init_clipping (colormodels.CLIP_CLAMP_TO_ZERO) rgb_clamp_color = colormodels.irgb_string_from_rgb ( colormodels.rgb_from_xyz (xyz_colors [i])) msg = 'Wavelength: %s White: %s Clamp: %s' % ( str(i), # FIXME: Put in Angstroms. rgb_white_color, rgb_clamp_color) if verbose: print (msg)
def color_vs_param_plot(param_list, rgb_colors, title, filename, tight=False, plotfunc=pylab.plot, xlabel='param', ylabel='RGB Color'): '''Plot for a color that varies with a parameter - In a two part figure, draw: top: color as it varies with parameter (x axis) low: r,g,b values, as linear 0.0-1.0 values, of the attempted color. param_list - list of parameters (x axis) rgb_colors - numpy array, one row for each param in param_list title - title for plot filename - filename to save plot to plotfunc - optional plot function to use (default pylab.plot) xlabel - label for x axis ylabel - label for y axis (default 'RGB Color') ''' pylab.clf() # draw color bars in upper plot pylab.subplot(2, 1, 1) pylab.title(title) # no xlabel, ylabel in upper plot num_points = len(param_list) for i in xrange(0, num_points - 1): x0 = param_list[i] x1 = param_list[i + 1] y0 = 0.0 y1 = 1.0 poly_x = [x0, x1, x1, x0] poly_y = [y0, y0, y1, y1] color_string = colormodels.irgb_string_from_rgb(rgb_colors[i]) pylab.fill(poly_x, poly_y, color_string, edgecolor=color_string) if tight: tighten_x_axis(param_list) # draw rgb curves in lower plot pylab.subplot(2, 1, 2) # no title in lower plot plotfunc(param_list, rgb_colors[:, 0], color='r', label='Red') plotfunc(param_list, rgb_colors[:, 1], color='g', label='Green') plotfunc(param_list, rgb_colors[:, 2], color='b', label='Blue') if tight: tighten_x_axis(param_list) pylab.xlabel(xlabel) pylab.ylabel(ylabel) print 'Saving plot %s' % str(filename) pylab.savefig(filename)
def check_xyz_irgb(self, xyz0, verbose): ''' Check the direct conversions from xyz to irgb. ''' irgb0 = colormodels.irgb_from_rgb(colormodels.rgb_from_xyz(xyz0)) irgb1 = colormodels.irgb_from_xyz(xyz0) self.assertEqual(irgb0[0], irgb1[0]) self.assertEqual(irgb0[1], irgb1[1]) self.assertEqual(irgb0[2], irgb1[2]) # The string should also match. irgbs0 = colormodels.irgb_string_from_rgb( colormodels.rgb_from_xyz(xyz0)) irgbs1 = colormodels.irgb_string_from_xyz(xyz0) msg = 'irgb0: %s text0: %s irgb1: %s text1: %s' % ( str(irgb0), irgbs0, str(irgb1), irgbs1) if verbose: print(msg) self.assertEqual(irgbs0, irgbs1)
def check_xyz_irgb(self, xyz0, verbose): ''' Check the direct conversions from xyz to irgb. ''' irgb0 = colormodels.irgb_from_rgb ( colormodels.rgb_from_xyz (xyz0)) irgb1 = colormodels.irgb_from_xyz (xyz0) self.assertEqual(irgb0[0], irgb1[0]) self.assertEqual(irgb0[1], irgb1[1]) self.assertEqual(irgb0[2], irgb1[2]) # The string should also match. irgbs0 = colormodels.irgb_string_from_rgb ( colormodels.rgb_from_xyz (xyz0)) irgbs1 = colormodels.irgb_string_from_xyz (xyz0) msg = 'irgb0: %s text0: %s irgb1: %s text1: %s' % ( str(irgb0), irgbs0, str(irgb1), irgbs1) if verbose: print (msg) self.assertEqual(irgbs0, irgbs1)
def test_xyz_irgb (verbose=1): '''Test the direct conversions from xyz to irgb.''' for i in xrange (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()'
def test_xyz_irgb(verbose=1): '''Test the direct conversions from xyz to irgb.''' for i in xrange(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()')
def spectrum_plot ( spectrum, title, filename = None, xlabel = 'Wavelength ($nm$)', ylabel = 'Intensity ($W/m^2$)'): '''Plot for a single spectrum - In a two part graph, plot: top: color of the spectrum, as a large patch. low: graph of spectrum intensity vs wavelength (x axis). The graph is colored by the (approximated) color of each wavelength. Each wavelength has equal physical intensity, so the variation in apparent intensity (e.g. 400, 800 nm are very dark, 550 nm is bright), is due to perceptual factors in the eye. This helps show how much each wavelength contributes to the percieved color. spectrum - spectrum to plot title - title for plot filename - filename to save plot to xlabel - label for x axis ylabel - label for y axis ''' pylab.clf () # upper plot - solid patch of color that matches the spectrum color pylab.subplot (2,1,1) pylab.title (title) color_string = colormodels.irgb_string_from_rgb ( colormodels.rgb_from_xyz (ciexyz.xyz_from_spectrum (spectrum))) poly_x = [0.0, 1.0, 1.0, 0.0] poly_y = [0.0, 0.0, 1.0, 1.0] pylab.fill (poly_x, poly_y, color_string) # draw a solid line around the patch to look nicer pylab.plot (poly_x, poly_y, color='k', linewidth=2.0) pylab.axis ('off') # lower plot - spectrum vs wavelength, with colors of the associated spectral lines below pylab.subplot (2,1,2) spectrum_subplot (spectrum) tighten_x_axis (spectrum [:,0]) pylab.xlabel (xlabel) pylab.ylabel (ylabel) # done if filename is not None: print 'Saving plot %s' % str (filename) pylab.savefig (filename)
def spectrum_plot(spectrum, title, filename, xlabel='Wavelength ($nm$)', ylabel='Intensity ($W/m^2$)'): '''Plot for a single spectrum - In a two part graph, plot: top: color of the spectrum, as a large patch. low: graph of spectrum intensity vs wavelength (x axis). The graph is colored by the (approximated) color of each wavelength. Each wavelength has equal physical intensity, so the variation in apparent intensity (e.g. 400, 800 nm are very dark, 550 nm is bright), is due to perceptual factors in the eye. This helps show how much each wavelength contributes to the percieved color. spectrum - spectrum to plot title - title for plot filename - filename to save plot to xlabel - label for x axis ylabel - label for y axis ''' pylab.clf() # upper plot - solid patch of color that matches the spectrum color pylab.subplot(2, 1, 1) pylab.title(title) color_string = colormodels.irgb_string_from_rgb( colormodels.rgb_from_xyz(ciexyz.xyz_from_spectrum(spectrum))) poly_x = [0.0, 1.0, 1.0, 0.0] poly_y = [0.0, 0.0, 1.0, 1.0] pylab.fill(poly_x, poly_y, color_string) # draw a solid line around the patch to look nicer pylab.plot(poly_x, poly_y, color='k', linewidth=2.0) pylab.axis('off') # lower plot - spectrum vs wavelength, with colors of the associated spectral lines below pylab.subplot(2, 1, 2) spectrum_subplot(spectrum) tighten_x_axis(spectrum[:, 0]) pylab.xlabel(xlabel) pylab.ylabel(ylabel) # done print 'Saving plot %s' % str(filename) pylab.savefig(filename)
def rgb_patch_plot ( rgb_colors, color_names, title, filename = None, patch_gap = 0.05, num_across = 6): '''Draw a set of color patches, specified as linear rgb colors.''' def draw_patch (x0, y0, color, name, patch_gap): '''Draw a patch of color.''' # patch relative vertices m = patch_gap omm = 1.0 - m poly_dx = [m, m, omm, omm] poly_dy = [m, omm, omm, m] # construct vertices poly_x = [ x0 + dx_i for dx_i in poly_dx ] poly_y = [ y0 + dy_i for dy_i in poly_dy ] pylab.fill (poly_x, poly_y, color) if name != None: dtext = 0.1 pylab.text (x0+dtext, y0+dtext, name, size=8.0) # make plot with each color with one patch pylab.clf() num_colors = len (rgb_colors) for i in xrange (0, num_colors): (iy, ix) = divmod (i, num_across) # get color as a displayable string colorstring = colormodels.irgb_string_from_rgb (rgb_colors [i]) if color_names != None: name = color_names [i] else: name = None draw_patch (float (ix), float (-iy), colorstring, name, patch_gap) pylab.axis ('off') pylab.title (title) if filename is not None: print 'Saving plot %s' % str (filename) pylab.savefig (filename)
def rgb_patch_plot(rgb_colors, color_names, title, filename, patch_gap=0.05, num_across=6): '''Draw a set of color patches, specified as linear rgb colors.''' def draw_patch(x0, y0, color, name, patch_gap): '''Draw a patch of color.''' # patch relative vertices m = patch_gap omm = 1.0 - m poly_dx = [m, m, omm, omm] poly_dy = [m, omm, omm, m] # construct vertices poly_x = [x0 + dx_i for dx_i in poly_dx] poly_y = [y0 + dy_i for dy_i in poly_dy] pylab.fill(poly_x, poly_y, color) if name != None: dtext = 0.1 pylab.text(x0 + dtext, y0 + dtext, name, size=8.0) # make plot with each color with one patch pylab.clf() num_colors = len(rgb_colors) for i in xrange(0, num_colors): (iy, ix) = divmod(i, num_across) # get color as a displayable string colorstring = colormodels.irgb_string_from_rgb(rgb_colors[i]) if color_names != None: name = color_names[i] else: name = None draw_patch(float(ix), float(-iy), colorstring, name, patch_gap) pylab.axis('off') pylab.title(title) print 'Saving plot %s' % str(filename) pylab.savefig(filename)
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 xrange (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 color patches for point in xy_list s = 0.025 # distance in xy plane towards white point for i in xrange (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) # 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 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 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 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 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 xrange(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 color patches for point in xy_list s = 0.025 # distance in xy plane towards white point for i in xrange(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) # 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 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