RGB Values
2 views (last 30 days)
Show older comments
I am currently detecting RGB values using the impixel() command. Is there a function in MatLab which states the name of the color given the RGB value? For instance, is there something that will say [r g b] = [100 100 100] = 'Dark Gray' or similar?
0 Comments
Answers (1)
Sven
on 28 Oct 2011
Your best bet would be to create a lookup from a source such as: http://en.wikipedia.org/wiki/List_of_colors.
Here's my take on it:
% Read from a great source and split it into table rows
str = urlread('http://en.wikipedia.org/wiki/List_of_colors');
trCells = regexp(str,'<tr>\n','split');
% Find rows matching pattern of "Xanadu | #738678 | 45% | 53% | 47%"
expressn = ['<th.*?>(?<colName>.*?)</th>\s*'...
'<td.*?>#\w{6}</td>\s*'...
'<td.*?>(?<pcntR>\d+)%</td>\s*'...
'<td.*?>(?<pcntG>\d+)%</td>\s*'...
'<td.*?>(?<pcntB>\d+)%</td>\s*'...
];
matchedCells = regexp(trCells, expressn,'tokens','once');
% Collate into names and rgb value pairs
allMatches = cat(1,matchedCells{:});
colNames = allMatches(:,1);
colVals = cellfun(@str2double, allMatches(:,2:4))/100;
% QUERY!
testRGB = [.4 .2 .7];
colDiffs = bsxfun(@minus, colVals, testRGB);
colDists = sqrt(sum(colDiffs.^2,2));
[minDist, minIdx] = min(colDists);
% REPORT!
fprintf('[%g %g %g] is closest to [%g %g %g] (%s)\n', testRGB, colVals(minIdx,:), colNames{minIdx})
[0.4 0.2 0.7] is closest to [0.41 0.21 0.61] (Purple Heart)
1 Comment
Sven
on 28 Oct 2011
Oh, and it looks like there's already a file exchange entry that does a similar thing:
http://www.mathworks.com/matlabcentral/fileexchange/24497-rgb-triple-of-color-name-version-2
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!