-
Notifications
You must be signed in to change notification settings - Fork 0
/
complexcolorwheel.m
129 lines (114 loc) · 5.16 KB
/
complexcolorwheel.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
function complexcolorwheel(varargin)
% Complex Color Wheel
% Author: Daniel Cox
% Draw a color wheel to represent the complex plane, drawn with
% the function complex2rgb, as a qualitative equivalent of a colorbar
% for real scalar color plots.
%
% The color wheel itself is also drawn with complex2rgb. All options
% will be passed to complex2rgb. See documentation of complex2rgb for
% options related to colors. When using a struct to pass options, it
% is recommended to use the same struct, to match the colorwheel with
% the plot.
%
%
% Options can be passed by either a struct or Parameter Value pairs.
% Options:
% position: Any of the following char arrays or strings:
% 'topleft', 'topright', 'bottomleft', 'bottomright', or
% a 4-element scalar array of normalized units:
% [x, y, width, height]. Default: 'bottomleft'.
% resolution: Positive integer. Number of pixels per dimension for
% drawing the color wheel. Default: 256.
% textparams: Struct. Parameters to be passed to the label text of
% the colorwheel. See Text Properties for more info.
% figure: Figure handle or index. Set target figure. Is ignored
% when an axes handle is passed. Default: Current figure.
% axes: Axes handle or index. Set target axes. Default: Current
% axes.
%
% Note: All objects corresponding to the color wheel (the color wheel
% itself and the text labels) are tagged with 'ComplexColorWheel'.
% This allows easy access after the color wheel has been added.
% For instance, the color wheel and labels can be hidden with the
% following code:
% wheel = findobj(gcf, 'Tag', 'ComplexColorWheel')
% for h = 1:length(wheel)
% wheel(h).Visible='off';
% end
% Check and parse input parameters
in = complexcolorparser(varargin{:});
res = in.resolution;
% Get target figure and current axes
fig = figure(in.figure); % Get target figure
currentaxes = get(fig, 'CurrentAxes'); % Remember current axes
% Get target parentaxes
if isnumeric(in.axes) % By axes index
allaxes = findobj(fig,'type','axes','-or','type','polaraxes');
parentaxes = allaxes(in.axes);
elseif strcmp('current', in.axes) % By CurrentAxes property
parentaxes = get(fig, 'CurrentAxes');
else
parentaxes = in.axes; % By axes handle
end
% Determine color wheel preset position or assign normalized position
if ischar(in.position) || isstring(in.position)
switch in.position % Check for presets
case 'topleft'
wheelpos = [0.02 0.79 0.15 0.15];
case 'topright'
wheelpos = [0.81 0.79 0.15 0.15];
case 'bottomleft'
wheelpos = [0.02 0.02 0.15 0.15];
case 'bottomright'
wheelpos = [0.81 0.02 0.15 0.15];
otherwise
error('Invalid preset for color wheel position.')
end
else
wheelpos = in.position;
end
% Compute color wheel position with respect to parent axes
paxpos = parentaxes.Position; % Parent axes position
xwheel = paxpos(1) + wheelpos(1) * paxpos(3); % Wheel axes x
ywheel = paxpos(2) + wheelpos(2) * paxpos(4); % Wheel axes y
wwheel = wheelpos(3) * paxpos(3); % Wheel axes width
hwheel = wheelpos(4) * paxpos(4); % Wheel axes height
% Option to pass 'auto' instead of a value for vscale
if strcmp(in.vscale, 'auto')
vscale = 1;
else
vscale = in.vscale;
end
% Construct color image of complex unit circle
X = linspace(-vscale, vscale, res);
Y = X';
R = sqrt(X.^2 + Y.^2); % Radius from origin
circle = (R <= 1); % Circle filled with 1
alpha = 1 - linstep(R, vscale*0.97, vscale); % Transparency
C = complex2rgb((X + 1i*Y) .* circle, varargin{:});
% Display color image of complex unit circle
image(axes(fig, 'Position', [xwheel ywheel wwheel hwheel]),...
C, 'AlphaData', alpha, 'Tag', 'ComplexColorWheel');
% Set Y-axis in normal direction, keep strictly to 1:1 aspect ratio
set(gca, 'YDir', 'normal',...
'DataAspectRatioMode', 'manual', 'DataAspectRatio', [1 1 1])
% Insert phase labels
text(res, res/2, '0',...
'FontSize', 14, 'Color', 'white',...
'Tag', 'ComplexColorWheel', in.textparams)
text(res/2, res, '\pi/2',...
'FontSize', 14, 'Color', 'white',...
'HorizontalAlignment', 'center','VerticalAlignment', 'bottom',...
'Tag', 'ComplexColorWheel', in.textparams)
axis off
% Return to original current axes
set(fig, 'CurrentAxes', currentaxes);
end
% Linear step function
function xs = linstep(x, smin, smax)
% Linear step function from (smin, 0) to (smax, 1)
xs = (x - smin)./(smax - smin);
xs(xs < 0) = 0;
xs(xs > 1) = 1;
end