www.pudn.com > 精通Matlab综合辅导与指南-源程序.zip > rainbow.m


function map=rainbow(m)
%RAINBOW Colormap Variant to HSV.
% RAINBOW(M) Rainbow Colormap with M entries.
%
% Red - Orange - Yellow - Green - Blue - Violet
%
% RAINBOW by itself is the same length as the current colormap.
%
% Apply using: colormap(rainbow)

% D. Hanselman, University of Maine, Orono, ME  04469
% 3/23/95
% Copyright (c) 1996 by Prentice Hall, Inc.

if nargin<1, m=size(get(gcf,'colormap'),1); end

mp=[
 1   0  0  % red
 1  1/3 0  % orange
 1   1  0  % yellow
 0   1  0  % green
 0   0  1  % blue
.5   0 .5];% violet

if m<=6,map=mp(1:m,:);return,end

k=ceil(m/5);
mk=5*k+1;
map=zeros(mk,3);
ks=(0:4)*k +1;

for i=0:k-1
	a=(1+cos(pi*i/k))/2;b=1-a;	% cosine interp to maximize rainbow colors
	map(i+ks,:)=a*mp(1:5,:) + b*mp(2:6,:);
end
map(mk,:)=mp(6,:);
map=map(1:m,:);