function newmap = b2r(cmin_input,cmax_input,ColorNum)
%BLUEWHITERED   Blue, white, and red color map.
%   this matlab file is designed to draw anomaly figures, the color of
%   the colorbar is from blue to white and then to red, corresponding to 
%   the anomaly values from negative to zero to positive, respectively. 
%   The color white always correspondes to value zero. 
%   
%   You should input two values like caxis in matlab, that is the min and
%   the max value of color values designed.  e.g. colormap(b2r(-3,5))
%   
%   the brightness of blue and red will change according to your setting,
%   so that the brightness of the color corresponded to the color of his
%   opposite number
%   e.g. colormap(b2r(-3,6))   is from light blue to deep red
%   e.g. colormap(b2r(-3,3))   is from deep blue to deep red
%
%   I'd advise you to use colorbar first to make sure the caxis' cmax and cmin
%
%   by Cunjie Zhang
%   2011-3-14
%   find bugs ====> email : daisy19880411@126.com
%  
%   Examples:
%   ------------------------------
%   figure
%   peaks;
%   colormap(b2r(-6,8)), colorbar, title('b2r')
%   colormap(cmap_white_at_zero(-6,8));
%   


%% check the input
if nargin ~= 3 ;
   disp('input error');
   disp('input two variables, the range of caxis , for example : colormap(b2r(-3,3))')
end

if cmin_input >= cmax_input
    disp('input error')
    disp('the color range must be from a smaller one to a larger one')
end

%% control the figure caxis 
lims = get(gca, 'CLim');   % get figure caxis formation
caxis([cmin_input cmax_input])

%% color configuration : from blue to light blue to white untill to red

% CMAP COLORMAP
blue_bottom  = [0 110 230]/255; % For 
white_middle = [255 255 255]/255; % For 
red_top = [210 40 20]/255; % For 
% blue_bottom  = [0 80 255]/255; % For 
% white_middle = [255 255 255]/255; % For 
% red_top = [230 20 0]/255; % For 


%% color interpolation 
color_num = ColorNum;   
color_input = [blue_bottom;  white_middle;  red_top];
oldsteps = linspace(-1, 1, length(color_input));
newsteps = linspace(-1, 1, color_num);  

%% Category Discussion according to the cmin and cmax input

%  the color data will be remapped to color range from -max(abs(cmin_input,abs(cmax_input)))
%  to max(abs(cmin_input,abs(cmax_input))) , and then squeeze the color
%  data in order to make suere the blue and red color selected corresponded
%  to their math values

%  for example :
%  if b2r(-3,6) ,the color range is from light blue to deep red ,
%  so that the color at -3 is light blue while the color at 3 is light red
%  corresponded


%% Category Discussion according to the cmin and cmax input
% first : from negative to positive
% then  : from positive to positive
% last  : from negative to negative

newmap_all = NaN(size(newsteps,2),3);

% Do you want extra white space at 0?
ExtraWhite = 'No';
if strcmp(ExtraWhite,'No') == 1
    
%% If you don't want extra white space at 0
if (cmin_input < 0)  &&  (cmax_input > 0) ;  
    if abs(cmin_input) < cmax_input 
        
         
        % |--------|---------|--------------------|    
      % -cmax      cmin       0                  cmax         [cmin,cmax]
      %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))
 
       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       start_point = round((cmin_input+cmax_input)/2/cmax_input*color_num);
       if start_point == 0
           start_point = start_point+1;
       end
       newmap = squeeze(newmap_all(start_point:color_num,:));
       
    elseif abs(cmin_input) > cmax_input
        
         % |------------------|------|--------------|    
       %  cmin                0     cmax          -cmin         [cmin,cmax]
       %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))       
       
       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       end_point = round((cmax_input-cmin_input)/2/abs(cmin_input)*color_num);
       newmap = squeeze(newmap_all(1:end_point,:));
       
    elseif abs(cmin_input) == cmax_input
        
         % |------------------|------|--------------|    
       %  cmin                0     cmax          -cmin         [cmin,cmax]
       %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))       
       
       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       end_point = round((cmax_input-cmin_input)/2/abs(cmin_input)*color_num);
       newmap = squeeze(newmap_all(1:end_point,:));
    end
    
       
elseif cmin_input >= 0

       if lims(1) < 0 
           disp('caution:')
           disp('there are still values smaller than 0, but cmin is larger than 0.')
           disp('some area will be in red color while it should be in blue color')
       end
        % |-----------------|-------|-------------|    
      % -cmax               0      cmin          cmax         [cmin,cmax]
      %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))
 
       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       start_point = round((cmin_input+cmax_input)/2/cmax_input*color_num);
       newmap = squeeze(newmap_all(start_point:color_num,:));

elseif cmax_input <= 0

       if lims(2) > 0 
           disp('caution:')
           disp('there are still values larger than 0, but cmax is smaller than 0.')
           disp('some area will be in blue color while it should be in red color')
       end
       
         % |------------|------|--------------------|    
       %  cmin         cmax    0                  -cmin         [cmin,cmax]
       %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))       

       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       end_point = round((cmax_input-cmin_input)/2/abs(cmin_input)*color_num);
       newmap = squeeze(newmap_all(1:end_point,:));

end

