Clamp (function)
{{Short description|Limiting a position to an area}}
{{More citations needed|date=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.
class="wikitable" style="margin:0 0 0 1em; text-align:right; float:right;" | |
colspan="2" | 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 {{code |lang=python |max(minimum, min(x, maximum))}} 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
{{cite web |url=https://pandas.pydata.org/docs/reference/api/pandas.Series.clip.html |title=Pandas Series.clip method documentation |access-date=2023-10-15}} and DataFrame.clip
{{cite web |url=https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.clip.html |title=Pandas DataFrame.clip method documentation |access-date=2023-10-15}} methods. The NumPy library offers the clip
{{cite web|url=https://numpy.org/doc/stable/reference/generated/numpy.clip.html |title=NumPy clip function documentation |access-date=2023-10-15}} function. In the Wolfram Language, it is implemented as {{code |lang=Mathematica |Clip[x, {minimum, maximum}]}}.{{cite web |url=https://reference.wolfram.com/language/ref/Clip.html |title=Wolfram Language Clip function documentation |access-date=2023-10-15}}
In OpenGL, the glClearColor
function takes four GLfloat
values which are then 'clamped' to the range .{{Cite web|url=https://www.khronos.org/registry/OpenGL-Refpages/gl4/|title=OpenGL 4 Reference Pages|website=www.khronos.org|access-date=2018-10-31}}
One of the many uses of clamping in computer graphics is the placing of a detail inside a polygon—for example, a bullet hole on a wall. It can also be used with wrapping to create a variety of effects.
In CSS, clamp()
{{cite web |title=clamp() |url=https://developer.mozilla.org/en-US/docs/Web/CSS/clamp |website=MDN Web Docs |publisher=Mozilla}} can help to implement responsive typography or responsive designs generally.{{cite web |last1=Bece |first1=Adrian |title=Modern Fluid Typography Using CSS Clamp |url=https://www.smashingmagazine.com/2022/01/modern-fluid-typography-css-clamp/ |website=Smashing Magazine |publisher=Smashing Media AG |access-date=29 January 2025}}
Although spreadsheets like Excel, Open Office Calc, or Google Sheets don't provide a clamping function directly, the same effect can be achieved by using functions like MAX
& MIN
together, by MEDIAN
,{{cite web |last1=Citi |first1=Jasper |title=How do I constrain a value to a range in excel? |url=https://stackoverflow.com/a/44716590/1024811 |website=Stack Overflow |access-date=29 January 2025}}{{cite web |title=Clamp function in Excel |url=https://www.excelforum.com/excel-general/781864-clamp-function-in-excel.html |website=Excel Forum |access-date=29 January 2025}} or with cell function macros.{{cite web |title=[Solved] Math Clamp Function? |url=https://forum.openoffice.org/en/forum/viewtopic.php?t=90037 |website=Apache OpenOffice Community Forum |access-date=29 January 2025}} When attempting to do a clamp where the input is an array, other methods must be used.{{cite web |title=Array-Safe Clamp Value in Google Sheets |url=https://webapps.stackexchange.com/questions/80921/array-safe-clamp-value-in-google-sheets |website=Stack Exchange > Web Applications |publisher=Stack Overflow |access-date=29 January 2025}}
References
{{Reflist}}
{{DEFAULTSORT:Clamping (Graphics)}}
Category:Computer graphics algorithms
Category:Articles with example Python (programming language) code
{{Compu-graphics-stub}}