forked from petercorke/toolbox-common-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorname.m
179 lines (158 loc) · 5.64 KB
/
colorname.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
%COLORNAME Map between color names and RGB values
%
% RGB = COLORNAME(NAME) is the RGB-tristimulus value (1x3) corresponding to
% the color specified by the string NAME. If RGB is a cell-array (1xN) of
% names then RGB is a matrix (Nx3) with each row being the corresponding
% tristimulus.
%
% XYZ = COLORNAME(NAME, 'xyz') as above but the XYZ-tristimulus value
% corresponding to the color specified by the string NAME.
%
% XY = COLORNAME(NAME, 'xy') as above but the xy-chromaticity coordinates
% corresponding to the color specified by the string NAME.
%
% NAME = COLORNAME(RGB) is a string giving the name of the color that is
% closest (Euclidean) to the given RGB-tristimulus value (1x3). If RGB is
% a matrix (Nx3) then return a cell-array (1xN) of color names.
%
% NAME = COLORNAME(XYZ, 'xyz') as above but the color is the closest (Euclidean)
% to the given XYZ-tristimulus value.
%
% NAME = COLORNAME(XYZ, 'xy') as above but the color is the closest (Euclidean)
% to the given xy-chromaticity value with assumed Y=1.
%
% Notes::
% - Color name may contain a wildcard, eg. "?burnt"
% - Based on the standard X11 color database rgb.txt.
% - Tristimulus values are in the range 0 to 1
% Copyright (C) 1993-2017, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
function out = colorname(a, varargin)
opt.space = {'rgb', 'xyz', 'xy', 'lab', 'ab'};
opt = tb_optparse(opt, varargin);
persistent rgbtable;
% ensure that the database is loaded
if isempty(rgbtable)
% load mapping table from file
fprintf('loading rgb.txt\n');
f = fopen('data/rgb.txt', 'r');
k = 0;
rgb = [];
names = {};
xy = [];
while ~feof(f)
line = fgets(f);
if line(1) == '#',
continue;
end
[A,count,errm,next] = sscanf(line, '%d %d %d');
if count == 3
k = k + 1;
rgb(k,:) = A' / 255.0;
names{k} = lower( strtrim(line(next:end)) );
end
end
s.rgb = rgb;
s.names = names;
rgbtable = s;
end
if isstr(a)
% map name to rgb/xy
if a(1) == '?'
% just do a wildcard lookup
out = namelookup(rgbtable, a(2:end), opt);
else
out = name2color(rgbtable, a, opt);
end
elseif iscell(a)
% map multiple names to colorspace
out = [];
for name=a
color = name2color(rgbtable, name{1}, opt);
if isempty(color)
warning('Color %s not found', name{1});
end
out = [out; color];
end
else
% map values to strings
out = {};
switch opt.space
case {'rgb', 'xyz', 'lab'}
assert(numcols(a) == 3, 'Color value must have 3 elements');
% convert reference colors to input color space
table = colorspace(['RGB->' opt.space], rgbtable.rgb);
for color=a'
d = distance(color, table');
[~,k] = min(d);
out = [out rgbtable.names{k}];
end
case {'xy', 'ab'}
assert(numcols(a) == 2, 'Color value must have 2 elements');
% convert reference colors to input color space
switch opt.space
case 'xy'
table = colorspace('RGB->XYZ', rgbtable.rgb);
table = table(:,1:2) ./ (sum(table,2)*[1 1]);
case 'ab'
table = colorspace('RGB->Lab', rgbtable.rgb);
table = table(:,2:3);
end
for color=a'
d = distance(color, table');
[~,k] = min(d);
out = [out rgbtable.names{k}];
end
end
if length(out) == 1
out = out{1};
end
end
end
function r = namelookup(table, s, opt)
s = lower(s); % all matching done in lower case
r = {};
count = 1;
for k=1:length(table.names),
if ~isempty( findstr(table.names{k}, s) )
r{count} = table.names{k};
count = count + 1;
end
end
end
function out = name2color(table, s, opt)
s = lower(s); % all matching done in lower case
for k=1:length(table.names),
if strcmp(s, table.names(k)),
rgb = table.rgb(k,:);
switch opt.space
case {'rgb', 'xyz', 'lab'}
out = colorspace(['RGB->' opt.space], rgb);
case 'xy'
XYZ = colorspace('RGB->XYZ', rgb);
out = tristim2cc(XYZ);
case 'ab';
Lab = colorspace('RGB->Lab', rgb);
out = Lab(2:3);
end
return;
end
end
out = [];
end