def planckian_locus(T): """XYZ colour coordinates of a theoretical incadescent black body radiator at a given temperature, or something like that. Can be used to calculate the chromaticity coordinates of, say, a 6500K light source http://en.wikipedia.org/wiki/Planckian_locus Argument T is in Kelvin, return value is a CIE XYZ value """ samples = (780-380)/5 # values every 5 nm are stored from colour_matching_functions import get_colour_matching_functions X, Y, Z = get_colour_matching_functions(two_degree = True) # Integrated between 380nm and 780nm, as that is the range of # values contained in the colour-matching functions X_T = integrate(lambda wavelen: plancks_law(wavelen, T) * X(wavelen), 380, 780, samples) Y_T = integrate(lambda wavelen: plancks_law(wavelen, T) * Y(wavelen), 380, 780, samples) Z_T = integrate(lambda wavelen: plancks_law(wavelen, T) * Z(wavelen), 380, 780, samples) return (X_T, Y_T, Z_T)
def main(): from matplotlib import pyplot # Plot CIE XYZ colour matching curves print "Calculating" from colour_matching_functions import get_colour_matching_functions X, Y, Z = get_colour_matching_functions(two_degree = True) print "Test" wavelens =[x for x in range(380, 780, 5)] sampled_x = [X(T) for T in wavelens] sampled_x = [x/max(sampled_x) for x in sampled_x] pyplot.plot(sampled_x) sampled_y = [Y(T) for T in wavelens] sampled_y = [x/max(sampled_y) for x in sampled_y] pyplot.plot(sampled_y) sampled_z = [Z(T) for T in wavelens] sampled_z = [x/max(sampled_z) for x in sampled_z] pyplot.plot(sampled_z) pyplot.show() # Plot spectral locus # As explained on http://www.photo-mark.com/notes/2010/sep/08/deconstructing-chromaticity/ spectral_locus_x = [] spectral_locus_y = [] for x, y, z in zip(sampled_x, sampled_y, sampled_z): spectral_locus_x.append( x / (x+y+z)) spectral_locus_y.append( y / (x+y+z)) pyplot.plot(spectral_locus_x, spectral_locus_y) # Also plot Plankian locus T_and_radiance = [] for T in range(1000, 15000, 350): # Calculate plankian locus T_and_radiance.append([T, planckian_locus(T)]) to_plot_x, to_plot_y = [], [] for (TT, XYZ) in T_and_radiance: # Calculate x,y chromaticity coordinate # http://en.wikipedia.org/wiki/CIE_1931_color_space#The_CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space XT, YT, ZT = XYZ x = XT / (XT+YT+ZT) y = YT / (XT+YT+ZT) to_plot_x.append(x) to_plot_y.append(y) pyplot.plot(to_plot_x, to_plot_y) pyplot.axis(xmin = 0, xmax = 0.8, ymin = 0, ymax = 0.9) pyplot.show() print "Checking against approximation" test_plankian_locus_against_approximation() print "..okay"