필터 지우기
필터 지우기

How to compare two colors?

조회 수: 7 (최근 30일)
Samah EL QASSAH
Samah EL QASSAH 2016년 12월 1일
댓글: Samah EL QASSAH 2016년 12월 6일
Hi!
I want to compare the color of a button with another color. I wrote the code below:
c = get(handles.showColor, 'BackgroundColor');
color = 'green';
st = strcmp(c,color);
if(st == 1)
axes(handles.axes1);
img = imread('image\electronique.png');
image(img);
axis off;
end;
is it correct? Can i compare c and color?

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 12월 1일
Samah - with R2014a, if I create a pushbutton and then call
get(handles.pushbutton1,'BackgroundColor')
an array of three elements is returned as
ans =
0.9255 0.9255 0.9255
The elements of this array correspond to the red, green, and blue components of the colour with values between zero and one. Your code
st = strcmp(c,color);
is attempting to compare a string with an array of numbers and so will fail. You would need to determine what the equivalent RGB colour for green would be. For example,
greenClr = [0 1 0];
and then you would need to compare each element of greenClr with each element of c. However since these elements are doubles (not integers) you would need to compare the pairs within some tolerance. For example,
myColour = rand(1,3);
greenColour = [0 1 0];
% compare the two
if all(abs(myColour - greenColour) < 0.000001)
fprintf('Colours are (near) identical!\n');
else
fprintf('Colours are not identical!\n');
end
Note that since
abs(myColour - greenColour) < 0.000001
returns a logical array of three elements, then we need to use all which returns true (1) or false (0) if all elements of the input array are one or not.
  댓글 수: 1
Samah EL QASSAH
Samah EL QASSAH 2016년 12월 6일
Thank you so much! It's so helpful :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by