Can the map command be used to “reverse ranges”. What I mean is can a value in the range 0-10 be mapped to 255-0? I.e., if x=2, then map(2,0,10,255,0) would return 205.
Yes. Did you try it?
Looks like it should work.
map (from WMath.cpp) is just:
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Yes that works,
Please not that if you have a 0 in the map parameter list the formula becomes simpler. Two 0's even more simple. That could be important if you need performance.
x = map(2, 0, 10, 255, 0) // spaces makes it better readable
==>
x = 255 - 10 * 25.5;
If performance is an issue, you might have a look at my fastmap class, that does some precalculations to minimize the math esp. remove the "expensive" divisions from code.