1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:38:47 +00:00

Add Clamp() for integral types

This commit is contained in:
Oliver Booth 2019-11-16 01:50:12 +00:00
parent 44b6773cc8
commit cae360edc3
No known key found for this signature in database
GPG Key ID: 4B0992B2602C3778
5 changed files with 96 additions and 0 deletions

View File

@ -11,6 +11,18 @@
/// </summary> /// </summary>
public static class DoubleExtensions public static class DoubleExtensions
{ {
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
public static double Clamp(this double value, double min, double max) =>
Math.Min(Math.Max(value, min), max);
/// <summary> /// <summary>
/// Converts an angle from degrees to radians. /// Converts an angle from degrees to radians.
/// </summary> /// </summary>

View File

@ -52,6 +52,30 @@
#endregion #endregion
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
public static short Clamp(this short value, short min, short max) =>
Math.Min(Math.Max(value, min), max);
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
public static ushort Clamp(this ushort value, ushort min, ushort max) =>
Math.Min(Math.Max(value, min), max);
/// <summary> /// <summary>
/// Converts the <see cref="Int16"/> to a <see cref="DateTime"/> treating it as a Unix timestamp. /// Converts the <see cref="Int16"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
/// </summary> /// </summary>

View File

@ -52,6 +52,30 @@
#endregion #endregion
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
public static int Clamp(this int value, int min, int max) =>
Math.Min(Math.Max(value, min), max);
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
public static uint Clamp(this uint value, uint min, uint max) =>
Math.Min(Math.Max(value, min), max);
/// <summary> /// <summary>
/// Converts the <see cref="Int32"/> to a <see cref="DateTime"/> treating it as a Unix timestamp. /// Converts the <see cref="Int32"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
/// </summary> /// </summary>

View File

@ -49,6 +49,30 @@
#endregion #endregion
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
public static long Clamp(this long value, long min, long max) =>
Math.Min(Math.Max(value, min), max);
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
public static ulong Clamp(this ulong value, ulong min, ulong max) =>
Math.Min(Math.Max(value, min), max);
/// <summary> /// <summary>
/// Converts the <see cref="Int64"/> to a <see cref="DateTime"/> treating it as a Unix timestamp. /// Converts the <see cref="Int64"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
/// </summary> /// </summary>

View File

@ -11,6 +11,18 @@
/// </summary> /// </summary>
public static class SingleExtensions public static class SingleExtensions
{ {
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
public static float Clamp(this float value, float min, float max) =>
Math.Min(Math.Max(value, min), max);
/// <summary> /// <summary>
/// Converts an angle from degrees to radians. /// Converts an angle from degrees to radians.
/// </summary> /// </summary>