% find the gain for red and blue
% check the peak green area

sr = 1;
sb = 1;

% if blue is too high
if tmb > mg 
   sb = mg/tmb;
% if blue is too low
else
   if mb < mg	
      sb = mg/mb;
   end
end

% if red is too high
if tmr > mg
   sr = mg/tmr;
% if red is too low
else 
   if mr < mg
      sr = mg/mr;
   end
end

% scale the red & blue colors
nr = (r*sr);
nb = (b*sb);

% map the highest green value to 255
scale = 255/mg;
nr = nr*scale;
nb = nb*scale;
ng = g*scale

% prevent saturation
for j=1:514
   for i=1:482
      if nr(i,j) > 255
         nr(i,j)=255;
      end
      if nb(i,j) > 255
         nb(i,j)=255;
      end
      if ng(i,j) > 255
         ng(i,j)=255;
      end
   end
end

% construct new color matrix 
newim(:,:,1)=nb;
newim(:,:,2)=ng;
newim(:,:,3)=nr;
