using System;
namespace Chernobyl.Utility
{
///
/// Extension methods for types.
///
public static class ComparableExtensions
{
///
/// A method that limits the value of to the range
/// [, ], both inclusive. This method was taken
/// from http://stackoverflow.com/a/2683487 (some minor changes were made).
///
/// The type of the value to clamp, this type must implement
/// .
/// The value that is to be clamped.
/// The inclusive minimum value of .
/// The inclusive maximum value of .
/// limited to the range
/// [, ].
public static T Clamp(this T val, T min, T max) where T : IComparable
{
T value = val;
if (val.CompareTo(min) < 0)
value = min;
else if (val.CompareTo(max) > 0)
value = max;
return value;
}
}
}