How to convert spectrum to RGB color in maxscript?


#1

Hi guys!

I need to get the color from the spectrum format (wave length and wave width) in RGB format.
Suppose I get the following data in the spectrum:
length = 400
width = 150
How to get the RGB values corresponding to this data?

ps: meaning the implementation of this solution in maxscript

Thank!


#2

following to this article: https://academo.org/demos/wavelength-to-colour-relationship/

mxs function might look as:

fn wavelengthToRGB wavelength round:off = 
(
	fn iround v = int (floor (if v >= 0 then v + 0.5 else v - 0.5))
	
	wavelength = wavelength as float
	
	local gamma = 0.80
	local factor, r, g, b
	
	if ((wavelength >= 380) and (wavelength < 440)) then
	(
		r = -(wavelength - 440) / (440 - 380)
		g = 0.0
		b = 1.0
	)
	else if ((wavelength >= 440) and (wavelength < 490)) then
	(
		r = 0.0
		g = (wavelength - 440) / (490 - 440)
		b = 1.0
	)
	else if ((wavelength >= 490) and (wavelength < 510)) then
	(
		r = 0.0
		g = 1.0
		b = -(wavelength - 510) / (510 - 490)
	)
	else if ((wavelength >= 510) and (wavelength < 580)) then
	(
		r = (wavelength - 510) / (580 - 510)
		g = 1.0
		b = 0.0
	)
	else if ((wavelength >= 580) and (wavelength < 645)) then
	(
		r = 1.0
		g = -(wavelength - 645) / (645 - 580)
		b = 0.0
	)
	else if ((wavelength >= 645) and (wavelength < 781)) then
	(
		r = 1.0
		g = 0.0
		b = 0.0
	)
	else
	(
		r = 0.0
		g = 0.0
		b = 0.0
	)
	
	-- Let the intensity fall off near the vision limits
	
	if ((wavelength >= 380) and (wavelength < 420)) then
	(
		factor = 0.3 + 0.7 * (wavelength - 380) / (420 - 380)
	)
	else if ((wavelength >= 420) and (wavelength < 701)) then
	(
		factor = 1.0
	)
	else if ((wavelength >= 701) and (wavelength < 781)) then
	(
		factor = 0.3 + 0.7 * (780 - wavelength) / (780 - 700)
	)
	else
	(
		factor = 0.0
	)
	
	r = pow (r * factor) gamma
	g = pow (g * factor) gamma
	b = pow (b * factor) gamma
	
	if round then
	(
		r = iround (255 * r)
		g = iround (255 * g)
		b = iround (255 * b)
	
		[r,g,b] as color
	)
	else ([r,g,b] * 255) as color
)

i don’t know how to use wave width here… by reading a theory i couldn’t find a place for width. more likely it might be used for intensity (lightness)


#3

Here are other two resources:
http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
http://www.midnightkite.com/color.html


#4

Thank you very much!
I will try to figure it out, but maybe there will be new questions.
In your code, the minimum value of wave length is 380, and the maximum is 780, but in my case the minimum value of the received data is 350 and maximum 750. Probably, therefore, the visible result after conversion is very different from the original (
I will understand further …
PS: Yes, wave width affects the intensity of the color, but I also don’t understand how to apply this value in this case.