How to compare two colors?

12 visualizaciones (últimos 30 días)
Samah EL QASSAH
Samah EL QASSAH el 1 de Dic. de 2016
Comentada: Samah EL QASSAH el 6 de Dic. de 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?

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by