# Clamp (function)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Clamp_(function)
> Markdown URL: https://mediated.wiki/source/Clamp_(function).md
> Source: https://en.wikipedia.org/wiki/Clamp_(function)
> Source revision: 1333878303
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Limiting a position to an area

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Clamp" function – news · newspapers · books · scholar · JSTOR (December 2009) (Learn how and when to remove this message)

In [computer science](/source/Computer_science), **clamping**, or **clipping** is the process of limiting a value to a range between a minimum and a maximum value. Unlike [wrapping](/source/Wrapping_(graphics)), Clamping moves a value to the nearest boundary if it is outside the allowed range.

Y = clamp(X, 1, 3) X Y 0 1 1 1 2 2 3 3 4 3

In [Python](/source/Python_(programming_language)), 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**.

## Uses

Several programming languages and libraries provide functions for fast and vectorized clamping. In Python, the [pandas](/source/Pandas_(software)) library offers the Series.clip[1] and DataFrame.clip[2] [methods](/source/Method_(computer_programming)). The [NumPy](/source/NumPy) library offers the clip[3] function. In the [Wolfram Language](/source/Wolfram_Language), it is implemented as Clip[x, {minimum, maximum}].[4]

In [OpenGL](/source/OpenGL), the glClearColor function takes four GLfloat values which are then 'clamped' to the range [ 0 , 1 ] {\displaystyle [0,1]} .[5]

One of the many uses of clamping in [computer graphics](/source/3D_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](/source/Wrapping_(graphics)) to create a variety of effects.

In CSS, clamp()[6] can help to implement responsive typography or responsive designs generally.[7]

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,[8][9] or with cell function macros.[10] When attempting to do a clamp where the input is an array, other methods must be used.[11]

## References

1. **[^](#cite_ref-1)** ["Pandas Series.clip method documentation"](https://pandas.pydata.org/docs/reference/api/pandas.Series.clip.html). Retrieved 2023-10-15.

1. **[^](#cite_ref-2)** ["Pandas DataFrame.clip method documentation"](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.clip.html). Retrieved 2023-10-15.

1. **[^](#cite_ref-3)** ["NumPy clip function documentation"](https://numpy.org/doc/stable/reference/generated/numpy.clip.html). Retrieved 2023-10-15.

1. **[^](#cite_ref-4)** ["Wolfram Language Clip function documentation"](https://reference.wolfram.com/language/ref/Clip.html). Retrieved 2023-10-15.

1. **[^](#cite_ref-5)** ["OpenGL 4 Reference Pages"](https://www.khronos.org/registry/OpenGL-Refpages/gl4/). *www.khronos.org*. Retrieved 2018-10-31.

1. **[^](#cite_ref-6)** ["clamp()"](https://developer.mozilla.org/en-US/docs/Web/CSS/clamp). *MDN Web Docs*. Mozilla.

1. **[^](#cite_ref-7)** Bece, Adrian. ["Modern Fluid Typography Using CSS Clamp"](https://www.smashingmagazine.com/2022/01/modern-fluid-typography-css-clamp/). *Smashing Magazine*. Smashing Media AG. Retrieved 29 January 2025.

1. **[^](#cite_ref-8)** Citi, Jasper. ["How do I constrain a value to a range in excel?"](https://stackoverflow.com/a/44716590/1024811). *Stack Overflow*. Retrieved 29 January 2025.

1. **[^](#cite_ref-9)** ["Clamp function in Excel"](https://www.excelforum.com/excel-general/781864-clamp-function-in-excel.html). *Excel Forum*. Retrieved 29 January 2025.

1. **[^](#cite_ref-10)** ["\[Solved\] Math Clamp Function?"](https://forum.openoffice.org/en/forum/viewtopic.php?t=90037). *Apache OpenOffice Community Forum*. Retrieved 29 January 2025.

1. **[^](#cite_ref-11)** ["Array-Safe Clamp Value in Google Sheets"](https://webapps.stackexchange.com/questions/80921/array-safe-clamp-value-in-google-sheets). *Stack Exchange > Web Applications*. Stack Overflow. Retrieved 29 January 2025.

## Further reading

- Apodaca, Anthony A.; Gritz, Larry (Dec 1999). [*Advanced RenderMan: Creating CGI for Motion Pictures*](https://books.google.com/books?id=6_4VaJiOx7EC&pg=PA177). Morgan Kaufmann.

This computer graphics–related article is a stub. You can help Wikipedia by adding missing information.

- [v](https://en.wikipedia.org/wiki/Template:Compu-graphics-stub)
- [t](/source/Template_talk%3ACompu-graphics-stub)
- [e](https://en.wikipedia.org/wiki/Special:EditPage/Template:Compu-graphics-stub)

---
Adapted from the Wikipedia article [Clamp (function)](https://en.wikipedia.org/wiki/Clamp_(function)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Clamp_(function)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
