diff --git a/ICD.Common.Utils/Attributes/RangeAttribute.cs b/ICD.Common.Utils/Attributes/RangeAttribute.cs index db53ce9..e16b7b5 100644 --- a/ICD.Common.Utils/Attributes/RangeAttribute.cs +++ b/ICD.Common.Utils/Attributes/RangeAttribute.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using ICD.Common.Properties; using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.Attributes @@ -285,12 +286,37 @@ namespace ICD.Common.Utils.Attributes #region Methods + /// + /// Remaps the numeric value into the min-max range of the target numeric type. + /// + /// + /// + /// + [NotNull] + public static object Remap([NotNull] object value, [NotNull] Type type) + { + if (value == null) + throw new ArgumentNullException("value"); + + if (type == null) + throw new ArgumentNullException("type"); + + if (!type.IsNumeric()) + throw new ArgumentException("Target type is not numeric"); + + if (!value.GetType().IsNumeric()) + throw new ArgumentException("Source value is not numeric"); + + double intermediate = RemapToDouble(value); + return RemapFromDouble(intermediate, type); + } + /// /// Remaps the given numeric value from its min/max range into double min/max range. /// /// /// - public static double RemapToDouble(object value) + public static double RemapToDouble([NotNull] object value) { if (value == null) throw new ArgumentNullException("value"); @@ -308,7 +334,8 @@ namespace ICD.Common.Utils.Attributes /// /// /// - public static object RemapFromDouble(double value, Type type) + [NotNull] + public static object RemapFromDouble(double value, [NotNull] Type type) { if (type == null) throw new ArgumentNullException("type"); @@ -320,13 +347,41 @@ namespace ICD.Common.Utils.Attributes return remap(value); } + /// + /// Remaps the given numeric value to the defined min/max. + /// + /// + /// + [NotNull] + public object RemapMinMax([NotNull] object value) + { + if (value == null) + throw new ArgumentNullException("value"); + + if (!value.GetType().IsNumeric()) + throw new ArgumentException("Source value is not numeric"); + + double sourceMin = GetMinAsDouble(value.GetType()); + double sourceMax = GetMaxAsDouble(value.GetType()); + + double targetMin = Convert.ToDouble(Min); + double targetMax = Convert.ToDouble(Max); + + double doubleValue = Convert.ToDouble(value); + + double remapped = MathUtils.MapRange(sourceMin, sourceMax, targetMin, targetMax, doubleValue); + + return Convert.ChangeType(remapped, value.GetType(), CultureInfo.InvariantCulture); + } + /// /// Clamps the given numeric value into the valid ranges of the target numeric type. /// /// /// /// - public static object Clamp(object value, Type type) + [NotNull] + public static object Clamp([NotNull] object value, [NotNull] Type type) { if (value == null) throw new ArgumentNullException("value"); @@ -352,7 +407,7 @@ namespace ICD.Common.Utils.Attributes /// /// /// - public static double Clamp(double value, Type type) + public static double Clamp(double value, [NotNull] Type type) { if (type == null) throw new ArgumentNullException("type"); @@ -364,37 +419,14 @@ namespace ICD.Common.Utils.Attributes return clamp(value); } - /// - /// Remaps the numeric value into the min-max range of the target numeric type. - /// - /// - /// - /// - public static object Remap(object value, Type type) - { - if (value == null) - throw new ArgumentNullException("value"); - - if (type == null) - throw new ArgumentNullException("type"); - - if (!type.IsNumeric()) - throw new ArgumentException("Target type is not numeric"); - - if (!value.GetType().IsNumeric()) - throw new ArgumentException("Source value is not numeric"); - - double intermediate = RemapToDouble(value); - return RemapFromDouble(intermediate, type); - } - /// /// Clamps the given numeric value to the defined min/max then remaps to the target numeric type. /// /// /// /// - public object ClampMinMaxThenRemap(object value, Type type) + [NotNull] + public object ClampMinMaxThenRemap([NotNull] object value, [NotNull] Type type) { if (value == null) throw new ArgumentNullException("value"); @@ -418,33 +450,12 @@ namespace ICD.Common.Utils.Attributes return Convert.ChangeType(remapped, value.GetType(), CultureInfo.InvariantCulture); } - /// - /// Remaps the given numeric value to the defined min/max. - /// - /// - /// - public object RemapMinMax(object value) - { - if (value == null) - throw new ArgumentNullException("value"); + #endregion - if (!value.GetType().IsNumeric()) - throw new ArgumentException("Source value is not numeric"); + #region Private Methods - double sourceMin = GetMinAsDouble(value.GetType()); - double sourceMax = GetMaxAsDouble(value.GetType()); - - double targetMin = Convert.ToDouble(Min); - double targetMax = Convert.ToDouble(Max); - - double doubleValue = Convert.ToDouble(value); - - double remapped = MathUtils.MapRange(sourceMin, sourceMax, targetMin, targetMax, doubleValue); - - return Convert.ChangeType(remapped, value.GetType(), CultureInfo.InvariantCulture); - } - - private object RemapMinMax(object value, Type type) + [NotNull] + private object RemapMinMax([NotNull] object value, [NotNull] Type type) { if (value == null) throw new ArgumentNullException("value"); @@ -471,16 +482,12 @@ namespace ICD.Common.Utils.Attributes return Convert.ChangeType(remapped, type, CultureInfo.InvariantCulture); } - #endregion - - #region Private Methods - /// /// Gets the min value for the given numeric type as a double. /// /// /// - private static double GetMinAsDouble(Type type) + private static double GetMinAsDouble([NotNull] Type type) { if (type == null) throw new ArgumentNullException("type"); @@ -500,7 +507,7 @@ namespace ICD.Common.Utils.Attributes /// /// /// - private static double GetMaxAsDouble(Type type) + private static double GetMaxAsDouble([NotNull] Type type) { if (type == null) throw new ArgumentNullException("type");