diff --git a/X10D/DoubleExtensions.cs b/X10D/DoubleExtensions.cs
index 5b24f1c..1dd50e7 100644
--- a/X10D/DoubleExtensions.cs
+++ b/X10D/DoubleExtensions.cs
@@ -11,6 +11,18 @@
///
public static class DoubleExtensions
{
+ ///
+ /// Clamps a value between a minimum and a maximum value.
+ ///
+ /// The value to clamp.
+ /// The minimum value.
+ /// The maximum value.
+ /// Returns if is greater than it,
+ /// if is less than it,
+ /// or itself otherwise.
+ public static double Clamp(this double value, double min, double max) =>
+ Math.Min(Math.Max(value, min), max);
+
///
/// Converts an angle from degrees to radians.
///
diff --git a/X10D/Int16Extensions.cs b/X10D/Int16Extensions.cs
index cfcb0c3..5e8d0fb 100644
--- a/X10D/Int16Extensions.cs
+++ b/X10D/Int16Extensions.cs
@@ -52,6 +52,30 @@
#endregion
+ ///
+ /// Clamps a value between a minimum and a maximum value.
+ ///
+ /// The value to clamp.
+ /// The minimum value.
+ /// The maximum value.
+ /// Returns if is greater than it,
+ /// if is less than it,
+ /// or itself otherwise.
+ public static short Clamp(this short value, short min, short max) =>
+ Math.Min(Math.Max(value, min), max);
+
+ ///
+ /// Clamps a value between a minimum and a maximum value.
+ ///
+ /// The value to clamp.
+ /// The minimum value.
+ /// The maximum value.
+ /// Returns if is greater than it,
+ /// if is less than it,
+ /// or itself otherwise.
+ public static ushort Clamp(this ushort value, ushort min, ushort max) =>
+ Math.Min(Math.Max(value, min), max);
+
///
/// Converts the to a treating it as a Unix timestamp.
///
diff --git a/X10D/Int32Extensions.cs b/X10D/Int32Extensions.cs
index 3112126..8f03a5d 100644
--- a/X10D/Int32Extensions.cs
+++ b/X10D/Int32Extensions.cs
@@ -52,6 +52,30 @@
#endregion
+ ///
+ /// Clamps a value between a minimum and a maximum value.
+ ///
+ /// The value to clamp.
+ /// The minimum value.
+ /// The maximum value.
+ /// Returns if is greater than it,
+ /// if is less than it,
+ /// or itself otherwise.
+ public static int Clamp(this int value, int min, int max) =>
+ Math.Min(Math.Max(value, min), max);
+
+ ///
+ /// Clamps a value between a minimum and a maximum value.
+ ///
+ /// The value to clamp.
+ /// The minimum value.
+ /// The maximum value.
+ /// Returns if is greater than it,
+ /// if is less than it,
+ /// or itself otherwise.
+ public static uint Clamp(this uint value, uint min, uint max) =>
+ Math.Min(Math.Max(value, min), max);
+
///
/// Converts the to a treating it as a Unix timestamp.
///
diff --git a/X10D/Int64Extensions.cs b/X10D/Int64Extensions.cs
index 8c46b64..84d6284 100644
--- a/X10D/Int64Extensions.cs
+++ b/X10D/Int64Extensions.cs
@@ -49,6 +49,30 @@
#endregion
+ ///
+ /// Clamps a value between a minimum and a maximum value.
+ ///
+ /// The value to clamp.
+ /// The minimum value.
+ /// The maximum value.
+ /// Returns if is greater than it,
+ /// if is less than it,
+ /// or itself otherwise.
+ public static long Clamp(this long value, long min, long max) =>
+ Math.Min(Math.Max(value, min), max);
+
+ ///
+ /// Clamps a value between a minimum and a maximum value.
+ ///
+ /// The value to clamp.
+ /// The minimum value.
+ /// The maximum value.
+ /// Returns if is greater than it,
+ /// if is less than it,
+ /// or itself otherwise.
+ public static ulong Clamp(this ulong value, ulong min, ulong max) =>
+ Math.Min(Math.Max(value, min), max);
+
///
/// Converts the to a treating it as a Unix timestamp.
///
diff --git a/X10D/SingleExtensions.cs b/X10D/SingleExtensions.cs
index a402b1b..ec37e84 100644
--- a/X10D/SingleExtensions.cs
+++ b/X10D/SingleExtensions.cs
@@ -11,6 +11,18 @@
///
public static class SingleExtensions
{
+ ///
+ /// Clamps a value between a minimum and a maximum value.
+ ///
+ /// The value to clamp.
+ /// The minimum value.
+ /// The maximum value.
+ /// Returns if is greater than it,
+ /// if is less than it,
+ /// or itself otherwise.
+ public static float Clamp(this float value, float min, float max) =>
+ Math.Min(Math.Max(value, min), max);
+
///
/// Converts an angle from degrees to radians.
///