From cae360edc33dd8915851d30a182128aec0b53f42 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 16 Nov 2019 01:50:12 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Clamp()=20for=20integral=20ty?= =?UTF-8?q?pes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- X10D/DoubleExtensions.cs | 12 ++++++++++++ X10D/Int16Extensions.cs | 24 ++++++++++++++++++++++++ X10D/Int32Extensions.cs | 24 ++++++++++++++++++++++++ X10D/Int64Extensions.cs | 24 ++++++++++++++++++++++++ X10D/SingleExtensions.cs | 12 ++++++++++++ 5 files changed, 96 insertions(+) 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. ///