Skip to content

Commit 5d1e765

Browse files
committed
add BasicColors enumeration
1 parent 66303b9 commit 5d1e765

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
classdef BasicColors
2+
% One-line description here, please.
3+
%
4+
% Enumeration BasicColors
5+
%
6+
% Example
7+
% label = 'Magenta';
8+
% item = imagem.util.enums.BasicColors.fromLabel(label);
9+
% rgb = item.RGB
10+
% rgb =
11+
% 1 0 1
12+
%
13+
% See also
14+
%
15+
16+
% ------
17+
% Author: David Legland
18+
19+
% Created: 2021-01-05, using Matlab 9.8.0.1323502 (R2020a)
20+
% Copyright 2021 INRAE - BIA-BIBS.
21+
22+
23+
%% Enumerates the different cases
24+
enumeration
25+
Red('Red', [1 0 0]);
26+
Green('Green', [0 1 0]);
27+
Blue('Blue', [0 0 1]);
28+
29+
Cyan('Cyan', [1 1 0]);
30+
Magenta('Magenta', [1 0 1]);
31+
Yellow('Yellow', [0 1 1]);
32+
33+
Black('Black', [0.0 0.0 0.0]);
34+
White('White', [1.0 1.0 1.0]);
35+
36+
Gray('Gray', [0.5 0.5 0.5]);
37+
Dark_Gray('Dark Gray', [0.25 0.25 0.25]);
38+
Light_Gray('Light Gray', [0.75 0.75 0.75]);
39+
end % end properties
40+
41+
42+
%% Static methods
43+
methods (Static)
44+
function res = allNames()
45+
% Returns a cell list with all enumeration names.
46+
mc = ?imagem.util.enums.BasicColors;
47+
itemList = mc.EnumerationMemberList;
48+
nItems = length(itemList);
49+
res = cell(1, nItems);
50+
51+
for i = 1:nItems
52+
% retrieve current enumeration item
53+
mitem = itemList(i);
54+
res{i} = mitem.Name;
55+
end
56+
end
57+
58+
function res = fromName(name)
59+
% Identifies a BasicColors from its name.
60+
if nargin == 0 || ~ischar(name)
61+
error('requires a character array as input argument');
62+
end
63+
64+
mc = ?imagem.util.enums.BasicColors;
65+
itemList = mc.EnumerationMemberList;
66+
for i = 1:length(itemList)
67+
% retrieve current enumeration item
68+
mitem = itemList(i);
69+
item = imagem.util.enums.BasicColors.(mitem.Name);
70+
if strcmpi(name, char(item))
71+
res = item;
72+
return;
73+
end
74+
end
75+
76+
error('Unrecognized BasicColors name: %s', name);
77+
end
78+
79+
function res = allLabels()
80+
% Returns a cell list with all enumeration names.
81+
mc = ?imagem.util.enums.BasicColors;
82+
itemList = mc.EnumerationMemberList;
83+
nItems = length(itemList);
84+
res = cell(1, nItems);
85+
86+
for i = 1:nItems
87+
% retrieve current enumeration item
88+
mitem = itemList(i);
89+
item = imagem.util.enums.BasicColors.(mitem.Name);
90+
res{i} = item.Label;
91+
end
92+
end
93+
94+
function res = fromLabel(label)
95+
% Identifies a BasicColors from its label.
96+
if nargin == 0 || ~ischar(label)
97+
error('requires a character array as input argument');
98+
end
99+
100+
mc = ?imagem.util.enums.BasicColors;
101+
itemList = mc.EnumerationMemberList;
102+
for i = 1:length(itemList)
103+
% retrieve current enumeration item
104+
mitem = itemList(i);
105+
item = imagem.util.enums.BasicColors.(mitem.Name);
106+
if strcmpi(label, item.Label)
107+
res = item;
108+
return;
109+
end
110+
end
111+
112+
error('Unrecognized BasicColors label: %s', label);
113+
end
114+
end % end methods
115+
116+
117+
%% Constructor
118+
methods
119+
function obj = BasicColors(label, rgb, varargin)
120+
% Constructor for BasicColors class.
121+
obj.Label = label;
122+
obj.RGB = rgb;
123+
end
124+
125+
end % end constructors
126+
127+
128+
%% Properties
129+
properties
130+
% The label of the color to display.
131+
Label;
132+
% The red, green and blue values, as a 1-by-3 array between 0 and 1.
133+
RGB;
134+
135+
end % end properties
136+
137+
end % end classdef
138+

0 commit comments

Comments
 (0)