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); } } }