File Exchange

image thumbnail

Spectral and XYZ Color Functions

version (33.3 KB) by Jeff Mather
Label plots with spectra. Convert wavelengths to RGB. Functions for color matching and illuminants.


Updated 08 Nov 2010

View License

This is the beginning of a set of functions for working with color as wavelengths (spectral color) and in the xyY and XYZ color spaces.

Current functionality includes:

spectrumLabel - Add a color bar containing the visible spectrum to a plot's X axis.

spectrumRGB - Converts wavelengths of light to equivalent RGB values (in the sRGB space).

colorMatchFcn - Raw data of popular color matching functions (spectra to XYZ) for various standard observers:
* CIE 1931 2-degree
* CIE 1964 10-degree
* CIE 1931 2-degree modified by Judd (1951)
* CIE 1931 2-degree modified by Judd (1951) and Vos (1978)
* Stiles and Burch 2-degree (1955)
* Stiles and Burch 10-degree (1959)

illuminant - Raw data for the A, D65, and E standard illuminants.

Cite As

Jeff Mather (2020). Spectral and XYZ Color Functions (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (31)

Yunhui Hou

Thumbs up even just for the color matching functions.

Sascha Hein

What does this mean and what information does it want? hFig = ancestor(hAxesTarget, 'figure');
When I try to run this function it tells me there is an error in line 32 which is this one above...

How can we adjust the colorbar to the actual X axis wavelength range?
By using spectrumLabel(gca) I get a colorbar in the range 300-800nm whereas I need it go up to 100nm and properly scale the width of the visible range with respect to the actual axis width. Thanks

Xinyi Tu

how to use this? did not understand this program


spectrumLabel is just what I was looking for: added a visible color axis to an x-axis in nm.

jakob aaes

Well it looks great on the pictures, but without a proper example it really is more or less useless... Please upload an example. Anybody!

Give some examples files man, so somebody can used it.

Give some examples files man, so somebody can used it.

How to use these functions? I have wavelength and intensity data.

I'm surprised that you don't obtain a unique RGB value for a given wavelength, as your RGB functions are truncated, saturated at 1. For example, in the range 512-548 nm you have the same RGB value: a pure green 0-1-0. Also in the range 611-641 you give constant pure red 1-0-0. I guess the eye should perceive a difference along those ranges, so the results are not realistic. Correct?

Tim Zaman


Dear Jeff Mather,
can you please provide/indicate the reference (paper, scientific publication) in which these algorithms have been based?

Thank you very much in advance!


very help ful,excellent work!

Very helpful!
Thank you.


I'm trying to use spectrumLabel for a calculated absorption spectrum.
It works fine from the command window, but I'm not able to put the color axis in a plot which is rendered to a GUI.
Here is the piece of code that I put in the .m file. The plot of the extinction, scattering and absorption spectra are sent to the axes style GUI handles.axes1:
xlabel('Wavelength (nm)');
ylabel('Arbitrary units');
Apparently, the spectrumLabel is put at the right position but with zero or very small vertical dimension.
Thank you!


I got this error information:
"the selected cell cannot be evaluated because it contains an invalid statement".
"??? Input argument "formulary" is undefined."
Any idea why this happen? Thank you!


but there seems to be a scale off. I don't know how to solve that. Please help!


Thanks to this excellent work, I've been able to image a spectrum plot with both the characteristic peaks marked in colors and intensity plot shown. The code is as following.

function SpecRGB_Intensity(wavelength,intensity, peak_threshold)
% wavelength and intensity are vectors
% the characteristic peaks are chosen if the value of the peak
% is bigger than the peak_threshold.
% by springface

sRGB = spectrumRGB(wavelength);

%% Transform spectrum plot into an image, marked by colors
len_wave = length(wavelength);
len_inten = 1000;
intensity_plot = max(intensity):-max(intensity)/(len_inten-1):0;
wavelength_plot = wavelength;

intensity_num = ones(size(intensity));

for cc = 1: len_wave
if intensity(cc)>=0
[C,I] = min(abs(intensity(cc)- intensity_plot));
intensity_num(cc) = len_inten-I;

im_plot = ones(len_inten,len_wave,3);

for cc = 1: len_wave
im_plot(1:intensity_num(cc),cc,:) = repmat(sRGB(1,cc,:),intensity_num(cc),1);

%% mark the non-peak area as black
spec_idx = intensity < peak_threshold;
sRGB(1,spec_idx,:) = zeros(1,sum(spec_idx),3);

%% Plot figures

whitegap_len = 2;
RGBspec_len = 100;

RGBspec_range = max(intensity)+whitegap_len+RGBspec_len+1:max(intensity)+1;


Great submission!

how do I get the spectrumon jpg?

Franck Riberi

Need demo files absolutely.

Neeraj Adsul

jeongwoon lee

NOne None


Vishnuvenkatesh Dhage

ben taylor

Does anyone know hot to convert it the other way? I have an RGB image of black body radiation and I am writing a program that will give me a 3d temperature plot, but I need to convert from RGB to wavelength to temperature.


Levent Onural

Thanks a lot, I have been looking for how I could generate the matching functions for 5 hours. Excellent piece of work...

marwan reyadh

please how can i convert an image from RGB of size n*n*3 to an image with of size n*n*n

colour rainbow

could you able to create function for r2xyz
for 1xm or 1xn matrix

Wu John

Good job! Thank you,but how about the other light source in illuminate.m?


BSD License

MATLAB Release Compatibility
Created with R14SP1
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Perceptually improved colormaps