For one, your two examples seem to have different colour scales.
Lets say that the scale is this:
1 2 3
|
0.0 blue
0.5 green
1.0 red
|
That table could be in a std::map.
The map has upper_bound()
http://www.cplusplus.com/reference/map/map/upper_bound/
Test lookups with upper_bound:
k<0.0 it==begin() (i.e. blue)
0.0<=k<0.5 (green)
0.5<=k<1.0 (red)
1.0<=k it==end()
Edge cases:
if ( it==begin() ) use *begin() (blue)
if ( it==end() ) use *rbegin() (red)
Otherwise look at element and previous. Say k=0.4
We look at (0.0, blue) and (0.5, green).
0.4 is at 80% from 0.0 to 0.5
Interpolate:
colour = 0.8 * green + 0.2 * blue
Lets make this more illustrative by changing the 0.5 colour to (200,200,200):
1 2 3
|
colour = 0.8 * (200,200,200) + 0.2 * (0,0,255)
= (160,160,160) + (0,0,51)
= (160,160,211)
|