---
title: "clamp"
description: "The value limited to the range `lo`..`hi`."
group: "space"
groupTitle: "Shapes, frames & warps"
categoryId: "math"
categorySlug: "math"
categoryTitle: "Math"
kind: "function"
sourceUrl: "https://github.com/shader-effects-inc/shaders/blob/main/packages/core/src/std/math.ts#L258"
---

# clamp

The value limited to the range `lo`..`hi`.

```ts
clamp(x: Val, lo: Val, hi: Val): Expr
```

Import `math` from `shaders/std` and use `math.clamp`.

## Parameters

| Name | Type | Notes |
|---|---|---|
| `x` | `Val` |  |
| `lo` | `Val` |  |
| `hi` | `Val` |  |

## Example

```ts
const alpha = clamp(add(body, ring), 0, 1)
```

## See also

- [`min`](/docs/primitives/math/min) — The smaller of two values.
- [`max`](/docs/primitives/math/max) — The larger of two values.

## Used by

[Aurora](/docs/components/aurora), [BrushedMetal](/docs/components/brushedmetal), [CarbonFiber](/docs/components/carbonfiber), [Chrome](/docs/components/chrome), [CompressionArtifacts](/docs/components/compressionartifacts), [Crystal](/docs/components/crystal), [FlowingGradient](/docs/components/flowinggradient), [Frost](/docs/components/frost), [Goo](/docs/components/goo), [Heatmap](/docs/components/heatmap), [Hologram](/docs/components/hologram), [Holographic](/docs/components/holographic), [InkFlow](/docs/components/inkflow), [LiquidMetal](/docs/components/liquidmetal), [MeshGradient](/docs/components/meshgradient), [Nebula](/docs/components/nebula), [PixelThrow](/docs/components/pixelthrow), [Plastic](/docs/components/plastic), [Prism](/docs/components/prism), [ReactionDiffusion](/docs/components/reactiondiffusion), [Satin](/docs/components/satin), [SmokeFill](/docs/components/smokefill), [StudioBackground](/docs/components/studiobackground), [Surface3D](/docs/components/surface3d), [Voxels](/docs/components/voxels), [Water](/docs/components/water), [Waveform](/docs/components/waveform)