-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluewhitered.m
executable file
·101 lines (91 loc) · 2.8 KB
/
bluewhitered.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
bottom = [0 0 0.5];
botmiddle = [0 0.5 1];
middle = [1 1 1];
topmiddle = [1 0 0];
top = [0.5 0 0];
% 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