namespace GenericImage
{
public static class ImageMaths
{
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value. If value is less than min, min will be returned.
/// The maximum value. If value is greater than max, max will be returned.
/// The clamped value.
public static float Clamp(float value, float min, float max)
{
// This compare order is very important!!!
// We must follow HLSL behavior in the case user specified min value is bigger than max value.
// First we check to see if we're greater than the max
value = (value > max) ? max : value;
// Then we check to see if we're less than the min.
value = (value < min) ? min : value;
// There's no check to see if min > max.
return value;
}
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value. If value is less than min, min will be returned.
/// The maximum value. If value is greater than max, max will be returned.
/// The clamped value.
public static int Clamp(int value, int min, int max)
{
value = (value > max) ? max : value;
value = (value < min) ? min : value;
return value;
}
}
}