1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:58:49 +00:00

Add GammaToLinear and LinearToGamma (#60)

This commit is contained in:
Oliver Booth 2023-02-26 13:10:59 +00:00
parent f0bce2983a
commit 795d696eca
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
3 changed files with 177 additions and 0 deletions

View File

@ -13,6 +13,8 @@
- X10D: Added `Color.GetClosestConsoleColor()`
- X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()`
- X10D: Added `DirectoryInfo.Clear()`
- X10D: Added `double.LinearToGamma([gamma])` and `float.LinearToGamma([gamma])` (#60)
- X10D: Added `double.GammaToLinear([gamma])` and `float.GammaToLinear([gamma])` (#60)
- X10D: Added `GreatestCommonFactor` for built-in integer types
- X10D: Added `IEnumerable<T>.CountWhereNot(Func<T, bool>)`
- X10D: Added `IEnumerable<T>.FirstWhereNot(Func<T, bool>)`

View File

@ -7,6 +7,26 @@ namespace X10D.Tests.Math;
[TestClass]
public class MathUtilityTests
{
[TestMethod]
public void GammaToLinear_ShouldReturnQuarter_GivenQuarterAndGamma1()
{
double doubleResult = MathUtility.GammaToLinear(0.25, 1.0);
float floatResult = MathUtility.GammaToLinear(0.25f, 1.0f);
Assert.AreEqual(0.25, doubleResult);
Assert.AreEqual(0.25f, floatResult);
}
[TestMethod]
public void GammaToLinear_ShouldReturn1_Given1AndDefaultGamma()
{
double doubleResult = MathUtility.GammaToLinear(1.0);
float floatResult = MathUtility.GammaToLinear(1.0f);
Assert.AreEqual(1.0, doubleResult);
Assert.AreEqual(1.0f, floatResult);
}
[TestMethod]
public void InverseLerp_ShouldReturn0_5_Given0_5_0_1()
{
@ -43,4 +63,24 @@ public class MathUtilityTests
Assert.AreEqual(0.0, doubleResult, 1e-6);
Assert.AreEqual(0.0f, floatResult, 1e-6f);
}
[TestMethod]
public void LinearToGamma_ShouldReturnQuarter_GivenQuarterAndGamma1()
{
double doubleResult = MathUtility.LinearToGamma(0.25, 1.0);
float floatResult = MathUtility.LinearToGamma(0.25f, 1.0f);
Assert.AreEqual(0.25, doubleResult);
Assert.AreEqual(0.25f, floatResult);
}
[TestMethod]
public void LinearToGamma_ShouldReturn1_Given1AndDefaultGamma()
{
double doubleResult = MathUtility.LinearToGamma(1.0);
float floatResult = MathUtility.LinearToGamma(1.0f);
Assert.AreEqual(1.0, doubleResult);
Assert.AreEqual(1.0f, floatResult);
}
}

View File

@ -8,6 +8,75 @@ namespace X10D.Math;
/// </summary>
public static class MathUtility
{
private const double DefaultGamma = 2.2;
private const float DefaultGammaF = 2.2f;
/// <summary>
/// Converts a gamma-encoded value to a linear value using a gamma value of <c>2.2</c>.
/// </summary>
/// <param name="value">The gamma-encoded value to convert. Expected range is [0, 1].</param>
/// <returns>The linear value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float GammaToLinear(float value)
{
return GammaToLinear(value, DefaultGammaF);
}
/// <summary>
/// Converts a gamma-encoded value to a linear value using the specified gamma value.
/// </summary>
/// <param name="value">The gamma-encoded value to convert. Expected range is [0, 1].</param>
/// <param name="gamma">The gamma value to use for decoding.</param>
/// <returns>The linear value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float GammaToLinear(float value, float gamma)
{
return MathF.Pow(value, 1.0f / gamma);
}
/// <summary>
/// Converts a gamma-encoded value to a linear value using a gamma value of <c>2.2</c>.
/// </summary>
/// <param name="value">The gamma-encoded value to convert. Expected range is [0, 1].</param>
/// <returns>The linear value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double GammaToLinear(double value)
{
return GammaToLinear(value, DefaultGamma);
}
/// <summary>
/// Converts a gamma-encoded value to a linear value using the specified gamma value.
/// </summary>
/// <param name="value">The gamma-encoded value to convert. Expected range is [0, 1].</param>
/// <param name="gamma">The gamma value to use for decoding.</param>
/// <returns>The linear value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double GammaToLinear(double value, double gamma)
{
return System.Math.Pow(value, 1.0 / gamma);
}
/// <summary>
/// Returns the linear interpolation inverse of a value, such that it determines where a value lies between two other
/// values.
@ -100,6 +169,72 @@ public static class MathUtility
return ((1.0 - alpha) * value) + (alpha * target);
}
/// <summary>
/// Converts a linear value to a gamma-encoded value using a gamma value of <c>2.2</c>.
/// </summary>
/// <param name="value">The linear value to convert. Expected range is [0, 1].</param>
/// <returns>The gamma-encoded value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float LinearToGamma(float value)
{
return LinearToGamma(value, DefaultGammaF);
}
/// <summary>
/// Converts a linear value to a gamma-encoded value using the specified gamma value.
/// </summary>
/// <param name="value">The linear value to convert. Expected range is [0, 1].</param>
/// <param name="gamma">The gamma value to use for encoding.</param>
/// <returns>The gamma-encoded value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float LinearToGamma(float value, float gamma)
{
return MathF.Pow(value, 1.0f / gamma);
}
/// <summary>
/// Converts a linear value to a gamma-encoded value using a gamma value of <c>2.2</c>.
/// </summary>
/// <param name="value">The linear value to convert. Expected range is [0, 1].</param>
/// <returns>The gamma-encoded value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double LinearToGamma(double value)
{
return LinearToGamma(value, DefaultGamma);
}
/// <summary>
/// Converts a linear value to a gamma-encoded value using the specified gamma value.
/// </summary>
/// <param name="value">The linear value to convert. Expected range is [0, 1].</param>
/// <param name="gamma">The gamma value to use for encoding.</param>
/// <returns>The gamma-encoded value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double LinearToGamma(double value, double gamma)
{
return System.Math.Pow(value, 1.0 / gamma);
}
/// <summary>
/// Converts a value from being a percentage of one range, to being the same percentage in a new range.
/// </summary>