mapValue function

double mapValue(
  1. double value,
  2. double minIn,
  3. double maxIn,
  4. double minOut,
  5. double maxOut,
)

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