pythoncolorsrgb

Converting multiple wavelengths to a single RGB value using the colour-science package


I'm working with a dataset that contains reflectance factor (Rf) values and their corresponding wavelengths, like this:

Rf          Wavelength
0.3907051   190
0.3907051   191
0.3907051   192
...
0.3907051   200

I am trying to convert the multiple wavelengths into a single RGB value using the colour-science package, but I’m having trouble finding relevant documentation on how to do this. How can I convert these wavelength-Rf pairs into a single RGB value?


Solution

  • https://colour.readthedocs.io/en/latest/tutorial.html should give you a good starting point. I do not know this package, but what I have extracted from the tutorial (links to the relevant sections shown in the comments):

    enter image description here

    import colour
    
    # define the spectral distribution as a dict
    # https://colour.readthedocs.io/en/latest/tutorial.html#from-spectral-distribution
    data_sample = {
        380: 0.048,
        385: 0.051,
    # [...]
        775: 0.432,
        780: 0.421,
    }
    sd = colour.SpectralDistribution(data_sample)
    colour.plotting.plot_single_sd(sd)
    
    # Calculate the tristimulus values from the spectral data
    # https://colour.readthedocs.io/en/latest/tutorial.html#convert-to-tristimulus-values
    cmfs = colour.MSDS_CMFS["CIE 1931 2 Degree Standard Observer"]
    illuminant = colour.SDS_ILLUMINANTS["D65"]
    
    XYZ = colour.sd_to_XYZ(sd, cmfs, illuminant)
    
    # Convert to color
    # https://colour.readthedocs.io/en/latest/tutorial.html#convert-to-display-colours
    RGB = colour.XYZ_to_sRGB(XYZ / 100)
    colour.plotting.plot_single_colour_swatch(colour.plotting.ColourSwatch(RGB, "Sample"), text_kwargs={"size": "x-large"})