NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/plots/applyhatch.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 function applyhatch(h,patterns,colorlist)
2 %APPLYHATCH Apply hatched patterns to a figure
3 %  APPLYHATCH(H,PATTERNS) creates a new figure from the figure H by
4 %  replacing distinct colors in H with the black and white
5 %  patterns in PATTERNS. The format for PATTERNS can be
6 %    a string of the characters '/', '\', '|', '-', '+', 'x', '.'
7 %    a cell array of matrices of zeros (white) and ones (black)
8 %
9 %  APPLYHATCH(H,PATTERNS,COLORS) maps the colors in the n by 3
10 %  matrix COLORS to PATTERNS. Each row of COLORS specifies an RGB
11 %  color value.
12 %
13 %  Note this function makes a bitmap image of H and so is limited
14 %  to low-resolution, bitmap output.
15 %
16 %  Example 1:
17 %    bar(rand(3,4));
18 %    applyhatch(gcf,'\-x.');
19 %
20 %  Example 2:
21 %    colormap(cool(6));
22 %    pie(rand(6,1));
23 %    legend('Jan','Feb','Mar','Apr','May','Jun');
24 %    applyhatch(gcf,'|-+.\/',cool(6));
25 %
26 %  See also: MAKEHATCH
27
28 %  By Ben Hinkle, bhinkle@mathworks.com
29 %  This code is in the public domain.
30  
31 oldppmode = get(h,'paperpositionmode');
32 oldunits = get(h,'units');
33 set(h,'paperpositionmode','auto');
34 set(h,'units','pixels');
35 figsize = get(h,'position');
36 if nargin == 2
37   colorlist = [];
38 end
39 bits = hardcopy(h,'-dzbuffer','-r0');
40 set(h,'paperpositionmode',oldppmode);
41
42 bwidth = size(bits,2);
43 bheight = size(bits,1);
44 bsize = bwidth * bheight;
45 if ~isempty(colorlist)
46   colorlist = uint8(255*colorlist);
47   [colors,colori] = nextnonbw(0,colorlist,bits);
48 else
49   colors = (bits(:,:,1) ~= bits(:,:,2)) | ...
50            (bits(:,:,1) ~= bits(:,:,3));
51 end
52 pati = 1;
53 colorind = find(colors);
54 while ~isempty(colorind)
55   colorval(1) = bits(colorind(1));
56   colorval(2) = bits(colorind(1)+bsize);
57   colorval(3) = bits(colorind(1)+2*bsize);
58   if iscell(patterns)
59     pattern = patterns{pati};
60   elseif isa(patterns,'char')
61     pattern = makehatch(patterns(pati));
62   else
63     pattern = patterns;
64   end
65   pattern = uint8(255*(1-pattern));
66   pheight = size(pattern,2);
67   pwidth = size(pattern,1);
68   ratioh = ceil(bheight/pheight);
69   ratiow = ceil(bwidth/pwidth);
70   bigpattern = repmat(pattern,[ratioh ratiow]);
71   if ratioh*pheight > bheight
72     bigpattern(bheight+1:end,:) = [];
73   end
74   if ratiow*pwidth > bwidth
75     bigpattern(:,bwidth+1:end) = [];
76   end
77   bigpattern = repmat(bigpattern,[1 1 3]);
78   color = (bits(:,:,1) == colorval(1)) & ...
79           (bits(:,:,2) == colorval(2)) & ...
80           (bits(:,:,3) == colorval(3));
81   color = repmat(color,[1 1 3]);
82   bits(color) = bigpattern(color);
83   if ~isempty(colorlist)
84     [colors,colori] = nextnonbw(colori,colorlist,bits);
85   else
86     colors = (bits(:,:,1) ~= bits(:,:,2)) | ...
87              (bits(:,:,1) ~= bits(:,:,3));
88   end
89   colorind = find(colors);
90   pati = (pati + 1);
91   if pati > length(patterns)
92     pati = 1;
93   end
94 end
95
96 newfig = figure('units','pixels','visible','off');
97 imaxes = axes('parent',newfig,'units','pixels');
98 im = image(bits,'parent',imaxes);
99 fpos = get(newfig,'position');
100 set(newfig,'position',[fpos(1:2) figsize(3) figsize(4)+1]);
101 set(imaxes,'position',[0 0 figsize(3) figsize(4)+1],'visible','off');
102 set(newfig,'visible','on');
103
104 function [colors,out] = nextnonbw(ind,colorlist,bits)
105 out = ind+1;
106 colors = [];
107 while out <= size(colorlist,1)
108   if isequal(colorlist(out,:),[255 255 255]) | ...
109         isequal(colorlist(out,:),[0 0 0])
110     out = out+1;
111   else
112     colors = (colorlist(out,1) == bits(:,:,1)) & ...
113              (colorlist(out,2) == bits(:,:,2)) & ...
114              (colorlist(out,3) == bits(:,:,3));
115     return
116   end
117 end
Note: See TracBrowser for help on using the browser.