clamp static method

Value clamp(
  1. Object min, [
  2. Object? value,
  3. Object? max
])

Creates a clamp() calculation with the given min, value, and max.

Each argument must be either a SassNumber, a SassCalculation, an unquoted SassString, or a CalculationOperation.

This automatically simplifies the calculation, so it may return a SassNumber rather than a SassCalculation. It throws an exception if it can determine that the calculation will definitely produce invalid CSS.

This may be passed fewer than three arguments, but only if one of the arguments is an unquoted var() string.

Implementation

static Value clamp(Object min, [Object? value, Object? max]) {
  if (value == null && max != null) {
    throw ArgumentError("If value is null, max must also be null.");
  }

  min = _simplify(min);
  value = value.andThen(_simplify);
  max = max.andThen(_simplify);

  if (min is SassNumber &&
      value is SassNumber &&
      max is SassNumber &&
      min.hasCompatibleUnits(value) &&
      min.hasCompatibleUnits(max)) {
    if (value.lessThanOrEquals(min).isTruthy) return min;
    if (value.greaterThanOrEquals(max).isTruthy) return max;
    return value;
  }

  var args = List<Object>.unmodifiable(
      [min, if (value != null) value, if (max != null) max]);
  _verifyCompatibleNumbers(args);
  _verifyLength(args, 3);
  return SassCalculation._("clamp", args);
}