// --------------------------------------------------------------------------------------------------------------------
//
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
//
//
// Extension methods for classes that implement .
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor
{
using System;
///
/// Extension methods for classes that implement .
///
internal static class ComparableExtensions
{
///
/// Returns value indicating whether the given number is with in the minimum and maximum
/// given range.
///
/// The value to to check.
/// The minimum range value.
/// The maximum range value.
/// The to test.
///
/// True if the value falls within the maximum and minimum; otherwise, false.
///
public static bool IsBetween(this T value, T min, T max) where T : IComparable
{
return (value.CompareTo(min) > 0) && (value.CompareTo(max) < 0);
}
///
/// Restricts a value to be within a specified range.
///
/// The 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 to clamp.
///
/// The representing the clamped value.
///
public static T Clamp(this T value, T min, T max) where T : IComparable
{
if (value.CompareTo(min) < 0)
{
return min;
}
if (value.CompareTo(max) > 0)
{
return max;
}
return value;
}
}
}