function makepage(fname,papersize,margin,boxsize,ptitle,crange,mrange,... 
                  yrange, varargin);

% usage makepage (fname, papersize, margin, ptitle, cyan, magenta, yellow)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% makepage.m                             %
% written 2/24/00 by ben olding          %
% generates a test page with different,  %
% labeled patches of color in PostScript %
%                                        %
% papersize is in inches                 %
% margin is in inches                    %
% color ranges from 0 to 1               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Requires: pagetitle.m, setfont.m, defbox.m
%           addcell.m, initps.m, al.m

% PS commands used --
% showpage: eject page from printer

global PSText PSCommon

cellsize=[boxsize(1)+0.2 boxsize(2)+0.5];
rndsnp = 1000;

% initialize the postscript file
initps;

% define a path that will make a colored box
defbox;

% resize papersize to usable papersize
papersize=papersize-2*margin;

% place a title on the page
tpoint=20;
pagetitle  (ptitle,tpoint,[papersize(1)+2*margin(1), papersize(2)+margin(2)]);
papersize(2)=papersize(2)-tpoint*1.5/72;

% set the font
setfont(10,'Courier');

% calculate number of patches per page dimension
w=floor(papersize(1)/cellsize(1));
h=floor(papersize(2)/cellsize(2));

% calculate horizontal starting position
sw=margin(1)+(papersize(1)-w*cellsize(1))/1.5;

if isempty(varargin),

  cdiff=crange(2)-crange(1);
  mdiff=mrange(2)-mrange(1);
  ydiff=yrange(2)-yrange(1);

  delta1=floor(sqrt(h*w));
  delta2=floor(h*w/delta1);

  delta1=1/(delta1-1);
  delta2=1/(delta2-1);
  delta3=1/(h*w-1);
  
  i=0;
  r=-delta3;
  for p=0:delta1:1,
    for q=0:delta2:1,
      i=i+1;
      r=r+delta3;
      if cdiff>0,
        if mdiff>0,
          colorspace(i,:)=[cdiff*p+crange(1) mdiff*q+mrange(1)...
			   ydiff*r+yrange(1)];
        else
          colorspace(i,:)=[cdiff*p+crange(1) mrange(1) ydiff*q+yrange(1)];
        end;
      else
        colorspace(i,:)=[crange(1) mdiff*p+mrange(1) ydiff*q+yrange(1)];
      end;
    end;
  end;
else
  colorspace = varargin{1};
end;

colorspace=round(rndsnp*colorspace)/rndsnp;

p=0;
q=1;
for i=1:size(colorspace,1),
  p=p+1;
  if p>w,
    p=1;
    q=q+1;
  end;
  addcell (colorspace(i,:),...
    [sw+cellsize(1)*(p-1), papersize(2)+margin(2)-cellsize(2)*q],...
    boxsize(1),boxsize(2));
end;

al('showpage');
fid=fopen([fname,'.ps'],'w');
fclose(fid);
more off;
spc = repmat (' ',size(PSText,1),1);
diary ([fname,'.ps']);
disp ([PSText spc PSComment]);
diary off;

save (fname,'colorspace');

