This article needs additional citations for verification. (December 2009) |
In computer science, clamping, or clipping is the process of limiting a value to a range between a minimum and a maximum value. Unlike wrapping, clamping merely moves the point to the nearest available value.
Y = clamp(X, 1, 3) | |
---|---|
X | Y |
0 | 1 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 3 |
In Python, clamping can be defined as follows:
def clamp(x, minimum, maximum):
if x < minimum:
return minimum
if x > maximum:
return maximum
return x
This is equivalent to max(minimum, min(x, maximum))
for languages that support the functions min and max.