// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Processing { using System; /// /// Extension methods for classes that implement . /// internal static class ComparableExtensions { /// /// 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 byte Clamp(this byte value, byte min, byte max) { // Order is important here as someone might set min to higher than max. if (value > max) { return max; } if (value < min) { return min; } 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 uint Clamp(this uint value, uint min, uint max) { if (value > max) { return max; } if (value < min) { return min; } 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 > max) { return max; } if (value < min) { return min; } 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 > max) { return max; } if (value < min) { return min; } 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 > max) { return max; } if (value < min) { return min; } return value; } /// /// Converts an to a first restricting the value between the /// minimum and maximum allowable ranges. /// /// The this method extends. /// The public static byte ToByte(this int value) { return (byte)value.Clamp(0, 255); } /// /// Converts an to a first restricting the value between the /// minimum and maximum allowable ranges. /// /// The this method extends. /// The public static byte ToByte(this float value) { return (byte)value.Clamp(0, 255); } /// /// Converts an to a first restricting the value between the /// minimum and maximum allowable ranges. /// /// The this method extends. /// The public static byte ToByte(this double value) { return (byte)value.Clamp(0, 255); } /// /// Swaps the references to two objects in memory. /// /// The first reference. /// The second reference. /// The type of object. public static void Swap(ref T first, ref T second) { T temp = second; second = first; first = temp; } } }