diff --git a/CHANGELOG.md b/CHANGELOG.md
index 68abe64..e510177 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -84,6 +84,9 @@ Removals
- Marked DynamicEnum as obsolete due to its reimplementation in [DynamicEnums](https://www.nuget.org/packages/DynamicEnums)
## MLEM.Extended
+Additions
+- Added Range extension methods GetPercentage and FromPercentage
+
Improvements
- Multi-target net452, making MLEM compatible with MonoGame for consoles
- Added trimming and AOT annotations and made MLEM.Extended trimmable
diff --git a/MLEM.Extended/Extensions/NumberExtensions.cs b/MLEM.Extended/Extensions/NumberExtensions.cs
index a66fa84..3ae1385 100644
--- a/MLEM.Extended/Extensions/NumberExtensions.cs
+++ b/MLEM.Extended/Extensions/NumberExtensions.cs
@@ -40,5 +40,53 @@ namespace MLEM.Extended.Extensions {
return rect.ToMlem().Penetrate(other.ToMlem(), out normal, out penetration);
}
+ ///
+ /// Returns how far between the given 's and value the given is, as a number between 0 and 1.
+ /// Note that, if the is outside the given , a correct proportional value outside the 0 to 1 range will still be returned.
+ /// This method is the reverse action of .
+ ///
+ /// The range to query.
+ /// The value to query.
+ /// The percentage.
+ public static float GetPercentage(this Range range, float value) {
+ return (value - range.Min) / (range.Max - range.Min);
+ }
+
+ ///
+ /// Returns how far between the given 's and value the given is, as a number between 0 and 1.
+ /// Note that, if the is outside the given , a correct proportional value outside the 0 to 1 range will still be returned.
+ /// This method is the reverse action of .
+ ///
+ /// The range to query.
+ /// The value to query.
+ /// The percentage.
+ public static float GetPercentage(this Range range, float value) {
+ return (value - range.Min) / (range.Max - range.Min);
+ }
+
+ ///
+ /// Returns a value within the given 's and values based on the given into the range.
+ /// Note that, if the is outside the 0 to 1 range, a correct value outside the will still be returned.
+ /// This method is the reverse action of .
+ ///
+ /// The range to query.
+ /// The percentage to query.
+ /// The value.
+ public static float FromPercentage(this Range range, float percentage) {
+ return (range.Max - range.Min) * percentage + range.Min;
+ }
+
+ ///
+ /// Returns a value within the given 's and values based on the given into the range.
+ /// Note that, if the is outside the 0 to 1 range, a correct value outside the will still be returned.
+ /// This method is the reverse action of .
+ ///
+ /// The range to query.
+ /// The percentage to query.
+ /// The value.
+ public static float FromPercentage(this Range range, float percentage) {
+ return (range.Max - range.Min) * percentage + range.Min;
+ }
+
}
}
diff --git a/Tests/NumberTests.cs b/Tests/NumberTests.cs
index 160d681..8251e1f 100644
--- a/Tests/NumberTests.cs
+++ b/Tests/NumberTests.cs
@@ -1,9 +1,14 @@
using Microsoft.Xna.Framework;
using MLEM.Extensions;
-using MLEM.Misc;
using NUnit.Framework;
+using RectangleF = MLEM.Misc.RectangleF;
-namespace Tests;
+#if !FNA
+using MonoGame.Extended;
+using MLEM.Extended.Extensions;
+#endif
+
+namespace Tests;
public class NumberTests {
@@ -68,4 +73,28 @@ public class NumberTests {
Assert.AreEqual(penetration, 0);
}
+ #if !FNA
+ [Test]
+ public void TestRangePercentage() {
+ Assert.AreEqual(0.5F, new Range(1, 7).GetPercentage(4));
+ Assert.AreEqual(1, new Range(1, 7).GetPercentage(7));
+ Assert.AreEqual(0, new Range(1, 7).GetPercentage(1));
+ Assert.AreEqual(4, new Range(1, 7).FromPercentage(0.5F));
+ Assert.AreEqual(7, new Range(1, 7).FromPercentage(1));
+ Assert.AreEqual(1, new Range(1, 7).FromPercentage(0));
+
+ Assert.AreEqual(0.5F, new Range(-1, 1).GetPercentage(0));
+ Assert.AreEqual(0.25F, new Range(-1, 1).GetPercentage(-0.5F));
+ Assert.AreEqual(0.75F, new Range(-1, 1).GetPercentage(0.5F));
+ Assert.AreEqual(0, new Range(-1, 1).FromPercentage(0.5F));
+ Assert.AreEqual(-0.5F, new Range(-1, 1).FromPercentage(0.25F));
+ Assert.AreEqual(0.5F, new Range(-1, 1).FromPercentage(0.75F));
+
+ Assert.AreEqual(1.5F, new Range(8, 10).GetPercentage(11));
+ Assert.AreEqual(-0.5F, new Range(8, 10).GetPercentage(7));
+ Assert.AreEqual(11, new Range(8, 10).FromPercentage(1.5F));
+ Assert.AreEqual(7, new Range(8, 10).FromPercentage(-0.5F));
+ }
+ #endif
+
}
diff --git a/Tests/Tests.FNA.csproj b/Tests/Tests.FNA.csproj
index 0f9cdad..c90997d 100644
--- a/Tests/Tests.FNA.csproj
+++ b/Tests/Tests.FNA.csproj
@@ -9,6 +9,7 @@
+
@@ -17,12 +18,13 @@
+
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
-
-
+
diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj
index 4386917..2090052 100644
--- a/Tests/Tests.csproj
+++ b/Tests/Tests.csproj
@@ -5,27 +5,30 @@
TestResults
false
-
+
+
-
+
+
+
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
-
-
+
PreserveNewest