code: public static double Clamp(double x, double min, double max) {
          if (x < min)
              return min;
          if (x > max)
              return max;
          return x;
      }
docs: Clamp values.
  
  @param x   Value.
  @param min Minimum value.
  @param max Maximum value.
  @return Value.