aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/numbers.js
blob: 87bbe7e2a54f5ed26b2badd62e3bda072492d8bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * Returns a number whose value is limited to the given range.
 *
 * Example: limit the output of this computation to between 0 and 255
 * (x * 255).clamp(0, 255)
 *
 * @param {Number} input
 * @param {Number} min The lower boundary of the output range
 * @param {Number} max The upper boundary of the output range
 * @returns A number in the range [min, max]
 * @type Number
 */
export function clamp(input = 0, min = 0, max = 255) {
  return Math.min(Math.max(input, min), max);
}

export default { clamp };