From 0931d6f1ece5b54cf12fffeb1d08d3b2b24e7c62 Mon Sep 17 00:00:00 2001 From: Jack Kanarish Date: Wed, 10 Apr 2019 17:17:30 -0400 Subject: [PATCH] feat: add helper method for un-mapping a range from ushorts --- ICD.Common.Utils/Attributes/RangeAttribute.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/ICD.Common.Utils/Attributes/RangeAttribute.cs b/ICD.Common.Utils/Attributes/RangeAttribute.cs index 5c733f8..1574f9e 100644 --- a/ICD.Common.Utils/Attributes/RangeAttribute.cs +++ b/ICD.Common.Utils/Attributes/RangeAttribute.cs @@ -206,6 +206,8 @@ namespace ICD.Common.Utils.Attributes throw new ArgumentException("the type of value is not a numeric type."); } + #region Range -> UShort + public ushort RemapRangeToUshort(double value) { return (ushort)MathUtils.MapRange(GetMin(), GetMax(), ushort.MinValue, ushort.MaxValue, value); @@ -227,5 +229,75 @@ namespace ICD.Common.Utils.Attributes } #endregion + + #region UShort -> Range + + public object RemapUshortToRange(ushort value) + { + if (Min is ushort) + { + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), value); + } + + if (Min is short) + { + var castVal = (short)value; + return (short)MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is uint) + { + var castVal = (uint)value; + return (uint)MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is int) + { + var castVal = (int)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is ulong) + { + var castVal = (ulong)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is long) + { + var castVal = (long)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is float) + { + var castVal = (float)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is double) + { + var castVal = (double)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is decimal) + { + var castVal = (decimal)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is byte) + { + var castVal = (byte)value; + return (byte)MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + throw new NotSupportedException("Value type of range attribute is not supported."); + } + + #endregion + + #endregion } } \ No newline at end of file