mapValue function
Re-maps a value from one range into another. That is, a value
between the
range minIn
- maxIn
will be mapped to the range minOut
- maxOut
,
the value
will be clamped in case is not inside its range minIn
- maxIn
Implementation
double mapValue(
double value,
double minIn,
double maxIn,
double minOut,
double maxOut,
) {
assert(minIn < maxIn,
"The minimum value of the given range must be less than its maximum value");
assert(minOut < maxOut,
"The minimum value of the given range must be less than its maximum value");
double finalValue = value;
if (value > maxIn) {
finalValue = maxIn;
} else if (value < minIn) {
finalValue = minIn;
}
final double result =
maxOut - ((maxIn - finalValue) / (maxIn - minIn)) * (maxOut - minOut);
return result;
}