function colpal=colorpal(col,ncol,xlohi)
% function colpal=colorpal(col,ncol,xlohi)
% b klinger, 5/2/15
% Define colormap vector based on some color choices at colorbrew2.org
% col=string; set col='H' for help (must define ncol & dograph also)

%           MODIFICATIONS
% 5/18/15 use upper/lower col to encode direction of colors, not ncol

% default values of ncol and dograph

if ~exist('ncol')
   ncol=5*ones(1,length(col)+1);
end

% ncol gives info on both number of shades & direction of
% progression for each color

jdir=(col==upper(col));
col=upper(col);
Ndir=length(ncol);
dograph= length(ncol) < length(col);

if ~exist('xlohi')
   xlohi=[zeros(Ndir,1) ones(Ndir,1)];
end   

%---------------------------------------------------------------------
% Define colors from http://colorbrew2.org numbers.
%---------------------------------------------------------------------

% Blue

brewB=[247,251,255
222,235,247
198,219,239
158,202,225
107,174,214
66,146,198
33,113,181
8,81,156
8,48,107];

% Green

brewG=[247,252,245
229,245,224
199,233,192
161,217,155
116,196,118
65,171,93
35,139,69
0,109,44
0,68,27];

% Red

brewR=[255,245,240
254,224,210
252,187,161
252,146,114
251,106,74
239,59,44
203,24,29
165,15,21
103,0,13];

% Orange from colorbrewer2.org (not used)

brewO=[255,245,235
254,230,206
253,208,162
253,174,107
253,141,60
241,105,19
217,72,1
166,54,3
127,39,4];

% home-made Orange w/lighter colors

brewO(1,:)=[1 1 .8];
brewO(2,:)=[1 1 .5];
jO=[64 58 51 43 33 23 11];  % place in matlab autumn palette 
brewO=[1 1    .8
       1 1    .5
       1 1     0
       1 .9048 0
       1 .7937 0
       1 .6667 0
       1 .5079 0
       1 .3492 0
       1 .1587 0];
brewO(9,:)=brewO(9,:)*.8;
brewO=round(brewO*255);

% grAy, browN, 

brewA=[255,255,255
240,240,240
217,217,217
189,189,189
150,150,150
115,115,115
82,82,82
37,37,37
0,0,0];

% browN, 

brewN=[255,255,229
255,247,188
254,227,145
254,196,79
254,153,41
236,112,20
204,76,2
153,52,4
 68,25,4];

%---------------------------------------------------------------------
% start graphics & define coords for rectangles to display colors
%---------------------------------------------------------------------

if dograph | col(1)=='H'
   if upper(col(length(col)))~='K'
      hold off; clf; top
   end
   rx=[0 1 1 0];
   ry=[0 0 1 1];
end

%---------------------------------------------------------------------
if col(1)=='H'     % help option
%---------------------------------------------------------------------

% in matlab window, display help info 

disp('function colpal=colorpal(col,ncol,xlohi)')
txt='(B=blue,R=red,G=green,O=orange,A=gray,N=brown)';
disp(['Colors: string col ' txt])
disp('(lower case=dark-->light, upper case=light-->dark)')
disp('Can use more than one string (ex: "BR" = blues, than reds).')
disp('To display map colors, put an additional letter at end of col')
disp('ncol=number of shades of each color')
disp('Include [xlo1 ylo1; xlo2 ylo2 etc] to alter range of shades.')
disp('( 0 <= xlo < xhi <= 1 ).')

% in graphics window, show colors w/ 9 shades each

   thecol='BRGOAN';
   for j=1:length(thecol)
      eval(['brew=brew' thecol(j) '/255;']);
      for i=1:9
         fill(rx+i-1,ry+j,brew(i,:))
         hold on
      end
      text(.5,j+.5,thecol(j))
   end
   axis([0 9 1 7])
   brewmap=0;

%---------------------------------------------------------------------
else    % main option
%---------------------------------------------------------------------

% loop through colors, assigning shades to each & concatenating
% each set of values to previous colormap

   i0=0;
   for iD=1:Ndir
      eval(['brew=brew' col(iD) '/255;'])
      if jdir(iD)
         cc=[0:ncol(iD)-1]/(ncol(iD)-1);
      else
	cc=[ncol(iD)-1:-1:0]/(ncol(iD)-1);
      end
      xlo=xlohi(iD,1);
      xhi=xlohi(iD,2);
      imap=1+(xlo+(xhi-xlo)*cc)*8;
      ii=1:ncol(iD);
      colpal(ii+i0,1)=interp1(1:9,brew(:,1),imap);
      colpal(ii+i0,2)=interp1(1:9,brew(:,2),imap);
      colpal(ii+i0,3)=interp1(1:9,brew(:,3),imap);
      i0=i0+ncol(iD);
   end

% display colors

   if dograph
      for i=1:i0
          fill(rx+i-1,ry,colpal(i,:));
          hold on
      end
   end
      
%---------------------------------------------------------------------
end         % end if col(1)...
%---------------------------------------------------------------------
