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?
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
}
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;
}