How to compare two colors?

12 vues (au cours des 30 derniers jours)
Samah EL QASSAH
Samah EL QASSAH le 1 Déc 2016
Commenté : Samah EL QASSAH le 6 Déc 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?

Réponse acceptée

Geoff Hayes
Geoff Hayes le 1 Déc 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 commentaire
Samah EL QASSAH
Samah EL QASSAH le 6 Déc 2016
Thank you so much! It's so helpful :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by