Clamp
Integer
CLAMP
The CLAMP node constrains an input integer value to a specified range defined by a minimum and maximum value. If the input value falls within the range, it is passed through unchanged. If the input value is less than the minimum or greater than the maximum, it is “clamped” to the nearest boundary value.
Inputs
value
(integer): The input value to be clamped. Default is 0.min
(integer): The minimum allowed value. Default is 0.max
(integer): The maximum allowed value. Default is 0.
Outputs
result
(integer): The clamped output value.
Configuration
This node has no configuration options.
Usage
- Connect an integer value to the
value
input. This is the value you want to constrain to the specified range. - Set the
min
input to the minimum allowed value for the output. - Set the
max
input to the maximum allowed value for the output. - The
result
output will emit the input value if it’s within the [min, max] range. If the input is less thanmin
,result
will bemin
. If the input is greater thanmax
,result
will bemax
.
Example
Let’s say you have a spell that generates a random integer between -100 and 100, but you need to ensure the value is always between 0 and 50 before using it. You can use the CLAMP node like this:
- Connect the random integer generator to the
value
input of CLAMP. - Set the CLAMP
min
input to 0. - Set the CLAMP
max
input to 50. - Connect the
result
output to the rest of your spell.
Now, any values less than 0 will be raised to 0, and any values greater than 50 will be reduced to 50. Values already in the [0, 50] range will pass through unchanged.
Tips
- CLAMP is useful any time you need to constrain a value to a known range. This is common when working with parameters that have legal bounds, like percentages (0-100), RGB color values (0-255), etc.
- Remember that CLAMP is inclusive of the
min
andmax
values. If you need an exclusive range, adjust your bounds accordingly. - If
min
is greater thanmax
, CLAMP will swap their values internally so thatmin
is always less than or equal tomax
.
Cautions
- Both the input value and the
min
/max
bounds must be integers. For floating-point clamping, use the CLAMP (Float) node instead. - Be aware that clamping discards out-of-bounds values rather than scaling or normalizing them. If you need to fit a range of values proportionally into a different range, consider using a map or lerp node instead.