mapRange method

double mapRange({
  1. required int fromMin,
  2. required int fromMax,
  3. required double toMin,
  4. required double toMax,
})

Maps this value from one range fromMin, fromMax to another toMin, toMax.

Example:

int value = 50;
double mapped = value.mapRange(fromMin: 0, fromMax: 100, toMin: 0.0, toMax: 1.0); // 0.5

Implementation

double mapRange({
  required int fromMin,
  required int fromMax,
  required double toMin,
  required double toMax,
}) {
  if (fromMax == fromMin) return toMin;
  final normalized = (this - fromMin) / (fromMax - fromMin);
  return toMin + normalized * (toMax - toMin);
}