Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by QuantumCD · Aug 29, 2013 at 10:38 PM · c#colorcompare

See if a Color is Within a Threshold

I'm looking for a fast/easy way to simply compare two Color or Color32 objects to apply a threshold to one of my scripts. The script just needs to see if a color is within the range of two colors given by a field. I couldn't find any method provided by the Unity API, and I was wondering if anyone on here might have a good solution. Thanks for any help!

Comment
Add comment · Show 5
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 AlucardJay · Aug 29, 2013 at 10:48 PM 0
Share

Would it be feasible to convert the colour to HSV , then check if the H value is within the specified ranges ranges (0-360)?

HSV <-> RGB conversion : http://www.cs.rit.edu/~ncs/color/t_convert.html

I have converted the above to uJS, the script can be found at the bottom of this post (ColourCalculator.js) : http://forum.unity3d.com/threads/190968-$$anonymous$$aking-a-Colour-Picker

avatar image QuantumCD · Aug 29, 2013 at 10:52 PM 0
Share

Unfortunately, I don't really think that'd work for my particular case. Thanks for the thought though. I was just looking for a way to do something like a fuzzy compare. The threshold isn't likely to be very big--just a few increments down/up in the RGB values.

avatar image robertbu · Aug 29, 2013 at 11:07 PM 1
Share

Not completely sure what you are looking for since in one place you say, "simply compare two Color or Color32 objects," and in another say, "within the range of two colors". If you want to see how close two color are to each other, then treat them like a vector and take a look at the magnitude of the difference:

Given:

     Color32 col1;
     Color32 col2;

Test difference:

     Vector3 test1 = new Vector3(col1.r, col1.g, col1.b);
     Vector3 test2 = new Vector3(col2.r, col2.b, col2.g);
     Debug.Log ((test2 - test1).magnitude);

Set a threshold for magnitude for an acceptable color. You can make it slightly more efficient by using sqr$$anonymous$$agnitude. The code for Color (vs Color32) is a bit simpler since you can subtract variables of type Color.

avatar image QuantumCD robertbu · Aug 29, 2013 at 11:37 PM 0
Share

Sorry I wasn't very clear. I basically want to see if a color is within a range of two colors. Given colorA and colorB, if colorC is in between these two in terms of value (RGB) then I'd like to know.

avatar image alonsoGarrote · Aug 24, 2014 at 12:11 AM 0
Share

robertbu, thanks, this helps with my current project.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DESTRUKTORR · Aug 29, 2013 at 10:58 PM

 bool isRedGood = checkedColor.r >= Mathf.Min(color1.r, color2.r) && checkedColor.r <= Mathf.Max(color1.r, color2.r);
 
 bool isGreenGood = checkedColor.g >= Mathf.Min(color1.g, color2.g) && checkedColor.g <= Mathf.Max(color1.g, color2.g);
 
 bool isBlueGood = checkedColor.b >= Mathf.Min(color1.b, color2.b) && checkedColor.b <= Mathf.Max(color1.b, color2.b);
 
 bool isAlphaGood = checkedColor.a >= Mathf.Min(color1.a, color2.a) && checkedColor.a <= Mathf.Max(color1.a, color2.a);// May not need this one if you don't have an alpha channel :P
 
 return isRedGood && isGreenGood && isBlueGood && isAlphaGood;


Assuming you don't know at run-time which color has the max and min for any of the color channels. However, if you do know that, then it's even easier:

 bool isRedGood = checkedColor.r >= min.r && checkedColor.r <= max.r;
 
 bool isGreenGood = checkedColor.g >= min.g && checkedColor.g <= max.g;
 
 bool isBlueGood = checkedColor.b >= min.b && checkedColor.b <= max.b;
 
 bool isAlphaGood = checkedColor.a >= min.a && checkedColor.a <= max.a;
 
 return isRedGood && isGreenGood && isBlueGood && isAlphaGood;
Comment
Add comment · Show 6 · 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 QuantumCD · Aug 29, 2013 at 11:48 PM 0
Share

It wasn't very pretty, but it's what I was planning on doing anyway. I'll just be sure to make a nice static method. ;)

avatar image Hoeloe · Aug 29, 2013 at 11:59 PM 0
Share

You might be able to do something a little cleaner using InverseLerp, which will give you a value greater than 0 and less than 1 if a value is strictly between (and not equal to) the border values.

avatar image DESTRUKTORR · Aug 30, 2013 at 01:55 AM 0
Share

The issue there, Hoeloe, is that you'd still have to run two conditional statements for each color, in addition to doing the inverse lerp, to ensure that each color value is between 0 and 1 XD You'd just be wasting processor time, and making the code less readable by using a more complex, and ultimately unnecessary route.

avatar image Hoeloe · Aug 30, 2013 at 10:15 AM 0
Share

I only mentioned it as a possibility, I haven't actually thought through how I'd do it. It's probably more sensible to use the individual comparisons anyway, though, as these are likely faster than the InverseLerp call anyway.

avatar image DESTRUKTORR · Aug 30, 2013 at 12:18 PM 0
Share

That's fairly questionable, but the issue is that the inverselerp call returns a number that must then be checked, anyway, which means that all you've done by calling an inverselerp is change the parameter that you're running the same number of checks on from a float that directly represents the color to a float that represents the linear interpolation parameter of that float given two other floats. It still needs to be checked to see if it is between two values, lol.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Change the color of the string 2 Answers

Accessing color presets in c# script. 1 Answer

Change color of all buttons listed in array 3 Answers


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