-
Notifications
You must be signed in to change notification settings - Fork 25
/
rgba.m
192 lines (170 loc) · 4.88 KB
/
rgba.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
function out=rgba(color,alpha)
%RGBA Translates a color from multiple formats into a [R G B alpha] color-transparency
%
% [RGBa] = RGBA(COLOR,ALPHA)
% Matlab color-transparency is returned as [R G B alpha] where alpha
% specifies the transparency.
%
% INPUTS:
% color : number of optional arguments
% alpha : blending of symbol face color (0.0 transparent through 1.0 opaque)
%
% OUTPUTS:
% RGBa : color + transparency returned as 4-element vector [R G B alpha],
% where [R G B] is a standard Matlab color triplet and
% 0 <= alpha <= 1 is the amount of blending
%
% DESCRIPTION:
% The rgb triplet function is used to obtain the Matlab color triplet and
% the transparency added to the vector. Refer to the documentation on the
% RGB function for allowed COLOR values.
%
% DEPENDENCY:
% RGB triplet function of Ben Mitch:
% https://www.mathworks.com/matlabcentral/fileexchange/1805-rgb-m
%
% EXAMPLE:
% type "rgba(demo,1.0)" to get started with opaque
% type "rgba(demo,0.5)" to get started with semi-transparent
% This is a simple interface built onto the RGB function written by
% Ben Mitch with modification of his demo program.
%
% Author: Peter A. Rochford
% Symplectic, LLC
% www.thesymplectic.com
% Check for demo
if strcmp(color,'demo')
rgb_demo(alpha);
return;
end
RGB3 = rgb(color);
out = [RGB3(1); RGB3(2); RGB3(3); alpha];
end % function rgba
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEMO
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function rgb_demo(alpha)
figure(1)
clf
cols = get_cols;
cols = {cols{:,1}}';
cols = { cols{:}, ...
'k', ...
'r', ...
'g', ...
'b', ...
'y', ...
'm', ...
'c', ...
'w', ...
'', ...
'extremely dark green', ...
'very dark green', ...
'dark green', ...
'slightly dark green', ...
'green', ...
'slightly pale green', ...
'pale green', ...
'very pale green', ...
'extremely pale green', ...
};
height=9;
x=0;
y=0;
for n=1:length(cols)
rect(x,y,cols{n},alpha)
y=y+1;
if y==height
x=x+2;
y=0;
end
end
if y==0 x=x-2; end
axis([0 (x+2) 0 height])
title(['names on different rows are alternates (alpha=' num2str(alpha) ')'])
end % function rgb_demo
function rect(x,y,col,alpha)
if isempty(col) return; end
% Get color triplet
col_=col;
if iscell(col) col=col{1}; end
colrgb=rgb(col);
% Draw rectangle with color-transparency
r = patch([x+0.1 x+1.9 x+1.9 x+0.1],[y+0.1 y+0.1 y+0.9 y+0.9], colrgb);
r.FaceAlpha = alpha; % set transparency
if strcmp(col(1),'u') & length(col)==2
t=text(x+1,y+0.5,{'unnamed',['colour (' col(2) ')']});
else
t=text(x+1,y+0.5,col_);
if sum(colrgb)<1.5 set(t,'color',[1 1 1]); end
end
set(t,'horizontalalignment','center')
set(t,'fontsize',10)
end % function rect
function rgb_list
cols=get_cols;
disp(' ')
for n=1:size(cols,1)
code=cols{n,2};
str=cols{n,1};
str_=[];
for m=1:length(str)
str_=[str_ str{m} ', '];
end
str_=str_(1:end-2);
if strcmp(str_(1),'u') & length(str_)==2
str_=['* (' str_(2) ')'];
end
disp([' [' sprintf('%.1f %.1f %.1f',code) '] - ' str_])
end
disp([10 '* colours marked thus are not named - if you know their' 10 ' designation, or if you feel sure a colour is mis-named,' 10 ' email me (address via help) or comment at' 10 ' www.mathworks.com/matlabcentral - "rgb demo" to see them' 10])
end % function rgb_list
function cols=get_cols
cols={
'black', [0 0 0]; ...
'navy', [0 0 0.5]; ...
'blue', [0 0 1]; ...
'u1', [0 0.5 0]; ...
{'teal','turquoise'}, [0 0.5 0.5]; ...
'slateblue', [0 0.5 1]; ...
{'green','lime'}, [0 1 0]; ...
'springgreen', [0 1 0.5]; ...
{'cyan','aqua'}, [0 1 1]; ...
'maroon', [0.5 0 0]; ...
'purple', [0.5 0 0.5]; ...
'u2', [0.5 0 1]; ...
'olive', [0.5 0.5 0]; ...
{'gray','grey'}, [0.5 0.5 0.5]; ...
'u3', [0.5 0.5 1]; ...
{'mediumspringgreen','chartreuse'}, [0.5 1 0]; ...
'u4', [0.5 1 0.5]; ...
'sky', [0.5 1 1]; ...
'red', [1 0 0]; ...
'u5', [1 0 0.5]; ...
{'magenta','fuchsia'}, [1 0 1]; ...
'orange', [1 0.5 0]; ...
'u6', [1 0.5 0.5]; ...
'u7', [1 0.5 1]; ...
'yellow', [1 1 0]; ...
'u8', [1 1 0.5]; ...
'white', [1 1 1]; ...
};
for n=1:size(cols,1)
if ~iscell(cols{n,1}) cols{n,1}={cols{n,1}}; end
end
end % function get_cols
% converts a DWORD into a four byte row vector
function out=bytes4(in)
out=[0 0 0 0];
if in<0 | in>(2^32-1)
warning('DWORD out of range, zero assumed');
return;
end
N=4;
while(in>0)
out(N)=mod(in,256);
in=(in-out(N))/256;
N=N-1;
end
end % function bytes4