Coloring a "fit_" wave the same as the data wave

I have generated a function to color the waves on my top graph in a rainbow gradient, but if I run the function after I have plotted the fit_ waves, they are included in the gradient and are different colors from the data they are fitted to.

I have played around with using less direct methods to color the fit_ waves to be the same as the data, but it is not quite what I want and there are several instances where I could see it failing. 

Is there a way to have my function to color the wave name, then do an if/then if there is another wave on the graph that contains the "fit_" appended?

 

Below is my coloring function and how it is set up for reference. In order to get this version to function, I have to interlace the wave order so the the fit waves followed each data wave. The last two lines are what allow this to work.


Function GradrainbowFits() : GraphStyle
//Makes rainbow gradient between roy and biv (with a lil g in between)
//Input GradRainbow() for example
	GetWindow kwTopWin, title

	ModifyGraph/Z mirror=2
	ModifyGraph/Z lSmooth=1,lHair=1,minor(bottom)=0
	ModifyGraph/Z lsize=2
	Variable i,r,g,b
	Variable j =  ItemsInList(TraceNameList(S_value,";",1))-1

   Make/O/D red = {65535,0,0}
   Make/O/D orange = {65535,42405,0}
   Make/O/D yellow = {65535,55255,0}
   Make/O/D green = {0,51120,0}
   Make/O/D blue = {0,0,65535}
   Make/O/D indigo = {19199,0,33279}
   Make/O/D violet = {38036,0,54227}


   Make/O/D w = {0,0,0}
   wave c1 = red
   wave c2 = orange
   wave c3 = yellow
   wave c4 = green
   wave c5 = blue
   wave c6 = indigo
   wave c7 = violet

	for(i=0;i<=j;i+=1)
		if(i<(1/6*j))
			w = c1-(c1-c2)*(i/(j/6))
			endif
		if(i==(1/6*j))
			w = c2
			endif
		if(i>(1/6*j))
			if(i<(2/6*j))
				w = c2-(c2-c3)*((i-j/6)/(j/6))
				endif
			endif
		if(i==(2/6*j))
			w = c3
			endif

		if(i>(2/6*j))
			if(i<(3/6*j))
				w = c3-(c3-c4)*((i-2*j/6)/(j/6))
				endif
			endif

		if(i==(3/6*j))
			w = c4
			endif
		if(i>(3/6*j))
			if(i<(4/6*j))
				w = c4-(c4-c5)*((i-3*j/6)/(j/6))
				endif
			endif

		if(i==(4/6*j))
			w = c5
			endif
		if(i>(4/6*j))
			if(i<(5/6*j))
				w = c5-(c5-c6)*((i-4*j/6)/(j/6))
				endif
			endif

		if(i==(5/6*j))
			w = c6
			endif
		if(i>(5/6*j))
			if(i<(6/6*j))
				w = c6-(c6-c7)*((i-5*j/6)/(j/6))
				endif
			endif

		if(i==(6/6*j))
			w = c7
			endif

		r = w[0]
		g = w[1]
		b = w[2]
		ModifyGraph/Z rgb[i]=(r,g,b)
		ModifyGraph/Z rgb[i+1]=(r,g,b)
		i=i+1
	endfor
	
End

 

You could pre-process the list of traces to remove all the traces that start with "fit_". Then, as you iterate through the list, color the current trace (with a given name) as desired. Then test for the existence of a trace with the name "fit_"+name. If it exists, color it, too.

To create the filtering function for the "fit_" waves, use stringmatch("fit_*", tracename) to test for a match.

Here is an excerpt from my Graph Tools package to get a filtered list of traces from a graph:

function/S FetchTraces(string win, string filter, int inverse)
	string tempList = "", traces = TraceNameList(win,";",1)
	int i, items = ItemsInList(filter)
	if (strlen(filter) && CmpStr(filter,"*"))
		if (inverse)
			for (i = 0; i < items; i++)
				traces = RemoveFromList(ListMatch(traces,StringFromList(i,filter)),traces)
			endfor
		else
			for (i = 0; i < items; i++)
				tempList += ListMatch(traces,StringFromList(i,filter))
			endfor
			traces = tempList
		endif
	endif
	return traces
end

The 'inverse' parameter decides if you want to include or omit the filtered traces.

Try this:

print FetchTraces("","fit_*",0)
print FetchTraces("","fit_*",1)

The 'filter' string can also be a semicolon-separated list of filter conditions, such as "fit_*;*data1*;".

By the way, if you want to use a convenient panel for your coloring needs, you may want to have a look at my Graph Tools package:

https://www.wavemetrics.com/node/21562

Also, if you want to have 'fit_' traces recolored while quickly fitting data, this is supported in my Super Quick Fit package, where you can choose from a few coloring options (I also can add more if needed):

https://www.wavemetrics.com/node/21866