How to compare two colors?

12 Ansichten (letzte 30 Tage)
Samah EL QASSAH
Samah EL QASSAH am 1 Dez. 2016
Kommentiert: Samah EL QASSAH am 6 Dez. 2016
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?

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 1 Dez. 2016
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 Kommentar
Samah EL QASSAH
Samah EL QASSAH am 6 Dez. 2016
Thank you so much! It's so helpful :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by