Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 08, 2014 at 10:29 AM by Romano for the following reason:

Tried to answer this question on my own and the site said I already had and wouldn't let me.

avatar image
0
Question by Romano · Sep 08, 2014 at 10:02 AM · variablecolorcolour

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gregorypierce · Feb 11, 2018 at 02:59 PM 0
Share

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.

1 Reply

  • Sort: 
avatar image
4
Best Answer

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
 }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Romano · Sep 08, 2014 at 10:31 AM 0
Share

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.

avatar image Tactical_Beard Romano · Jan 16, 2016 at 10:29 PM 1
Share

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.

avatar image movAX13h · Feb 26, 2015 at 05:02 PM 0
Share

colour1.Equals(colour2) does not compare colors but object instance.

avatar image buronix movAX13h · Oct 26, 2016 at 06:23 PM 0
Share

Color.Equals(Color) compare the data, not the object instance, thats is made by referenceEquals.

avatar image Necronomicron · Dec 14, 2017 at 11:09 AM 0
Share

This is how you compare floats.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Odd colour tint on scene after adding menu.,Error: Colour Tint added to scene 1 Answer

Get Average Colour From Texture? 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Invert Color Shader (without see-through) 0 Answers

Setting a variable to the amount of red pixels in a texture 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges