In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied 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.
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 for languages that support the functions min and max.
Uses
Several programming languages and libraries provide functions for fast and vectorized clamping. In Python, the
pandas library offers the
Series.clip
and
DataFrame.clip
methods. The
NumPy library offers the
clip
function. In the
Wolfram Language
The Wolfram Language ( ) is a proprietary, very high-level multi-paradigm programming language developed by Wolfram Research. It emphasizes symbolic computation, functional programming, and rule-based programming and can employ arbitrary stru ...
, it is implemented as .
In
OpenGL
OpenGL (Open Graphics Library) is a Language-independent specification, cross-language, cross-platform application programming interface (API) for rendering 2D computer graphics, 2D and 3D computer graphics, 3D vector graphics. The API is typic ...
, the
glClearColor
function takes four
GLfloat
values which are then 'clamped' to the range