MATLAB: How to convert light spectral / intensity to RGB value

colorImage Processing Toolboxlightrgbrgb from spectrumspectral

dear all,
how do we convert light spectral / intensity to RGB value?
below is two spectral example., and i expect to have them having two different RGB value.
thanks
regards

Best Answer

Well I assume that you want to convert the envelope of the coloured region into a single RGB-tripplet. In your particular case I'd simply try to determine the coordinates of the curve, let's call it I(lambda). Then I'd get the corresponding RGB-values, just above the wavelength-axis, that would give us the RGB-values at each wavelength, in an [n_lambda x 3] array. The total contributions to the R, G and B channels would then be the sum of the product of the respective intensities. You might have to scale those to be between 0 and 1 (or 0 and 255) for display-purposes.
I_of_lambda1 = % Your best estimate of the first curve
RGB = double(squeeze(IM(idx_xaxis+1,:,:)));
% make sure they have the same lengths.
Rtot = sum(I_of_lambda(:).*RGB(:,1));
Gtot = sum(I_of_lambda(:).*RGB(:,2));
Btot = sum(I_of_lambda(:).*RGB(:,3));
Then repeat procedure for second curve.
For more detailed discussion about spectral response and RGB-representations, take a look at Steve Eddins description: Making Color Spectrum Plots – Part 2
HTH