필터 지우기
필터 지우기

RGB Values

조회 수: 2 (최근 30일)
Jake Bolanski
Jake Bolanski 2011년 10월 28일
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?

답변 (1개)

Sven
Sven 2011년 10월 28일
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
Sven
Sven 2011년 10월 28일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by