# clamp( )

The expression clamp( ) allows you to restrict a value to within a specified range. If a clamped value exceeds the range in either direction, it returns whichever is closest, the minimum or maximum specified limit.

## Expression form:

clamp(value_source, limit1, limit2)

value_source is a number or array returned from the source identified in this part of the expression. This source can be a property value, variable value, expression control value, or other identified source that returns a number or numerical array.

limit1 is a number or array.

limit2 is a number or array.

### Expression explained:

The value_source  must fall within the range of limit1 through limitor it gets modified to the closest of the two limits. For example; if limit1 and limitare 15 and 40 respectively, and value_source is 10, then  the expression will return 15 as that is the lowest value allowed.

### Returns:

A number or numerical array.

### Example 1

You have two shape layers: Shape Layer 1 and Shape Layer 2. You place the following expression in Shape Layer 2‘s opacity property:

`clamp(thisComp.layer("Shape Layer 1").transform.opacity, 40, 80)`

value_source becomes Shape Layer 1‘s opacity property.

limit1 and limit2 are 40 and 80 respectively.

#### EFFECT

Because limit1 is set to 40, if Shape Layer 1‘s opacity falls below 40%, Shape Layer 2‘s opacity will remain at 40%. Likewise, because limit2 is set to 80, if Shape Layer 1‘s opacity goes above 80%, Shape Layer 2‘s opacity will remain at 80%. Otherwise, Shape Layer 2’s opacity property will dutifully mimic that of Shape Layer 1.

### Example 2

A shape layer has an oval at position [937, 572], and the following expression is placed in its position property:

```SP = [937, 572];
P = clamp(transform.position, SP + [-100, -100], SP + [100, 100]);
P```

The variable SP (for starting position) is created to hold the starting position of the oval.

The variable P is assigned the new position generated by the clamp expression.

value_source is the shape layer’s own position property [X, Y] value.

limit1 is set to the original starting position (SP) plus an X value of -100 and a Y value of -100; written SP+[-100, -100].

limit2 is set to the original starting position (SP) plus an X value of 100 and a Y value of 100; written SP+[100, 100].

The oval will use the [X, Y] array held by P as its position property.

#### EFFECT:

As you move the oval in the composition panel it will be confined to within a 200 pixel square area, with its starting point in the center. This is because we limited the range of both the X and Y values to a -100 through 100 pixels range.