Clamp
Constrain a value to a given range
Clamp restricts a value to stay within defined bounds. If the input falls below the lower limit, the lower limit is returned. If it exceeds the upper limit, the upper limit is returned. Otherwise the value passes through unchanged.
The result is a flat-bottomed, flat-topped curve — a hard limit on both ends. This is essential for preventing values from going out of bounds: pixel coordinates that must stay on screen, audio levels that must not clip, or opacity values that must remain between 0 and 1.
Clamp is often used as a safety net after other operations. Apply a remap or a math expression, then clamp the result to guarantee it stays in a valid range.
function clamp(min: number, max: number, value: number): number {
return Math.min(Math.max(min, value), max);
}