Newer
Older
function target = colorbar(this,cAxis,colorMap,varargin)

jacques.grelet_ird.fr
committed
if verLessThan('matlab','8.4')
ss = get(0,'ScreenSize')*.8;
else
r = groot;
ss = (r.ScreenSize) *.8;
end
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
p = inputParser;
p.addRequired('cAxis', @isnumeric);
p.addRequired('colorMap', @isnumeric);
p.addParamValue('type','vertical',@(a)ismember(lower(a),{'vertical','horizontal'}));
p.addParamValue('tick',[],@isnumeric);
p.addParamValue('tickLabel',{},@iscell);
p.addParamValue('id','kml_colorbar',@ischar);
p.addParamValue('name','kml_colorbar',@ischar);
p.addParamValue('description','',@ischar);
p.addParamValue('visibility',true,@islogical);
p.addParamValue('rotation',0,@(a)isnumeric(a) &&~isempty(a) && numel(a)==1);
p.addParamValue('drawOrder',0,@(a)isnumeric(a) &&~isempty(a) && numel(a)==1);
p.addParamValue('timeStamp','',@ischar);
p.addParamValue('timeSpanBegin','',@ischar);
p.addParamValue('timeSpanEnd','',@ischar);
p.parse(cAxis,colorMap,varargin{:});
arg = p.Results;
if numel(cAxis) > 2
cAxis = [min(cAxis) max(cAxis)];
end
l = linspace(cAxis(1),cAxis(2),1000);
fh = figure;
if strcmpi(arg.type,'vertical')
ratio = 40;
pcolor([0*l.' 1+0*l.'],[l.' l.'],[l.' l.'])
set(gcf,'Position',[0 0 ss(4)./ratio ss(4)]);%[ss(1)+ss(4)./ratio ss(2) ss(4)./ratio ss(4)]);
set(gca,'XTick',[]);
if ~isempty(arg.tick)
set(gca,'YTick',arg.tick);
end
if ~isempty(arg.tickLabel)
set(gca,'YTickLabel',arg.tickLabel);
end
sz = [2/ratio 1];
overlayPos = [2 1];
screenPos = [1 1];
else
ratio = 40;
pcolor([l.' l.'],[0*l.' 1+0*l.'],[l.' l.'])
set(gcf,'Position',[0 0 ss(3) ss(3)./ratio ]);%[ss(1) ss(2)+ss(3)./ratio ss(3) ss(3)./ratio ]);
set(gca,'YTick',[]);
if ~isempty(arg.tick)
set(gca,'XTick',arg.tick);
end
if ~isempty(arg.tickLabel)
set(gca,'XTickLabel',arg.tickLabel);
end
sz = [1 2/ratio];
overlayPos = [1 (ratio-4)/2];
screenPos = [1 1];
end
movegui(fh,'north');
shading flat;
caxis(cAxis);
colormap(colorMap);
name = 'Colorbar';
basename = 'Colorbar';
name = [name '.png'];
k = 1;
while ~isempty(dir(name))
name = sprintf('%s (%i)%s',basename,k,'.png');
k = k+1;
end
bgColor = [0 0 0];
fgColor = [1 1 1];
set(gca,'Color',bgColor,'XColor',fgColor,'YColor',fgColor);
set(fh,'Color',bgColor);
set(gca,'FontWeight','bold','FontSize',15)
% grid on
% set(gca,'GridLineStyle','-')
% figure(fh)
% drawnow
% pause(1) %drawing on windows starts with a transparent window, better give some time for it to show, so we don't capture the background
% drawnow
% drawnow('expose');
% im = getframe(fh);
% drawnow('expose');
% im = im.cdata;
set(fh,'PaperPositionMode','auto','InvertHardcopy','off')
print('-dpng','-r0',name,fh)
im = imread(name);
axc = ceil(bgColor*255);
alphaMap = uint8(~(im(:,:,1)==axc(1) & im(:,:,2)==axc(2) & im(:,:,3)==axc(3))*255);
imwrite(im,name,'Alpha',alphaMap);
close(fh);
drawnow
% <ScreenOverlay id="khScreenOverlay756">
% <name>Simple crosshairs</name>
% <description>This screen overlay uses fractional positioning
% to put the image in the exact center of the screen</description>
% <Icon>
% <href>http://myserver/myimage.jpg</href>
% </Icon>
% <overlayXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
% <screenXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
% <rotation>39.37878630116985</rotation>
% <size x="0" y="0" xunits="pixels" yunits="pixels"/>
% </ScreenOverlay>
overlay = this.xml.createElement('ScreenOverlay');
overlayXY = this.xml.createElement('overlayXY');
screenXY = this.xml.createElement('screenXY');
size = this.xml.createElement('size');
icon = this.xml.createElement('Icon');
overlay.setAttribute('id',arg.id);
overlay.appendChild(this.textNode('name',arg.name));
overlay.appendChild(this.textNode('open','1'));
overlay.appendChild(this.textNode('visibility',num2str(arg.visibility)));
overlay.appendChild(this.textNode('description',arg.description));
overlay.appendChild(this.textNode('drawOrder',num2str(arg.drawOrder)));
if ~isempty(arg.timeStamp)
timeStamp = this.xml.createElement('TimeStamp');
timeStamp.appendChild(this.textNode('when',arg.timeStamp));
overlay.appendChild(timeStamp);
elseif ~isempty(arg.timeSpanBegin) || ~isempty(arg.timeSpanEnd)
timeSpan = this.xml.createElement('TimeSpan');
if ~isempty(arg.timeSpanBegin)
timeSpan.appendChild(this.textNode('begin',arg.timeSpanBegin));
end
if ~isempty(arg.timeSpanEnd)
timeSpan.appendChild(this.textNode('end',arg.timeSpanEnd));
end
overlay.appendChild(timeSpan);
end
overlayXY.setAttribute('x',num2str(overlayPos(1),16))
overlayXY.setAttribute('y',num2str(overlayPos(2),16))
overlayXY.setAttribute('xunits','fraction')
overlayXY.setAttribute('yunits','fraction')
screenXY.setAttribute('x',num2str(screenPos(1),16))
screenXY.setAttribute('y',num2str(screenPos(2),16))
screenXY.setAttribute('xunits','fraction')
screenXY.setAttribute('yunits','fraction')
size.setAttribute('x',num2str(sz(1),16))
size.setAttribute('y',num2str(sz(2),16))
size.setAttribute('xunits','fraction')
size.setAttribute('yunits','fraction')
overlay.appendChild(this.textNode('rotation', num2str(arg.rotation)));
icon.appendChild(this.textNode('href',name));
overlay.appendChild(overlayXY);
overlay.appendChild(screenXY);
overlay.appendChild(size);
overlay.appendChild(icon);
this.doc.appendChild(overlay);
target.id = arg.id;
target.type = 'ScreenOverlay';
this.addIncludeFile(name);
end