RGB Values

9 vues (au cours des 30 derniers jours)
Jake Bolanski
Jake Bolanski le 28 Oct 2011
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?

Réponses (1)

Sven
Sven le 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 commentaire
Sven
Sven le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Report Generator dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by