- Home /
Tried to answer this question on my own and the site said I already had and wouldn't let me.
Comparing 2 color variables.
Hi there, I'm trying to compare 2 colour variables but having no success.
Setting them like this:
private var colour1 : Color = new Color(1.000, 0.945, 0.000, 1.000);
// uvPoint refers to a conversion from a RaycastHit2D's point to uv coordinates
private var colour2 : Color = theSprite.texture.GetPixel(uVPoint.x, uVPoint.y);
And comparing like this:
// This happens on clicking on the colour.
if (colour1 == colour2)
{
print("colour1 = colour2");
print("colour1 = "+ colour1);
print("colour2 = "+ colour2);
}
else if (colour1 != colour2)
{
print("colour1 != colour2");
print("colour1 = "+ colour1);
print("colour2 = "+ colour2);
}
I keep getting the results:
colour1 != colour2
colour1 = RGBA(1.000, 0.949, 0.000, 1.000)
colour2 = RGBA(1.000, 0.949, 0.000, 1.000)
So if both colour variables have identical contents am I missing something fundamental about how colors should be compared?
The sprite texture is set to RGBA 32 bit, if that helps anything...
Thanks very much!
UPDATE:
Tried to print each individual colour value, which gives fuller results. Here's the culprit:
colour1.g = 0.9490196
colour2.g = 0.949
Am now wondering if using Color32 would solve this issue or if it would have the same issue? (Looking into it and will post an answer soon)
I had this show up in a session that I was $$anonymous$$ching today. User was using a $$anonymous$$aterial and wanted to compare the color of a bullet to that of a $$anonymous$$aterial. The issue is that the floating precision of the $$anonymous$$aterial isn't something that you'll ever see.
To resolve this, do your compare using $$anonymous$$athf.Approximately( color1.g, color2.g ) and you will get the right result. You won't be able to deter$$anonymous$$e it from just comparing the values because of weird precision issues.
Answer by khenkel · Sep 08, 2014 at 10:30 AM
I would recommend to separately compare the r, g, b and a variables (respectively only the ones you need).
Also try printing the values of colour2 separately (eg. "print(colour2.r + ", " + colour2.g + ", " + colour2.b + ", " + colour2.a)") and check if they are REALLY equal to colour1. Unity logs often skip decimals.
You could also try out the following:
if(colour1.Equals(colour2))
{
// Do stuff
}
(but I don't know if this is actually the same as the == operator)
UPDATE:
As I said, Unity log often skips decimals. To compare those values up to 3 decimals you could do the following:
if((int)(colour1.r * 1000) == (int)(colour2.r * 1000))
{
// Do stuff
}
Hey, the answers correct :) I tried giving the same answer and the site wouldn't let me which is why I grumpily closed the question. Thanks for answering.
The answer is inaccurate in most situations.
Color Color1 = new Color(0, 0, 0.498f, 1);
print((int)(Color1.b * 1000));//returns 497
print($$anonymous$$athf.RoundToInt(Color1.b * 1000));//returns 498
Color Color2 = new Color(0, 0, 0.333f, 1);
print((int)(Color2.b * 1000));//returns 333
print($$anonymous$$athf.RoundToInt(Color2.b * 1000));//returns 333
We can see why by printing Color1.b and Color2.b as doubles.
double var1 = Color1.b;
print(var1);//returns 0.497999995946884
double var2 = Color2.b;
print(var2); //returns 0.333000004291534
$$anonymous$$athf.RoundToInt returns the correct value every time where as (int) does not.
colour1.Equals(colour2) does not compare colors but object instance.
Color.Equals(Color) compare the data, not the object instance, thats is made by referenceEquals.
Follow this Question
Related Questions
variable before (dot) 2 Answers
How do I change a disabled camera's background colour? 1 Answer