Skip to content

Instantly share code, notes, and snippets.

@vishalvashistha87
Created August 14, 2016 21:08
Show Gist options
  • Save vishalvashistha87/c5c6852b64ff6cb63280aa18220da546 to your computer and use it in GitHub Desktop.
Save vishalvashistha87/c5c6852b64ff6cb63280aa18220da546 to your computer and use it in GitHub Desktop.
from colour.plotting import *
import colour
from pprint import pprint
import colour.plotting
from pprint import pprint
import colour.colorimetry as colorimetry
import colour.colorimetry.dataset as dataset
# Defining a sample spectral power distribution data.
sample_spd_data = {
406: 0.356748826,
420: 0.433866837,
436: 0.442941741,
449: 0.382922535,
457: 0.29693662,
464: 0.213380282,
470: 0.136663732,
480: 0.039276569,
491: 0.083054577,
497: 0.173635563,
501: 0.262676056,
506: 0.34278169,
513: 0.429295775,
520: 0.515070423,
528: 0.598943662,
541: 0.67956146,
556: 0.740396927,
572: 0.77299936,
587: 0.80459347,
603: 0.822071063,
619: 0.829129321,
634: 0.831482074,
650: 0.840556978,
666: 0.855345711,
681: 0.862403969,
697: 0.867445583,
713: 0.868453905,
728: 0.86778169,
742: 0.866549296}
spd = colour.SpectralPowerDistribution('Sample', sample_spd_data)
print(spd)
# Cloning the sample spectral power distribution.
clone_spd = spd.clone()
# Interpolating the cloned sample spectral power distribution.
# Methos should be spline.
clone_spd.interpolate(colour.SpectralShape(406, 742, 1),method='spline')
# Check the value at 407 nm.
clone_spd[407]
# Extrapolating the cloned sample spectral power distribution.
clone_spd.extrapolate(colour.SpectralShape(340, 830))
clone_spd[340], clone_spd[830]
# Make sample data as clone data.
spd = colour.SpectralPowerDistribution('Sample', spd)
cmfs = colour.STANDARD_OBSERVERS_CMFS['CIE 1931 2 Degree Standard Observer']
illuminant = colour.ILLUMINANTS_RELATIVE_SPDS['D65']
# Calculating the sample spectral power distribution *CIE XYZ* tristimulus values.
XYZ = colour.spectral_to_XYZ(spd, cmfs, illuminant)
print(XYZ)
# The output domain of *colour.spectral_to_XYZ* is [0, 100] and the input
# domain of *colour.XYZ_to_sRGB* is [0, 1]. We need to take it in account and
# rescale the input *CIE XYZ* colourspace matrix.
RGB = colour.XYZ_to_sRGB(XYZ / 100)
print(RGB)
# Plotting the *sRGB* colourspace colour of the *Sample* spectral power distribution.
single_colour_plot(ColourParameter('Sample', RGB), text_size=32)
# Computing *xy* chromaticity coordinates for the *neutral 5 (.70 D)* patch.
xy = colour.XYZ_to_xy(XYZ)
print(xy)
import pylab
# Plotting the *CIE 1931 Chromaticity Diagram*.
# The argument *standalone=False* is passed so that the plot doesn't get displayed
# and can be used as a basis for other plots.
CIE_1931_chromaticity_diagram_plot(standalone=False)
# Plotting the *xy* chromaticity coordinates.
x, y = xy
pylab.plot(x, y, 'o-', color='white')
# Annotating the plot.
pylab.annotate(spd.name.title(),
xy=xy,
xytext=(-50, 30),
textcoords='offset points',
arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=-0.2'))
# Displaying the plot.
display(standalone=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment