For example, this will plot the blackbody spectrum at 6500 K using the default wavelength range.
bb = Blackbody(6500)
bb.plot()
Change Blackbody.λ_nm
or make a new object Blackbody(temp,wavelengths)
to change the internal settings.
bb
You can also index the Blackbody object like an array. This will give you a tuple of the wavelengths at that index and the blackblody specific intensity
bb[0:3]
plot_cie_xyz_matching_functions()
Since the XYZ colour space has more colours than what sRGB can represent, there will be some RGB values that are negative. To "fix" this, you can use mode=CLAMP
to zero any negative values or mode=WHITEN
to inflate all values to be positive and then rescaling it back to the range from zero to original max value.
test_eq(wavelength2xyz(500),[0.0022533296481400955, 0.32735772154968634, 0.27144376385475233] )
test_eq(xyz2sRGB([0.00225333, 0.32735772, 0.27144376],mode="WHITEN"),[0,160,108])
plot_colour(xyz2sRGB(wavelength2xyz(580)))
test_eq(sRGB2hex([0, 160, 108]),"#00A06C")
Here are the two clipping modes. The difference is the whitening mode appears more purple in the ultraviolet region.
plot_wavelength_colours("WHITEN")
plot_wavelength_colours("CLAMP")
wavelength_intensity2hsv()
You may want to use a single hue to define a colour palette. The following pure_hue_palette
will create a palette from black to saturated colour depending on the wavelength. These palettes can be used in Bokeh plots.
pure_hue_palette(λ=450,n=8)
I was able to use these palettes in HoloViews to colour in grayscale images given the wavelength. Try the slider so iterate through different wavelengths.
import holoviews as hv
hv.notebook_extension("bokeh",logo=False)
img = cv2.imread("assets/test_img.jpeg", cv2.IMREAD_GRAYSCALE)
wavelengths = np.linspace(380,800,num=16)
def coloured_img(img,λ):
return hv.Image(img).opts(aspect="equal", cmap=pure_hue_palette(λ=λ,n=16),colorbar=True)
img_dict = {λ:coloured_img(img,λ) for λ in wavelengths}
hmap = hv.HoloMap(img_dict, kdims='wavelength (nm)')
hv.output(hmap, widget_location="bottom")
The UV and NIR regions should show as white if your spectrometer detects something at that frequency. Black is reserved for no light.
plot_hsv_LUT_spectrum()
img2 = Image.open("assets/flat_pic.png")
img2 = np.asarray(img2,dtype=np.float64)
img2 /= np.max(img2)
plt.imshow(img2,cmap='gray',extent=[400,850,0,np.shape(img2)[0]])
plt.xlabel("wavelengths (nm)")
plt.ylabel("cross-track")
plt.colorbar()
colour_hyperspectral_line(np.linspace(400,850,2064),img2)
img3
is a picture of the spectral lines from a HgAr spectral source.
img3 = Image.open("assets/HgAr_pic.png")
img3 = np.asarray(img3,dtype=np.float64)
img3 /= np.max(img3)
plt.imshow(img3,cmap='gray',extent=[400,850,0,np.shape(img3)[0]])
plt.xlabel("wavelengths (nm)")
plt.ylabel("cross-track")
plt.colorbar()
colour_hyperspectral_line(np.linspace(400,850,2064),img3)
hyperspec_line2colour(np.linspace(400,850,2064),img2)