From 24e7e8f27e95a401048ee0b409ce9390301ee9a5 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 8 Oct 2015 00:00:40 +1100 Subject: [PATCH] Improve performance and accuracy of Clamp Former-commit-id: 6b1c9852e2f7031bfffe87663e6001f8fae6dced Former-commit-id: ac05d23bf0bf292e2388ba87b5f06e2cb7172b2a Former-commit-id: e319ad71c5ae2e13755b12c7f665b08da3068c01 --- .../Common/Extensions/ComparableExtensions.cs | 99 +++++++++++++++---- 1 file changed, 82 insertions(+), 17 deletions(-) diff --git a/src/ImageProcessor/Common/Extensions/ComparableExtensions.cs b/src/ImageProcessor/Common/Extensions/ComparableExtensions.cs index d223aec673..8b73b327f6 100644 --- a/src/ImageProcessor/Common/Extensions/ComparableExtensions.cs +++ b/src/ImageProcessor/Common/Extensions/ComparableExtensions.cs @@ -1,12 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright © James South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright © James South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Extension methods for classes that implement . -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor { @@ -18,24 +13,94 @@ namespace ImageProcessor internal static class ComparableExtensions { /// - /// Restricts a value to be within a specified range. + /// Restricts a to be within a specified range. /// /// The The value to clamp. /// The minimum value. If value is less than min, min will be returned. /// The maximum value. If value is greater than max, max will be returned. - /// The to clamp. /// - /// The representing the clamped value. + /// The representing the clamped value. /// - public static T Clamp(this T value, T min, T max) - where T : IComparable + 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; + } + + /// + /// Restricts a to be within a specified range. + /// + /// The The value to clamp. + /// The minimum value. If value is less than min, min will be returned. + /// The maximum value. If value is greater than max, max will be returned. + /// + /// The representing the clamped value. + /// + public static int Clamp(this int value, int min, int max) + { + if (value < min) + { + return min; + } + + if (value > max) + { + return max; + } + + return value; + } + + /// + /// Restricts a to be within a specified range. + /// + /// The The value to clamp. + /// The minimum value. If value is less than min, min will be returned. + /// The maximum value. If value is greater than max, max will be returned. + /// + /// The representing the clamped value. + /// + public static float Clamp(this float value, float min, float max) + { + if (value < min) + { + return min; + } + + if (value > max) + { + return max; + } + + return value; + } + + /// + /// Restricts a to be within a specified range. + /// + /// The The value to clamp. + /// The minimum value. If value is less than min, min will be returned. + /// The maximum value. If value is greater than max, max will be returned. + /// + /// The representing the clamped value. + /// + 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 /// The public static byte ToByte(this float value) { - return (byte)value.Clamp(0, 255); + return (byte)(value.Clamp(0, 255) + 0.5f); } /// @@ -73,7 +138,7 @@ namespace ImageProcessor /// The public static byte ToByte(this double value) { - return (byte)value.Clamp(0, 255); + return (byte)(value.Clamp(0, 255) + 0.5d); } } }