clampWeights static method

List<int> clampWeights({
  1. required List<int> pattern,
  2. required double viewport,
  3. required double smallMin,
  4. required double smallMax,
  5. required double largeMax,
})

Clamps small slots to smallMin–smallMax and large slots to largeMax.

The leftover width is shared by the other slots so the row still fills viewport.

Implementation

static List<int> clampWeights({
  required List<int> pattern,
  required double viewport,
  required double smallMin,
  required double smallMax,
  required double largeMax,
}) {
  if (pattern.isEmpty || viewport <= 0) {
    return pattern;
  }
  final int sum = pattern.fold<int>(0, (int a, int b) => a + b);
  if (sum <= 0) {
    return pattern;
  }
  final int minWeight = pattern.reduce(math.min);
  final int maxWeight = pattern.reduce(math.max);
  final double unit = viewport / sum;
  final slots = <double>[for (final int weight in pattern) weight * unit];
  final small = <bool>[for (final int weight in pattern) weight == minWeight];
  final large = <bool>[
    for (final int weight in pattern)
      weight == maxWeight && weight != minWeight,
  ];

  _clampExtremeSlots(
    slots: slots,
    small: small,
    large: large,
    smallMin: smallMin,
    smallMax: smallMax,
    largeMax: largeMax,
  );

  final List<int> targets = _receiverIndices(small: small, large: large);
  _shareRemainingWidth(
    slots: slots,
    targets: targets,
    viewport: viewport,
    large: large,
    largeMax: largeMax,
  );

  return _roundSlots(slots: slots, targets: targets, viewport: viewport);
}