How to compare two colors?

7 次查看(过去 30 天)
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.

更多回答(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