Ok, as we know lerping between 2 values is easy however it seems lerping between more than 2 is... well easy but usually means a heap of branches and/or mucking around.
I had the need to pass in a time value 0.0f to 1.0f and getting back an interpolated float4 color that went from white -> yellow -> orange -> red -> brown -> gray -> black. Basically the 0.0 to 1.0 was the percentage of an explosion duration and the color is used to tint it.
Originally I had a heap of branches and broke it up into 6 conditional lerps. However the code is in a particle system so rather not be adding heaps of branches per particle.
My solution, a small bit of code and a lookup table. The table could be static or filled on the fly.
The code below uses the DX XMVector type but the logic will work just as well for other similar classes.
#include <cmath>
// sample data array of colors
const XMVector arr_col_temp[] = {
XMFLOAT4(1, 0.8f, 0.8f, 1), // white
XMFLOAT4(1, 1, 145.0f / 255.0f, 1), // yellow
XMFLOAT4(1, 0.8f, 0, 1), // orange
XMFLOAT4(1, 0, 0, 1), // red
XMFLOAT4(99 / 255.0f, 37 / 255.0f, 19 / 255.0f, 1), // brown
XMFLOAT4(42 / 255.0f, 42 / 255.0f, 42 / 255.0f, 1), // gray
XMFLOAT4(0.2f, 0, 0, 1), // black
XMFLOAT4(0.2f, 0, 0, 1) // black ... last one needs to be duplicated
};
inline XMVector ColorTemp(float t) // t must be 0.0 to 1.0
{
float i_int;
float i_frac = modf(t * (ARRAYSIZE(arr_col_temp) - 2), &i_int);
int i1 = i_int;
return XMVectorLerp(arr_col_temp[i1], arr_col_temp[i1+1], i_frac);
}
This will do a "linear" lerp between the values however if you want a curved fit then just adjust the passed in value.
For example.
XMVector c = ColorTemp(sqrtf(t));