elseif strcmp(ExtraWhite,'Yes') == 1
%% If you want an extra large white space at centre
if (cmin_input < 0)  &&  (cmax_input > 0) ;  
    if abs(cmin_input) < cmax_input 
        
         
        % |--------|---------|--------------------|    
      % -cmax      cmin       0                  cmax         [cmin,cmax]
      %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))
 
       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       start_point = round((cmin_input+cmax_input)/2/cmax_input*color_num);
       if start_point == 0
           start_point = start_point+1;
       end
       newmap = squeeze(newmap_all(start_point:color_num,:));
       newmap(length(newmap)-1,:) = 1;     
       
    elseif abs(cmin_input) > cmax_input
        
         % |------------------|------|--------------|    
       %  cmin                0     cmax          -cmin         [cmin,cmax]
       %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))       
       
       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       end_point = round((cmax_input-cmin_input)/2/abs(cmin_input)*color_num);
       newmap = squeeze(newmap_all(1:end_point,:));
       newmap(length(newmap)-1,:) = 1;
    elseif abs(cmin_input) == cmax_input
        
         % |------------------|------|--------------|    
       %  cmin                0     cmax          -cmin         [cmin,cmax]
       %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))       
       
       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       end_point = round((cmax_input-cmin_input)/2/abs(cmin_input)*color_num);
       newmap = squeeze(newmap_all(1:end_point,:));
       newmap((length(newmap)+1)/2-1:(length(newmap)+1)/2+1,:) = 1;
    end
%     newmap((length(newmap)+1)/2-1:(length(newmap)+1)/2+1,:) = 1;
    
       
elseif cmin_input >= 0 % I.e. There will be no blue negative values

       if lims(1) < 0 
           disp('caution:')
           disp('there are still values smaller than 0, but cmin is larger than 0.')
           disp('some area will be in red color while it should be in blue color')
       end
        % |-----------------|-------|-------------|    
      % -cmax               0      cmin          cmax         [cmin,cmax]
      %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))
 
       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       start_point = round((cmin_input+cmax_input)/2/cmax_input*color_num);
       newmap = squeeze(newmap_all(start_point:color_num,:));
       newmap(1,:) = 1;
%        newmap(1:2,:) = 1;

elseif cmax_input <= 0

       if lims(2) > 0 
           disp('caution:')
           disp('there are still values larger than 0, but cmax is smaller than 0.')
           disp('some area will be in blue color while it should be in red color')
       end
       
         % |------------|------|--------------------|    
       %  cmin         cmax    0                  -cmin         [cmin,cmax]
       %    squeeze(colormap(round((cmin+cmax)/2/cmax),size(colormap)))       

       for j=1:3
           newmap_all(:,j) = min(max(transpose(interp1(oldsteps, color_input(:,j), newsteps)), 0), 1);
       end
       end_point = round((cmax_input-cmin_input)/2/abs(cmin_input)*color_num);
       newmap = squeeze(newmap_all(1:end_point,:));
       newmap(length(newmap),:) = 1;
%        newmap(length(newmap)-1:end,:) = 1;
end
  
end













%%%%%%%%% Spare colormap %%%%%%%%

% CMAP NEW COLORMAP
% blue_bottom  = [0 110 255]/255; % For Wave decay/growth and Horiz/Vert EPFD
% white_middle = [255 255 255]/255; % For Wave decay/growth and Horiz/Vert EPFD
% red_top = [230 20 0]/255; % For Wave decay/growth and Horiz/Vert EPFD

% CMAP OLD COLORMAP
% blue_bottom  = [0 50 255]/255; % For 
% white_middle = [255 255 255]/255; % For 
% red_top = [230 20 0]/255; % For 

%                     c1 =  [78 101 148];
%                     c2 =  [88 109 154];
%                     c3 =  [98 118 160];
%                     c4 =  [107 126 166];                   
%                     c5 =  [117 135 172];
%                     c6 =  [127 144 177];
%                     c7 =  [137 152 184];
%                     c8=   [147 161 189];
%                     c9 =  [157 169 195];
%                     c10 = [166 178 201];
%                     c11 = [176 186 207];
%                     c12 = [186 195 213];
%                     c13 = [196 203 219];
%                     c14 = [206 212 225];
%                     c15 = [215 221 231];
%                     c16 = [225 229 233];
%                     c17 = [235 238 243];
%                     c18 = [245 246 249];
%                     c19 = [255 255 255];    
%                     c20 = [252 249 249];
%                     c21 = [249 244 244];
%                     c22 = [247 239 239];
%                     c23 = [244 233 233];
%                     c24 = [241 228 228];
%                     c25 = [239 222 222];
%                     c26 = [236 217 217];
%                     c27 = [233 211 211];
%                     c28 = [231 206 206];    
%                     c29 = [228 201 201];
%                     c30 = [225 195 195];
%                     c31 = [222 190 190];
%                     c32 = [220 184 184];
%                     c33 = [217 179 179];
%                     c34 = [214 173 173];
%                     c35 = [212 168 168];
%                     c36 = [209 163 163];
%                     c37 = [206 157 157];    
%                     c38 = [203 152 152];
%                     c39 = [201 146 146];
%                     c40 = [198 141 141];
%                     c41 = [195 135 135];
%                     c42 = [193 130 130];
%                     c43 = [190 125 125];
%                     c44 = [187 119 119];
%                     c45 = [185 114 114];
%                     c46 = [182 108 108];
%                     c47 = [179 103 103];    
%                     c48 = [176 97 97];
%                     c49 = [174 92 92];
%                     c50 = [171 87 87];
%                     c51 = [168 81 81];
%                     c52 = [166 76 76];
%                     c53 = [163 70 70];
%                     c54 = [160 65 65];
%                     c55 = [158 60 60];
%                     c56 = [155 54 54];
%                     c57 = [152 49 49];    
%                     c58 = [149 43 43];
%                     c59 = [147 38 38];
%                     c60 = [144 32 32];
%                     c61 = [141 27 27];
%                     c62 = [139 27 27];
%                     c63 = [136 16 16];
%                     c64 = [133 11 11];
%                     c65 = [131 5 5];
%                     c66 = [128 0 0];

