123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- %Nathan Childress (2024). bluewhitered (https://www.mathworks.com/matlabcentral/fileexchange/4058-bluewhitered), MATLAB Central File Exchange.
- function newmap = bluewhitered(m)
- %BLUEWHITERED Blue, white, and red color map.
- % BLUEWHITERED(M) returns an M-by-3 matrix containing a blue to white
- % to red colormap, with white corresponding to the CAXIS value closest
- % to zero. This colormap is most useful for images and surface plots
- % with positive and negative values. BLUEWHITERED, by itself, is the
- % same length as the current colormap.
- %
- % Examples:
- % ------------------------------
- % figure
- % imagesc(peaks(250));
- % colormap(bluewhitered(256)), colorbar
- %
- % figure
- % imagesc(peaks(250), [0 8])
- % colormap(bluewhitered), colorbar
- %
- % figure
- % imagesc(peaks(250), [-6 0])
- % colormap(bluewhitered), colorbar
- %
- % figure
- % surf(peaks)
- % colormap(bluewhitered)
- % axis tight
- %
- % See also HSV, HOT, COOL, BONE, COPPER, PINK, FLAG,
- % COLORMAP, RGBPLOT.
- if nargin < 1
- m = size(get(gcf,'colormap'),1);
- end
- % default
- bottom = [0 0 0.5];
- botmiddle = [0 0.5 1];
- middle = [1 1 1];
- topmiddle = [1 0 0];
- top = [0.5 0 0];
- % custom colour code
- % {'#2980B9','#5499C7','#7FB3D5','#A9CCE3','#D4E6F1','#EAF2F8',...
- % '#FBEEE6','#F6DDCC','#EDBB99','#E59866','#DC7633','#D35400'}
- % bottom = hex2rgb('2980B9');%hex2rgb('2980B9')*255
- % botmiddle = hex2rgb('7FB3D5');%hex2rgb('7FB3D5')*255
- % middle = [1 1 1];
- % topmiddle = hex2rgb('E59866');%hex2rgb('E59866')*255
- % top = hex2rgb('D35400');%hex2rgb('D35400')*255
- % Find middle
- lims = get(gca, 'CLim');
- % Find ratio of negative to positive
- if (lims(1) < 0) & (lims(2) > 0)
- % It has both negative and positive
- % Find ratio of negative to positive
- ratio = abs(lims(1)) / (abs(lims(1)) + lims(2));
- neglen = round(m*ratio);
- poslen = m - neglen;
-
- % Just negative
- new = [bottom; botmiddle; middle];
- len = length(new);
- oldsteps = linspace(0, 1, len);
- newsteps = linspace(0, 1, neglen);
- newmap1 = zeros(neglen, 3);
-
- for i=1:3
- % Interpolate over RGB spaces of colormap
- newmap1(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
- end
-
- % Just positive
- new = [middle; topmiddle; top];
- len = length(new);
- oldsteps = linspace(0, 1, len);
- newsteps = linspace(0, 1, poslen);
- newmap = zeros(poslen, 3);
-
- for i=1:3
- % Interpolate over RGB spaces of colormap
- newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
- end
-
- % And put 'em together
- newmap = [newmap1; newmap];
-
- elseif lims(1) >= 0
- % Just positive
- new = [middle; topmiddle; top];
- len = length(new);
- oldsteps = linspace(0, 1, len);
- newsteps = linspace(0, 1, m);
- newmap = zeros(m, 3);
-
- for i=1:3
- % Interpolate over RGB spaces of colormap
- newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
- end
-
- else
- % Just negative
- new = [bottom; botmiddle; middle];
- len = length(new);
- oldsteps = linspace(0, 1, len);
- newsteps = linspace(0, 1, m);
- newmap = zeros(m, 3);
-
- for i=1:3
- % Interpolate over RGB spaces of colormap
- newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
- end
-
- end
- %
- % m = 64;
- % new = [bottom; botmiddle; middle; topmiddle; top];
- % % x = 1:m;
- %
- % oldsteps = linspace(0, 1, 5);
- % newsteps = linspace(0, 1, m);
- % newmap = zeros(m, 3);
- %
- % for i=1:3
- % % Interpolate over RGB spaces of colormap
- % newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
- % end
- %
- % % set(gcf, 'colormap', newmap), colorbar
|