Newer
Older
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
function [map,scheme] = brewermap_view(N,scheme)
% An interactive figure for ColorBrewer colormap selection. With demo!
%
% (c) 2015 Stephen Cobeldick
%
% View Cynthia Brewer's ColorBrewer colorschemes in a figure.
%
% * Two colorbars give the colorscheme in color and grayscale.
% * A button toggles between 3D-cube and 2D-lineplot of the RGB values.
% * A button toggles an endless cycle through the colorschemes.
% * A button reverses the colormap.
% * 35 buttons select any ColorBrewer colorscheme.
% * Text with the colorscheme's type (Diverging/Qualitative/Sequential)
% * Text with the colorscheme's number of nodes (defining colors).
%
% Syntax:
% brewermap_view
% brewermap_view(N)
% brewermap_view(N,scheme)
% brewermap_view([],...)
% brewermap_view({axes/figure handles},...) % see "Adjust External Colormaps"
% [map,scheme] = brewermap_view(...)
%
% Calling the function with an output argument blocks MATLAB execution until
% the figure is deleted: the final colormap and parameters are then returned.
%
% See also BREWERMAP CUBEHELIX RGBPLOT COLORMAP COLORMAPEDITOR COLORBAR UICONTROL ADDLISTENER
%
% ### Adjust External Colormaps ###
%
% % Example:
% load spine
% image(X)
% brewermap_view({gca})
%
% Very useful! Simply provide a cell array of axes or figure handles when
% calling this function, and their colormaps will be updated in real-time.
% Note that MATLAB versions <=2010 only support axes handles for this.
%
% ### Input and Output Arguments ###
%
% Input Arguments (*==default):
% N = NumericScalar, an integer to define the colormap length.
% = *[], use the maximum defined number nodes for each colorscheme.
% = {axes/figure handles}, their colormaps will be updated by this function.
% scheme = StringToken, a ColorBrewer scheme name to select the colorscheme.
%
% Output Arguments (block execution until the figure is deleted!):
% map = NumericMatrix, the colormap defined when the figure is closed.
% scheme = StringToken, the name of the colorscheme given in <map>.
%
% [map,scheme] = brewermap_view(N, scheme)
% ### Input Wrangling ###
%
if nargin<1 || isnumeric(N)&&isempty(N)
N = 128;
elseif iscell(N)&&numel(N)
tmp = all(1==cellfun('prodofsize',N)&cellfun(@ishghandle,N));
assert(tmp,'Input <N> may be a cell array of axes or figure handles.')
else
assert(isnumeric(N)&&isscalar(N),'Input <N> must be a scalar numeric.')
assert(isreal(N)&&fix(N)==N&&N>0,'Input <N> must be positive real integer: %g+%gi',N,imag(N))
N = double(N);
end
%
if nargin<2
L = brewermap('list');
scheme = L{1+rem(round(now*1e7),numel(L))};
else
assert(ischar(scheme)&&isrow(scheme),'Second input <scheme> must be a string.')
end
%
[~,~,h] = bmvUpDt(N, scheme);
%
if nargout
waitfor(h);
[~,scheme,map] = bmvUpDt();
end
%
end
%----------------------------------------------------------------------END:colorbrewer_view
function [N,S,ghnd] = bmvUpDt(N,S)
% Draw a new figure or update an existing figure. Callback for buttons & demo.
%
persistent H prvS prvN extH
%
% LHS and RHS slider bounds/limits, and slider step sizes:
lbd = 1;
rbd = 128;
stp = [1,10];
%
[L,V,T] = brewermap('list');
%
switch nargin
case 0 % Demo initialize OR return colormap
N = prvN;
S = prvS;
if nargout==3 % Return colormap
ghnd = brewermap(N,H.fRev(S));
return
end
case 1 % Button/Slider callback
if get(H.demo,'Value') % Demo
return
elseif ischar(N) % scheme name
S = N;
N = prvN;
else % parameter N only
S = prvS;
N = round(get(H.vSld(1),'Value'));
end
case 2 % Demo update OR function call
if iscell(N) % External axes/figure
extH = N;
N = size(colormap(extH{1}),1);
end
if isempty(H) || ~ishghandle(H.fig)
if nargout<3 % Demo
return
end
% Check brewermap version:
str = 'The function "brewermap" returned an unusual %s.';
assert(all(35==[numel(L),numel(V),numel(T)]),str,'array size')
tmp = find(any(diff(+char(T)),2));
assert(numel(tmp)==2&&all(tmp==[9;17]),str,'scheme name sequence')
% Create a new figure:
H = bmvPlot(lbd,rbd,stp,N,S,L);
else % Update slider position:
set(H.vSld, 'Value',max(lbd,min(rbd,N)));
if nargout<3 % Demo
S = L{1+mod(find(strcmpi(S,L)),numel(L))};
set(H.bGrp,'SelectedObject',H.bSep(strcmpi(S,L)));
end
end
%
otherwise
error('How did this happen? This should not be possible.')
end
%
prvN = N;
prvS = S;
ghnd = H.fig;
%
% Adjust slider if out of range for the selected scheme:
idx = strcmpi(S,L);
idy = strcmp('Qualitative',T{idx}) && V(idx)<N;
if idy
N = V(idx);
set(H.vSld, 'Value',max(lbd,min(rbd,N)));
end
%
% Update parameter value text:
set(H.vTxt(1), 'String',sprintf('N = %.0f',N));
%
% Get ColorBrewer colormap and grayscale equivalent:
[map,num,typ] = brewermap(N,H.fRev(S));
mag = sum(map*[0.298936;0.587043;0.114021],2);
%
% Update colorbar values:
set(H.cbAx, 'YLim', [0,abs(N)+(N==0)]+0.5);
set(H.cbIm(1), 'CData',reshape(map,[],1,3))
set(H.cbIm(2), 'CData',repmat(mag,[1,1,3]))
%
% Update 2D line / 3D patch values:
if get(H.D2D3,'Value')
set(H.ln2D, 'XData',linspace(0,1,abs(N)));
set(H.ln2D, {'YData'},num2cell([map,mag],1).');
else
set(H.pt3D, 'XData',map(:,1), 'YData',map(:,2), 'ZData',map(:,3), 'FaceVertexCData',map)
end
%
% Update warning text:
str = {typ;sprintf('%d Nodes',num)};
set(H.warn,'String',str);
%
% Update external axes/figure:
if ~isempty(extH)
for k = find(cellfun(@ishghandle,extH))
colormap(extH{k},map);
end
end
%
end
%----------------------------------------------------------------------END:bmvUpDt
function H = bmvPlot(lbd,rbd,stp,V,S,L)
% Draw a new figure with RGBplot axes, ColorBar axes, and uicontrol sliders.
%
M = 9; % buttons per column
gap = 0.01; % gaps
bth = 0.04; % demo height
btw = 0.09; % demo width
uih = 0.40; % height of UI control group
cbw = 0.24; % width of both colorbars
axh = 1-uih-2*gap; % axes height
wdt = 1-cbw-2*gap; % axes width
%
H.fig = figure('HandleVisibility','callback', 'Color','white',...
'IntegerHandle','off', 'NumberTitle','off',...
'Name','ColorBrewer Interactive Scheme Selector');
%
% Add 2D lineplot:
H.ax2D = axes('Parent',H.fig, 'Position',[gap, uih+gap, wdt, axh],...
'ColorOrder',[1,0,0; 0,1,0; 0,0,1; 0.6,0.6,0.6], 'HitTest','off',...
'Visible','off', 'XLim',[0,1], 'YLim',[0,1], 'XTick',[], 'YTick',[]);
H.ln2D = line([0,0,0,0;1,1,1,1],[0,0,0,0;1,1,1,1], 'Parent',H.ax2D, 'Visible','off');
%
% Add 3D scatterplot:
H.ax3D = axes('Parent',H.fig, 'OuterPosition',[0, uih, wdt+2*gap, 1-uih],...
'Visible','on', 'XLim',[0,1], 'YLim',[0,1], 'ZLim',[0,1], 'HitTest','on');
H.pt3D = patch('Parent',H.ax3D, 'XData',[0;1], 'YData',[0;1], 'ZData',[0;1],...
'Visible','on', 'LineStyle','none', 'FaceColor','none', 'MarkerEdgeColor','none',...
'Marker','o', 'MarkerFaceColor','flat', 'MarkerSize',10, 'FaceVertexCData',[1,1,0;1,0,1]);
view(H.ax3D,3);
grid(H.ax3D,'on')
xlabel(H.ax3D,'Red')
ylabel(H.ax3D,'Green')
zlabel(H.ax3D,'Blue')
%
% Add warning text:
H.warn = text('Parent',H.ax2D, 'Units','normalized', 'Position',[1,1],...
'HorizontalAlignment','right', 'VerticalAlignment','top', 'Color','k');
%
% Add demo button:
H.demo = uicontrol(H.fig, 'Style','togglebutton', 'Units','normalized',...
'Position',[gap,uih+gap+0*bth,btw,bth], 'String','Demo',...
'Max',1, 'Min',0, 'Callback',@bmvDemo);
% Add 2D/3D button:
H.D2D3 = uicontrol(H.fig, 'Style','togglebutton', 'Units','normalized',...
'Position',[gap,uih+gap+1*bth,btw,bth], 'String','2D / 3D',...
'Max',1, 'Min',0, 'Callback',@(h,~)bmv2D3D(H,h));
% Add reverse button:
H.bRev = uicontrol(H.fig, 'Style','togglebutton', 'Units','normalized',...
'Position',[gap,uih+gap+2*bth,btw,bth], 'String','Reverse',...
'Max',1, 'Min',0, 'Callback',@(~,~)bmvUpDt());
H.fRev = @(s)[char(42*ones(1,get(H.bRev,'Value'))),s];
%
% Add colorbars:
C = reshape([1,1,1],1,[],3);
H.cbAx(1) = axes('Parent',H.fig, 'Visible','off', 'Units','normalized',...
'Position',[1-cbw/1,gap,cbw/2-gap,1-2*gap], 'YLim',[0.5,1.5], 'HitTest','off');
H.cbAx(2) = axes('Parent',H.fig, 'Visible','off', 'Units','normalized',...
'Position',[1-cbw/2,gap,cbw/2-gap,1-2*gap], 'YLim',[0.5,1.5], 'HitTest','off');
H.cbIm(1) = image('Parent',H.cbAx(1), 'CData',C);
H.cbIm(2) = image('Parent',H.cbAx(2), 'CData',C);
%
% Add parameter slider, listener, and corresponding text:
V = max(lbd,min(rbd,V));
H.vTxt = uicontrol(H.fig,'Style','text', 'Units','normalized',...
'Position',[gap,uih-bth,btw,bth], 'String','X');
H.vSld = uicontrol(H.fig,'Style','slider', 'Units','normalized',...
'Position',[gap,gap,btw,uih-bth], 'Min',lbd(1), 'Max',rbd(1),...
'SliderStep',stp(1,:)/(rbd(1)-lbd(1)), 'Value',V(1));
addlistener(H.vSld, 'Value', 'PostSet',@(a,b)bmvUpDt(0));
%
% Add scheme button group:
H.bGrp = uibuttongroup('Parent',H.fig, 'BorderType','none', 'Units','normalized',...
'BackgroundColor','white', 'Position',[2*gap+btw,gap,wdt-btw-gap,uih-gap]);
% Determine button locations:
Z = 1:numel(L);
Z = Z+(Z>17);
C = (ceil(Z/M)-1)/4;
R = (M-1-mod(Z-1,M))/M;
% Add scheme buttons to group:
for k = numel(L):-1:1
H.bSep(k) = uicontrol('Style','Toggle', 'String',L{k}, 'Parent',H.bGrp,...
'Unit','normalized', 'Position',[C(k),R(k),1/4,1/M]);
end
set(H.bGrp,'SelectedObject',H.bSep(strcmpi(S,L)));
set(H.bGrp,'SelectionChangeFcn',@(~,e)bmvUpDt(get(e.NewValue,'String')));
%
end
%----------------------------------------------------------------------END:bmvPlot
function bmv2D3D(H,tgh)
% Switch between 2D-line and 3D-cube: swap visibility and hittest, then update.
%
if get(tgh,'Value')% 2D
set(H.ax3D, 'HitTest','off', 'Visible','off')
set(H.ax2D, 'HitTest','on')
set(H.pt3D, 'Visible','off')
set(H.ln2D, 'Visible','on')
else % 3D
set(H.ax2D, 'HitTest','off')
set(H.ax3D, 'HitTest','on', 'Visible','on')
set(H.ln2D, 'Visible','off')
set(H.pt3D, 'Visible','on')
end
bmvUpDt();
%
end
%----------------------------------------------------------------------END:bmv2D3D
function bmvDemo(tgh,~)
% While the toggle button is depressed run a loop showing ColorBrewer schemes.
%
% Initial scheme:
[N,S] = bmvUpDt();
%
% While the toggle button is down, step schemes:
while ishghandle(tgh)&&get(tgh,'Value')
%
% Update figure:
[~,S] = bmvUpDt(round(N),S);
%
% Frame-rate faster/slower:
pause(1.1);
end
%
end
%----------------------------------------------------------------------END:bmvDemo
% Copyright (c) 2015 Stephen Cobeldick
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and limitations under the License.
%----------------------------------------------------------------------END:license