• 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
Question by tuf91497 · Sep 11, 2018 at 06:43 PM · scripting problemcolormath

Convert List of Floats to Colors (Color Map)

So, I have an arbitrarily long list of arbitrary float values. I need to write a function that can take in this list of floats return colors for each float such that the maximum float is pure red, the minimum float is pure blue, and the middle of the range is pure white. i.e.


 Float value =>        max         (max - min)/2           min
 RGB value =>    (1, 0, 0, 100) -> (1, 1, 1, 100) -> (0, 0, 1, 100)
 Color name =>         red             white               blue
  Hex Value =>        FF0000           FFFFFF              0000FF

I have this equation to scale a range [min, max] to a new range [a, b]:

          (b - a)(x - min)  
  f(x) =  ----------------  + a
             max - min

But I'm feeling stuck as to what an appropriate range is to scale to. Do I need to just scale it twice, once for the bottom half of values and once for the top half of values?

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Bunny83 · Sep 11, 2018 at 08:45 PM

It's not clear what you mean by max and min float values. Are they arbitrary limits you set? Because floats actually have a huge dynamic range otherwise.


Assuming min and max are some constants you have to do something like this:

 public static Color InterpolateColor(float aInput)
 {
     float range = max - min;
     float f = 2f * (aInput - min) / range; // float in the range of 0 - 2
     if (f > 1)
         return Color.Lerp(Color.white, Color.red, f - 1f); // top half
     return Color.Lerp(Color.blue, Color.white, f); // bottom half
 }

Comment
tuf91497

People who like this

1 Show 2 · 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 elenzil · Sep 12, 2018 at 06:35 PM 0
Share

i think red and blue may be swapped in this, if red should correspond to the maximum float. the OP's diagram is a bit confusing because they wrote "max, middle, min".

avatar image Bunny83 elenzil · Sep 12, 2018 at 11:25 PM 0
Share

Yes you're right. I somehow assumed that the min value was the first one and the max value was the last one. However he has it the other way round. Though currently I'm not home and i'm writing on a panasonic TV so the editing capabilities are quite poor ^^

avatar image

Answer by tuf91497 · Sep 13, 2018 at 12:11 AM

So this ended up being my solution, thank you anyways Bunny83
btw, what I meant in my case is that I get a text file of ASCII format data that contains on each line a float value that represents the "magnitude" of one data point. I have a script that reads these values into a list of floats, since I don't necessarily even know the length of the file. I don't know any of the values, but they adhere to the range that a float can be. I need to convert this list of floats to a list of colors. In this case the data is temperature values, hence red->white->blue, but it doesn't need to be.

So this ended up essentially being my code:

     public List<Color> ColorMap_RedWhiteBlue(List<float> colorFloatList)
     {
         // Uses this function to scale a range [min, max] to a new range [a, b]
         //          (b - a)(x - min)  
         //  f(x) =  ----------------  + a
         //             max - min

         //Find max, min, and mid point
         float max = colorFloatList.Max();
         float min = colorFloatList.Min();
         float mid = (max - min) / 2f;
         float scaledValue;
         List<Color> colorList = new List<Color>();
 
         //Produce color for every point in the list
         foreach(float point in colorFloatList)
         {
             //Split the values into two groups: below the middle value and above the middle value
             if((point >= min) && (point <= mid))  //Bottom half of range (mid is                                                                            '                                                   now the maximum for this range)
             {
                 //Scale to range (0, 1). 1 (max) makes white, 0  (min) makes blue
                 scaledValue = (((point - min)) / (mid - min));
                 Color color = new Color(scaledValue, scaledValue, 1);
             }else if((point > mid) && (point <= max)) //Top half of range (mid 
                                                       is now the minimum for this range)
             {
                 //Scale to range (0, 1), flip it with (1 - x). 1 (min) makes white, 0 (max) makes red
                 scaledValue = 1 - (((point - mid)) / (max - mid));
                 Color color = new Color(1, scaledValue, scaledValue);
             } 
             colorList.Add(color);
        }
        return colorList;
    }





Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

164 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 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 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 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 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 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 avatar image avatar image

Related Questions

How to build a car script that allows for car to turn in full circles 1 Answer

Clamp a Vector above minimum angle from another Vector? 0 Answers

Create new mesh from script always black color even i change material. 1 Answer

Help with some math! 1 Answer

get color of randomly generated objects 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