mod function

double mod(
  1. double value,
  2. List<double> a,
  3. List<double> b, {
  4. bool clamp = false,
})

Modulate a value between two ranges

Implementation

double mod(double value, List<double> a, List<double> b, {bool clamp = false}) {
  final lh = b[0] < b[1] ? [b[0], b[1]] : [b[1], b[0]];
  final result = b[0] + ((value - a[0]) / (a[1] - b[0])) * (b[1] - b[0]);

  if (clamp) {
    if (result < lh.first) return lh.first;
    if (result > lh.last) return lh.last;
  }

  return result;
}