---
title: "clamp: WGSL and GLSL examples, uses and input limits"
description: "Clamping keeps an input between a lower and an upper bound. Use it to constrain a signal or a user-controlled parameter."
url: https://typeshade.dev/reference/functions/clamp/
language: en
---

# `clamp`

Function

Clamping keeps an input between a lower and an upper bound.

## Explore the function

`y = clamp(x, -k, k)`

![clamp graph. A curve shows the output and a highlighted point follows the selected input.](https://typeshade.dev/stills/reference-function-clamp.webp)

`y` = 2 `y` = -2 `x` = -2 `x` = 2

The blue curve shows the output. The orange point marks the selected input.

Wheel to zoom; drag to pan, or rotate in 3D. Keyboard: arrows, +/-, Home to reset.

The axis labels show the displayed range. Values outside this window are not drawn.

Explore the result:

- Change k to move the symmetric bounds in this example.

**Full shader source**

[Open in the Playground](https://typeshade.dev/playground/)

clamp-plot.shade.ts

```ts
"use typeshade";

import { PlotUniforms, plot2D, plot3D } from "./function-plot.shade.ts";

class Uniforms extends PlotUniforms { k: f32; }
declare const u: uniform<Uniforms>;

// Inputs outside the mathematical domain are omitted, including after pan or zoom.
function sample(x: f32): f32 {

  return clamp(x, -u.k, u.k);
}

function trajectory(t: f32): vec3 {
  return vec3(t, sample(t), 0.);
}

@fragment
export function main(uv: location<0, vec2>): vec4 {
  const flat = plot2D(sample, uv, u, false);
  if (u.plotBlend <= 0.) { return flat; }
  const space = plot3D(trajectory, uv, u, false);
  return mix(flat, space, clamp(u.plotBlend, 0., 1.));
}
```

Filled by the page every frame, with no control: `resolution`, `plotCenter`, `plotSpan`, `plotRotation`, `plotBlend`.

## When to use it

Use it to constrain a signal or a user-controlled parameter.

Example calculation: `clamp(1.5, -1.0, 1.0) ≈ 1`

The reference result is rounded for readability. Shader output follows the precision rules of its target.

## Inputs and limits

Conditions in this example: x ∈ ℝ , k > 0

Keep the lower bound at or below the upper bound. Clamping also discards excess magnitude.

For shader input rules and precision, consult [WGSL](https://www.w3.org/TR/WGSL/) and [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf).

## Related functions

- [`min`](https://typeshade.dev/reference/functions/min/)
- [`max`](https://typeshade.dev/reference/functions/max/)
- [`saturate`](https://typeshade.dev/reference/functions/saturate/)
- [`step`](https://typeshade.dev/reference/functions/step/)

**WGSL**

`clamp(a, b, c)`

**GLSL ES 3.00**

`clamp(a, b, c)`

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Clamps `x` to the range \[low, high\], computed as `min(max(x, low), high)`, componentwise over vectors.

## Syntax

```ts
declare function clamp<V extends vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(a0: V, a1: V, a2: V): V
declare function clamp<P0 extends number, P1 extends number, P2 extends number>(a0: P0, a1: P1, a2: P2): ScalarOf<P0, P1, P2>
declare function clamp<T extends Numeric>(a0: T, a1: T, a2: T): T
```

A `number` in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

`type f32 = number & { readonly [f32Tag]?: true }`

A `number` you write as a type is inferred from its uses, and `f32`, `i32` and `u32` fix a width; [the TypeScript constructs page](https://typeshade.dev/guide/language/from-typescript/) carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

**`ScalarOf`**

`type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>`

**`Numeric`**

`type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u`

## Parameters

A parameter named by position (`a0`, `a1`) carries a name the ambient file generates. A call passes its arguments in order and names none of them.

- `a0` `V``P0``T`
- `a1` `V``P1``T`
- `a2` `V``P2``T`

## Return value

`V``ScalarOf<P0, P1, P2>``T`

## Description

Evaluated in `f64` by the CPU oracle.

## Targets

**WGSL**

`clamp(a, b, c)`

**GLSL ES 3.00**

`clamp(a, b, c)`

## Examples

- [`path/palette/palette.shade.ts`](https://typeshade.dev/guide/examples/palette/)
- [`path/image-blur/image-blur.shade.ts`](https://typeshade.dev/guide/examples/image-blur/)
- [`path/histogram/histogram.shade.ts`](https://typeshade.dev/guide/examples/histogram/)
- [`path/ray-tracer/ray-tracer.shade.ts`](https://typeshade.dev/guide/examples/ray-tracer/)
- [`gallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.ts`](https://typeshade.dev/guide/examples/capsule-corp-namek-class/)
- [`gallery/domain-warp-twin/domain-warp-twin.shade.ts`](https://typeshade.dev/guide/examples/domain-warp-twin/)
- [`gallery/hillshade-twin/hillshade-twin.shade.ts`](https://typeshade.dev/guide/examples/hillshade-twin/)
- [`gallery/ocean-twin/ocean-twin.shade.ts`](https://typeshade.dev/guide/examples/ocean-twin/)
- [`gallery/tunnel-twin/tunnel-twin.shade.ts`](https://typeshade.dev/guide/examples/tunnel-twin/)
- [`gallery/atomic-histogram/atomic-histogram.shade.ts`](https://typeshade.dev/guide/examples/atomic-histogram/)
- [`gallery/closures/closures.shade.ts`](https://typeshade.dev/guide/examples/closures/)
- [`gallery/cube-array-gather/cube-array-gather.shade.ts`](https://typeshade.dev/guide/examples/cube-array-gather/)
- [`gallery/cube-env/cube-env.shade.ts`](https://typeshade.dev/guide/examples/cube-env/)
- [`gallery/packing-bitcast/packing-bitcast.shade.ts`](https://typeshade.dev/guide/examples/packing-bitcast/)
- [`gallery/textured-quad/textured-quad.shade.ts`](https://typeshade.dev/guide/examples/textured-quad/)
- [`gallery/uniform-array/uniform-array.shade.ts`](https://typeshade.dev/guide/examples/uniform-array/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#maths)
- [`clamp()` in `typeshade/core/ir`](https://typeshade.dev/api/clamp/)
