Browse Source

Improve performance and accuracy of Clamp

Former-commit-id: 6b1c9852e2f7031bfffe87663e6001f8fae6dced
Former-commit-id: ac05d23bf0bf292e2388ba87b5f06e2cb7172b2a
Former-commit-id: e319ad71c5ae2e13755b12c7f665b08da3068c01
pull/17/head
James Jackson-South 11 years ago
parent
commit
24e7e8f27e
  1. 99
      src/ImageProcessor/Common/Extensions/ComparableExtensions.cs

99
src/ImageProcessor/Common/Extensions/ComparableExtensions.cs

@ -1,12 +1,7 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ComparableExtensions.cs" company="James South">
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
// <copyright file="ComparableExtensions.cs" company="James South">
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Extension methods for classes that implement <see cref="IComparable{T}" />.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor
{
@ -18,24 +13,94 @@ namespace ImageProcessor
internal static class ComparableExtensions
{
/// <summary>
/// Restricts a value to be within a specified range.
/// Restricts a <see cref="byte"/> to be within a specified range.
/// </summary>
/// <param name="value">The The value to clamp.</param>
/// <param name="min">The minimum value. If value is less than min, min will be returned.</param>
/// <param name="max">The maximum value. If value is greater than max, max will be returned.</param>
/// <typeparam name="T">The <see cref="System.Type"/> to clamp.</typeparam>
/// <returns>
/// The <see cref="IComparable{T}"/> representing the clamped value.
/// The <see cref="byte"/> representing the clamped value.
/// </returns>
public static T Clamp<T>(this T value, T min, T max)
where T : IComparable<T>
public static byte Clamp(this byte value, byte min, byte max)
{
if (value.CompareTo(min) < 0)
if (value < min)
{
return min;
}
if (value.CompareTo(max) > 0)
if (value > max)
{
return max;
}
return value;
}
/// <summary>
/// Restricts a <see cref="int"/> to be within a specified range.
/// </summary>
/// <param name="value">The The value to clamp.</param>
/// <param name="min">The minimum value. If value is less than min, min will be returned.</param>
/// <param name="max">The maximum value. If value is greater than max, max will be returned.</param>
/// <returns>
/// The <see cref="int"/> representing the clamped value.
/// </returns>
public static int Clamp(this int value, int min, int max)
{
if (value < min)
{
return min;
}
if (value > max)
{
return max;
}
return value;
}
/// <summary>
/// Restricts a <see cref="float"/> to be within a specified range.
/// </summary>
/// <param name="value">The The value to clamp.</param>
/// <param name="min">The minimum value. If value is less than min, min will be returned.</param>
/// <param name="max">The maximum value. If value is greater than max, max will be returned.</param>
/// <returns>
/// The <see cref="float"/> representing the clamped value.
/// </returns>
public static float Clamp(this float value, float min, float max)
{
if (value < min)
{
return min;
}
if (value > max)
{
return max;
}
return value;
}
/// <summary>
/// Restricts a <see cref="double"/> to be within a specified range.
/// </summary>
/// <param name="value">The The value to clamp.</param>
/// <param name="min">The minimum value. If value is less than min, min will be returned.</param>
/// <param name="max">The maximum value. If value is greater than max, max will be returned.</param>
/// <returns>
/// The <see cref="double"/> representing the clamped value.
/// </returns>
public static double Clamp(this double value, double min, double max)
{
if (value < min)
{
return min;
}
if (value > max)
{
return max;
}
@ -62,7 +127,7 @@ namespace ImageProcessor
/// <returns>The <see cref="byte"/></returns>
public static byte ToByte(this float value)
{
return (byte)value.Clamp(0, 255);
return (byte)(value.Clamp(0, 255) + 0.5f);
}
/// <summary>
@ -73,7 +138,7 @@ namespace ImageProcessor
/// <returns>The <see cref="byte"/></returns>
public static byte ToByte(this double value)
{
return (byte)value.Clamp(0, 255);
return (byte)(value.Clamp(0, 255) + 0.5d);
}
}
}

Loading…
Cancel
Save