Comments and answers for "Convert List of Floats to Colors (Color Map)"
http://answers.unity.com/questions/1552114/convert-list-of-floats-to-colors-color-map.html
The latest comments and answers for the question "Convert List of Floats to Colors (Color Map)"Answer by tuf91497
http://answers.unity.com/answers/1552609/view.html
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;
}Thu, 13 Sep 2018 00:11:08 GMTtuf91497Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1552595/view.html
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 ^^Wed, 12 Sep 2018 23:25:19 GMTBunny83Comment by elenzil on elenzil's answer
http://answers.unity.com/comments/1552513/view.html
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".Wed, 12 Sep 2018 18:35:40 GMTelenzilAnswer by Bunny83
http://answers.unity.com/answers/1552159/view.html
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.
<br>
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
}Tue, 11 Sep 2018 20:45:09 GMTBunny83