Help with If, Elseif and modifying traces

I am trying to write a procedure that takes a plot and modifies it such that the color of the trace mimics what the human eye would see. I know this is not easy, but I used the simplified procedure at: http://miguelmoreno.net/sandbox/wavelengthtoRGB/ to get the basic code. What I can't seem to do is get the red green and blue values to be determined in a long series of if, elseif statements. What I try to do is this (although I am still learning)

Determine what traces are in a window
determine there max value
determine what wavelength that is at
if they are plotted against another wave (wavelength for example) then from the max point determine wavelength from XWave
Then take that wavelength and determine red green and blue values to
modify the trace

Right now there are three plots only one of which is plotted versus another wave, but I can't get the RGB colors to work... After I figure out the wavelength I have the long series of if, elseif statements and at the end it won't even print the Red Blue and Green values, let alone modify the graph. Would anyone mind taking a glance and telling me if i made an obvious mistake? I have attached the experiment, any help would be appreciated. Sorry if the procedure is a mess.

Cheers,
Anthony


PS I'm not sure how to include data or procedures so it is below..

Data:
•make/n=1000 Test Test2 Test3
•Test=gauss(p,500,100)
•Display Test
•SetScale/I x 300,800,"", Test
•SetScale/I x 300,800,"", Test2
•Test2=gauss(p,660,100)
•AppendToGraph Test2
•ShowInfo
•Test3=gauss(p,440,100)
•make/n=1000 Wavelength
•Wavelength=300+0.5*p
•AppendToGraph Test3 vs Wavelength

Procedure:
#pragma rtGlobals=1 // Use modern global access method.
Function WavelengthToColor()

string TraceNames
TraceNames=TraceNameList("",";",1)

variable NumTraces
NumTraces=ItemsinList(TraceNames)


variable i
for (i=0;i
String XWave
String YString=StringFromList(i, Tracenames)

WaveStats/Q $Ystring
Variable PntMax=V_Maxloc

print YString
Print PntMax

XWave=XWaveName("", YString)
variable DoesXWaveExist=strlen(Xwave)

if(DoesXWaveExist!=0)
duplicate/o $xwave Dummywave
Variable PntAtWl=DummyWave[PntMax]

Variable Blue
Variable Green
Variable Red

variable WL=pntatwl

Print WL

if(WL>= 350 && WL <= 439)
Red=-(WL - 440)/(440-350)
Green=0.0
Blue=1.0
elseif(WL>= 440 && WL<=489)
Red=0.0
Green=(WL-440)/(490-440)
Blue=1.0
elseif(WL>=490 && WL<=509)
Red=0.0
Green=1.0
Blue=-(WL-510)/(510-490)
elseif(WL>=510 && WL<=579)
Red=(WL-510)/(580-510)
Green=1.0
Blue=0.0
elseif(WL>=580 && WL<=644)
Red=1.0
Green=-(WL-645)/(645-580)
Blue = 0.0
elseif(WL>=645 && WL<=780)
Red=1.0
Green =0.0
Blue=0.0
else
Red=0.0
Green=0.0
Blue=0.0

Red*=65535
Green*=65535
Blue*=65535

Wave RGBVal
make/n=3 RGBVal={Red,Green,Blue}


print Red
print Green
print Blue
modifygraph rgb($Ystring)=(RGBval[0],RGBval[1],RGBval[2])
endif
endif
endfor

end
Sorry, the procedure was not complete, I am not sure why, here it is:

Function WavelengthToColor()

string TraceNames
TraceNames=TraceNameList("",";",1)

variable NumTraces
NumTraces=ItemsinList(TraceNames)


variable i
for (i=0;i
String XWave
String YString=StringFromList(i, Tracenames)

WaveStats/Q $Ystring
Variable PntMax=V_Maxloc

print YString
Print PntMax

XWave=XWaveName("", YString)
variable DoesXWaveExist=strlen(Xwave)

if(DoesXWaveExist!=0)
duplicate/o $xwave Dummywave
Variable PntAtWl=DummyWave[PntMax]

Variable Blue
Variable Green
Variable Red

variable WL=pntatwl

Print WL

if(WL>= 350 && WL <= 439)
Red=-(WL - 440)/(440-350)
Green=0.0
Blue=1.0
elseif(WL>= 440 && WL<=489)
Red=0.0
Green=(WL-440)/(490-440)
Blue=1.0
elseif(WL>=490 && WL<=509)
Red=0.0
Green=1.0
Blue=-(WL-510)/(510-490)
elseif(WL>=510 && WL<=579)
Red=(WL-510)/(580-510)
Green=1.0
Blue=0.0
elseif(WL>=580 && WL<=644)
Red=1.0
Green=-(WL-645)/(645-580)
Blue = 0.0
elseif(WL>=645 && WL<=780)
Red=1.0
Green =0.0
Blue=0.0
else
Red=0.0
Green=0.0
Blue=0.0

Red*=65535
Green*=65535
Blue*=65535

Wave RGBVal
make/n=3 RGBVal={Red,Green,Blue}


print Red
print Green
print Blue
modifygraph rgb($Ystring)=(RGBval[0],RGBval[1],RGBval[2])
endif
endif
endfor

end
It looks like there a few problems with your function, but there is a much easier way to do this in Igor, so I'll talk about that. If you still want help with your function please let me know.

Also, when you're posting Igor code on this site, it's easiest if you surround it by <igor></igor> tags so that the code will be highlighted just as it is in an Igor procedure window.

So basically the code you wrote (based on what you found) translates a wavelength, in nm, to red, green, and blue values which represent the appearance of that color.

Igor has many built in color tables that are lists of colors used for specific purposes. Two of these relate to the visible light spectrum, Spectrum and SpectrumBlack. You don't say what version of Igor you are using but both of these tables have been available since Igor 5, so you likely have them in your version.

For more information on these and the other color tables, from the Igor command line execute the following command:

DisplayHelpTopic "Color Table Details"

What you want to do is to store the values in the color table into a wave so that you can access them. Assuming you want to use the Spectrum color table, you use the following command:

ColorTab2Wave Spectrum

This command will create a wave named M_Colors that is a 2D wave with a few hundred rows and 3 columns, one each for red, green, and blue (and in that order). You next need to scale this wave so that Igor knows how the wavelength of the color relates to the data in the wave. To do that, execute:

SetScale/P x, 380, 2, "nm", M_Colors

Then, if you need to know the RGB values at, say, 400 nm, you could print them like this:

print m_colors(400)[0], m_colors(400)[1], m_colors(400)[2]

Does this help you get what you want? If not let me know and I'll help you more.