function map = rainbow(n) 
% map = rainbow(n)
% Create a rainbow color colormap. 
% This colormap ranges from red through orange, yellow, green, blue, to violet.
%
% The argument n must be a scalar.
% If unspecified, the length of the current colormap, or 64, is used.

%!demo
%! %% Show the 'rainbow' colormap as an image
%! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
%! axis ([1, 64, 0, 1], 'ticy', 'xy');
%! colormap (rainbow (64));

  map = zeros (0, 3);

  if (n == 1)
    map = [1, 0, 0];
  end
  if (n > 1)
    x = [0:(n-1)]' / (n - 1);

    r = (  (x <  2/5) ...
         + (x >= 2/5 & x < 3/5) .* (-5 * x + 3) ...
         + (x >= 4/5) .* (10/3 * x - 8/3) );

    g = (  (x <  2/5) .* (5/2 * x) ...
         + (x >= 2/5 & x < 3/5) ...
         + (x >= 3/5 & x < 4/5) .* (-5 * x + 4) );

    b = (  (x >= 3/5 & x < 4/5) .* ( 5 * x - 3) ...
         + (x >= 4/5) );

    map = [ r, g, b ];
  end

end % =============================================== function rainbow

