-
Notifications
You must be signed in to change notification settings - Fork 14
/
applyhatch_plusC.m
238 lines (221 loc) · 8.37 KB
/
applyhatch_plusC.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
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
function im_hatchC = applyhatch_plusC(h,patterns,patterncolors,colorlist,dpi,hatchsc)
%APPLYHATCH_PLUSC Apply colored hatched patterns to a figure
% im_hatch = applyhatch_plusC(h,patterns,patterncolors,colorlist,dpi,hatchsc)
%
% APPLYHATCH_PLUSC(H,PATTERNS) creates a new figure from the figure H by
% replacing distinct colors in H with the black and white
% patterns in PATTERNS. The format for PATTERNS can be
% a string of the characters '/', '\', '|', '-', '+', 'x', '.'
% a cell array of matrices of zeros (white) and ones (black)
%
% By default the lines are of uniform thickenss. hatch patterns line
% thickness can be modified using a direct call to MAKEHATCH_PLUS using
% the following syntax: makehatch_plus('HHn',m) where;
% HH the hatch character written twice, '//', '\\', '||', '--', '++'
% n integer number for thickness
% m integer number for the matrix size (n<=m)
% Ex. makehatch_plus('\\4',9)
%
% APPLYHATCH_PLUSC(H,PATTERNS,COLORS) maps the colors in the n by 3
% matrix COLORS to PATTERNS. Each row of COLORS specifies an RGB
% color value. COLORS can also be a character string list.
%
% Note this function makes a bitmap image of H and so is limited
% to bitmap output.
%
% Example 1: basic operation using color char string
% bar(rand(3,6));
% im_hatchC = applyhatch_plusC(1,'\-x.\x','rkgrgb');%
%
% Example 2: basic operation using color matrix
% bar(rand(3,4));
% im_hatchC = applyhatch_plusC(1,'\-x.',[1 0 0;0 1 0;0 0 1;0 1 1]);
%
% Example 3: basic operation using resolution modification
% pie(rand(6,1));
% legend('Jan','Feb','Mar','Apr','May','Jun');
% im_hatch = applyhatch_plusC(gcf,'|-+.\/','rgbcmy',[],150,0.5);
% imwrite(im_hatch,'im_hatch.tiff','tiff')
% Note : have not been able to understand exactly how colors are assigned
% for some plot functions, so better to leave COLORLIST empty for
% starters
%
% Example 4: basic operation with user defined patterns
% bar(rand(3,3));
% im_hatch = applyhatch_plusC(gcf,{makehatch_plus('\',6),1-makehatch_plus('\',6),makehatch_plus('\',1)},'ggg');%
%
% Example 5: using variable thickness hatches
% bar(rand(3,3));
% im_hatch = applyhatch_plusC(gcf,{makehatch_plus('\',9),makehatch_plus('\\4',9),makehatch_plus('\\8',9)},'rgb');%
%
% Example 6: basic operation using IMAGE plot
% data = reshape([randperm(8) randperm(8) randperm(8)],4,6)
% image(data)
% im_hatch = applyhatch_plusC(1,'|-+.\/x/','rgbcmykr',colormap);
% Note : do not use imagesc, as you need an indexed image if you want to
% control the hatch assignments related to data values.
%
% Modification of APPLYHATCH_PLUS to allow colored patterns
% Modified Brian FG Katz 25-feb-2010
% im_hatch = applyhatch_plusC(h,patterns,patterncolors,colorlist,dpi,hatchsc)
%
% input patterncolors RGB matrix of colors for patterns
% (length(PATTERNS) X 3) or string of color char
% 'r' 'g' 'b' 'c' 'm' 'y' of length = length(PATTERNS)
% DPI allows specification of bitmap resolution, making plot resolution
% better for printing
% HATCHSC multiplier for hatch scale to increase size of pattern for better operation
% at higher resolutions (not used when PATTERNS
% defines pattern matrix)
% default [] uses screen resolution as in APPLYHATCH
% output IM_HATCH RGB bitmap matrix of new figure
% use IMWRITE to output in desired format
%
% Modified Brian FG Katz 21-sep-11
% Variable line thickness
%
% See also: APPLYHATCH, APPLYHATCH_PLUS
% By Ben Hinkle, [email protected]
% This code is in the public domain.
oldppmode = get(h,'paperpositionmode');
oldunits = get(h,'units');
oldcolor = get(h,'color');
oldpos = get(h,'position');
set(h,'paperpositionmode','auto');
set(h,'units','pixels');
set(h,'color',[1 1 1]);
figsize = get(h,'position');
if nargin < 6; hatchsc = 1 ; end
if nargin < 5; dpi = 0 ; end % defaults to screen resolution
if nargin < 4; colorlist = [] ; end
if length(patterns) ~= length(patterncolors)
error('PATTERN and PATTERNCOLORS must be the same length')
end
if ischar(patterncolors),
patterncolors = charcolor2rgb(patterncolors);
end
bits = hardcopy(h,'-dzbuffer',['-r' num2str(dpi)]);
bitsC = ones(size(bits))*0;
blackpixels = intersect(find(bits(:,:,1)==255), (intersect(find(bits(:,:,1)==bits(:,:,2)),find(bits(:,:,1)==bits(:,:,3)))) ) ;
set(h,'paperpositionmode',oldppmode);
set(h,'color',oldcolor);
bwidth = size(bits,2);
bheight = size(bits,1);
bsize = bwidth * bheight;
if ~isempty(colorlist)
colorlist = uint8(floor(255*colorlist));
[colors,colori] = nextnonbw(0,colorlist,bits);
else
colors = (bits(:,:,1) ~= bits(:,:,2)) | ...
(bits(:,:,1) ~= bits(:,:,3));
end
pati = 1;
colorind = find(colors);
while ~isempty(colorind)
colorval(1) = bits(colorind(1));
colorval(2) = bits(colorind(1)+bsize);
colorval(3) = bits(colorind(1)+2*bsize);
if iscell(patterns)
pattern = patterns{pati};
elseif isa(patterns,'char')
pattern = makehatch_plus(patterns(pati),6*hatchsc);
else
pattern = patterns;
end
patternC = uint8(255*pattern);
pattern = uint8(255*(1-pattern));
pheight = size(pattern,2);
pwidth = size(pattern,1);
ratioh = ceil(bheight/pheight);
ratiow = ceil(bwidth/pwidth);
bigpattern = repmat(pattern,[ratioh ratiow]);
if ratioh*pheight > bheight
bigpattern(bheight+1:end,:) = [];
end
if ratiow*pwidth > bwidth
bigpattern(:,bwidth+1:end) = [];
end
bigpattern = repmat(bigpattern,[1 1 3]);
% Create RGB pattern
pat_size = size(pattern,1)*size(pattern,2) ;
pat_id = find(patternC);
patternCrgb = repmat(ones(size(patternC))*255,[1 1 3]) ;
for rgbLOOP = 1:3,
patternCrgb(pat_id+(pat_size*(rgbLOOP-1)))=patternCrgb(pat_id+(pat_size*(rgbLOOP-1)))*patterncolors(pati,rgbLOOP) ;
end % rgbLOOP
bigpatternC = repmat(patternCrgb,[ratioh ratiow 1]);
bigpatternC = bigpatternC(1:size(bigpattern,1),1:size(bigpattern,2),:) ;
% if ratioh*pheight > bheight
% bigpatternC(bheight+1:end,:,:) = [];
% end
% if ratiow*pwidth > bwidth
% bigpatternC(:,bwidth+1:end,:) = [];
% end
color = (bits(:,:,1) == colorval(1)) & ...
(bits(:,:,2) == colorval(2)) & ...
(bits(:,:,3) == colorval(3));
color = repmat(color,[1 1 3]);
bits(color) = bigpattern(color);
bitsC(color) = bigpatternC(color);
if ~isempty(colorlist)
[colors,colori] = nextnonbw(colori,colorlist,bits);
else
colors = (bits(:,:,1) ~= bits(:,:,2)) | ...
(bits(:,:,1) ~= bits(:,:,3));
end
colorind = find(colors);
pati = (pati + 1);
if pati > length(patterns)
pati = 1;
end
end
bitsC(blackpixels)= 255;
bitsC(blackpixels+(bheight*bwidth))= 255;
bitsC(blackpixels+(2*(bheight*bwidth)))= 255;
newfig = figure('units','pixels','visible','off');
imaxes = axes('parent',newfig,'units','pixels');
im = image(bitsC/255,'parent',imaxes);
%fpos = get(newfig,'position');
%set(newfig,'position',[fpos(1:2) figsize(3) figsize(4)+1]);
set(newfig,'position',oldpos)
set(imaxes,'position',[0 0 figsize(3) figsize(4)+1],'visible','off');
set(newfig,'visible','on');
set(newfig,'units','normalized');
set(imaxes,'units','normalized');
set(imaxes,'DataAspectRatio',[1 1 1],'DataAspectRatioMode','manual');
if nargout == 1, im_hatchC = bitsC; end
function [colors,out] = nextnonbw(ind,colorlist,bits)
out = ind+1;
colors = [];
while out <= size(colorlist,1)
if isequal(colorlist(out,:),[255 255 255]) | ...
isequal(colorlist(out,:),[0 0 0])
out = out+1;
else
colors = (colorlist(out,1) == bits(:,:,1)) & ...
(colorlist(out,2) == bits(:,:,2)) & ...
(colorlist(out,3) == bits(:,:,3));
return
end
end
function colors_rgb = charcolor2rgb(colors_char);
for LOOP = 1:length(colors_char),
switch colors_char(LOOP)
case 'r'
colors_rgb(LOOP,:) = [1 0 0] ;
case 'g'
colors_rgb(LOOP,:) = [0 1 0] ;
case 'b'
colors_rgb(LOOP,:) = [0 0 1] ;
case 'c'
colors_rgb(LOOP,:) = [0 1 1] ;
case 'm'
colors_rgb(LOOP,:) = [1 0 1] ;
case 'y'
colors_rgb(LOOP,:) = [1 1 0] ;
case 'k'
colors_rgb(LOOP,:) = [0 0 0] ;
otherwise
error('Invalid folor char string')
end
end