1/*  File:    canny/clamp.pl
    2    Author:  Roy Ratcliffe
    3    Created: Jun 20 2026
    4    Purpose: Clamp a value between a minimum and maximum
    5
    6Copyright (c) 2026, Roy Ratcliffe, Northumberland, United Kingdom
    7
    8Permission is hereby granted, free of charge,  to any person obtaining a
    9copy  of  this  software  and    associated   documentation  files  (the
   10"Software"), to deal in  the   Software  without  restriction, including
   11without limitation the rights to  use,   copy,  modify,  merge, publish,
   12distribute, sub-license, and/or sell copies  of   the  Software,  and to
   13permit persons to whom the Software is   furnished  to do so, subject to
   14the following conditions:
   15
   16    The above copyright notice and this permission notice shall be
   17    included in all copies or substantial portions of the Software.
   18
   19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT  WARRANTY OF ANY KIND, EXPRESS
   20OR  IMPLIED,  INCLUDING  BUT  NOT   LIMITED    TO   THE   WARRANTIES  OF
   21MERCHANTABILITY, FITNESS FOR A PARTICULAR   PURPOSE AND NONINFRINGEMENT.
   22IN NO EVENT SHALL THE AUTHORS  OR   COPYRIGHT  HOLDERS BE LIABLE FOR ANY
   23CLAIM, DAMAGES OR OTHER LIABILITY,  WHETHER   IN  AN ACTION OF CONTRACT,
   24TORT OR OTHERWISE, ARISING FROM,  OUT  OF   OR  IN  CONNECTION  WITH THE
   25SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   26
   27*/
   28
   29:- module(canny_clamp,
   30          [ clamp/4 % +Min, +Max, +Unclamped, -Clamped
   31          ]).
 clamp(+Min, +Max, +Unclamped, -Clamped) is det
Clamp a value between a minimum and maximum.

This predicate takes an input value (Unclamped) and ensures that it falls within the specified minimum (Min) and maximum (Max) bounds. If Unclamped is less than Min, Clamped will be unified with Min. If Unclamped is greater than Max, Clamped will be unified with Max. If Unclamped is between Min and Max, Clamped will be unified with Unclamped. The predicate is deterministic and will always succeed with a single solution.

Arguments:
Min- The minimum value that the result can be.
Max- The maximum value that the result can be.
Unclamped- The value to be clamped.
Clamped- The result of clamping Unclamped between Min and Max.
   49clamp(Min, Max, Unclamped, Clamped) :- Clamped is min(Max, max(Min, Unclamped))