How to simplify [ 1 0 0] to red?

2 次查看(过去 30 天)
Conner Carriere
Conner Carriere 2021-2-10
This is my code that checks which color I have and converts it to matlab syntax
%{
Red = [0 0 0; 255 20 20]; should be complete
Orange = [240 50 0; 255 146 100]; should be complete
Blue = [0 0 0; 20 20 255]; should be complete
Green = [0 200 60; 46 255 150]; should be complete
White = [190 200 200; 255 255 255]; should be complete
Yellow = [215 230 0; 255 255 34]; should be complete
%}
function ColorIdent = ColorID(h)
for w = 1: size(h,1)
if [(h(w,1) <= 255),(h(w,2) <= 20),(h(w,3) <= 20)] %Red limits
ColorIdent(w,:) = [1 0 0]; %Red
elseif [(h(w,1) >= 240),((h(w,2) >= 50) && (h(w,2) <= 146)),(h(w,3) <= 100)] %Orange limits
ColorIdent(w,:) = [1 0 1]; %Orange but had to use magenta
elseif [(h(w,1) <= 20),(h(w,2) <= 20),(h(w,3) <= 255)&&(h(w,3) >= 0)] %Blue limits
ColorIdent(w,:) = [0 0 1]; %Blue
elseif [(h(w,1) <= 46),(h(w,2) >= 200),(h(w,3) <= 150)&&(h(w,3) >= 60)] %Green limits
ColorIdent(w,:) = [0 1 0]; %Green
elseif [(h(w,1) >= 190),(h(w,2) >= 200),(h(w,3) >= 200)] %White limits
ColorIdent(w,:) = [1 1 1]; %White
elseif [(h(w,1) >= 215),(h(w,2) >= 230),(h(w,3) <= 140)] %Yellow limits
ColorIdent(w,:) = [1 1 0]; %Yellow
end
end
end
What I want to do is change the rgb triplet to its respective color
so if [1 0 0] then r
if [1 0 1] then m
if [0 0 1] then b
so on so forth
  4 个评论
Conner Carriere
Conner Carriere 2021-2-10
This is good, but is there a way i can call the variable r later?
so i would ask r = [1 0 0]
Walter Roberson
Walter Roberson 2021-2-10
ColorCodes = 'kbgyrmcw';
k = [0 0 0]; b = [0 0 1]; g = [0 1 0]; y = [0 1 1]; r = [1 0 0]; m = [1 0 1]; c = [1 1 0]; w = [1 1 1];
idx = rgb2ind(reshape(h, [], 1, 3), [k;b;g;y;r;m;c;w]);
ColorIdent = ColorCodes(idx);

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by