function h=colortime(x,t,Y,rgb1, rgb2);
%COLORTIME.m--for each t(j), plots Y(x,t(j)) on a colorscale
%
%h=colortime(x,t,Y,rgb1, rgb2);
%
%Plots each column of a matrix of data Y in a different color.
%The colors will range from rgb1 to rgb2 (length 3 vectors 
%specifying RGB values) s.t. Y(:,1) is plotted with rgb1
%and Y(:,end) is plotted with rgb2.   The vectors x and
%t are the labels for Y:
%
%				x = length m--positions along x axis
%           t = length n--column labels for Y
%           Y = m-by-n  --the data.  Y(j,k)  is the observation
%                         corresponding to x(j) and t(k)
%

%1) Check that sizes are correct
[m,n]=size(Y);
x=x(:);%make sure x is a column vector
p=length(x);
if(p~=m)
	error('x should have the same number of rows as Y');
end
t=t(:)';%make sure t is a row vector
p=length(t);
if(p~=n)
	error('t should have the same number of columns as Y');
end
rgb1=rgb1(:)';
rgb2=rgb2(:)';
if(length(rgb1)~=3 | length(rgb2)~=3)
	error('Color vectors rgb1 and rgb2 should have 3 entries for RGB values');
end
tmp=[rgb1,rgb2];
if(max(tmp)>1 | min(tmp)<0)
	error('Matlab RGB values are between 0 and 1');
end

%2) Plot the data and let Matlab choose the colors
%   Be sure to capture handles

h=plot(x,Y);

%3) Construct RGB vector for each t(j) according to 
%   a linear function of t

RGB=zeros(3,n);

tmax=max(t);
tmin=min(t);
%dt=tmax-tmin;
%for j=1:3;
%   %RGB(:,j)=m*t+b
%   m=(rgb2(j)-rgb1(j))/dt;
%   b=rgb2(j)-m*tmax;
%   RGB(j,:)=m*t+b;
%end
RGB=interp1([tmin;tmax],[rgb1;rgb2],t);

for j=1:n
	set(h(j),'color',RGB(j,:));
end
