Class NumericExtensions
- Namespace
- Plugin.BaseTypeExtensions
- Assembly
- Plugin.BaseTypeExtensions.dll
Provides extension methods for generic numeric operations, including angle conversions, clamping, and percentage calculations.
public static class NumericExtensions
- Inheritance
-
NumericExtensions
- Inherited Members
Methods
Clamp<T>(T, T, T)
Clamps a numeric value between a minimum and maximum value for any numeric type.
public static T Clamp<T>(this T value, T min, T max) where T : INumber<T>
Parameters
value
TThe value to clamp
min
TThe minimum allowed value
max
TThe maximum allowed value
Returns
- T
The clamped value
Type Parameters
T
Any numeric type that implements INumber<T>
DegreeToRadian<T>(T)
Converts an angle from degrees to radians for any numeric type.
public static T DegreeToRadian<T>(this T angleInDegrees) where T : INumber<T>
Parameters
angleInDegrees
TThe angle in degrees
Returns
- T
The angle in radians
Type Parameters
T
Any numeric type that implements INumber<T>
GetInvertedSafe<T>(T, bool, T)
Returns the value as-is or inverted based on the isInverted flag for any numeric type. When inverted, returns maxValue - value. When not inverted, returns the original value.
public static T GetInvertedSafe<T>(this T value, bool isInverted, T maxValue) where T : INumber<T>
Parameters
value
TThe value to potentially invert
isInverted
boolWhether to apply inversion
maxValue
TThe maximum value used for inversion calculation
Returns
- T
The original value if not inverted, or maxValue - value if inverted
Type Parameters
T
Any numeric type that implements INumber<T>
PercentageToByte<T>(T)
Converts a percentage (0.0 to 1.0) to a byte value (0-255) for any numeric type.
public static byte PercentageToByte<T>(this T percentage) where T : INumber<T>
Parameters
percentage
TThe percentage value (0.0 to 1.0)
Returns
- byte
A byte value in the range 0-255
Type Parameters
T
Any numeric type that implements INumber<T>
PercentageToValue<T>(T, T, T)
Converts a percentage (0.0 to 1.0) to a value within the specified range for any numeric type.
public static T PercentageToValue<T>(this T percentage, T minBound, T maxBound) where T : INumber<T>
Parameters
percentage
TThe percentage value (0.0 to 1.0)
minBound
TThe minimum bound of the target range
maxBound
TThe maximum bound of the target range
Returns
- T
The calculated value within the specified range
Type Parameters
T
Any numeric type that implements INumber<T>
Exceptions
- ArgumentOutOfRangeException
Thrown when minBound is greater than maxBound
RadianToDegree<T>(T)
Converts an angle from radians to degrees for any numeric type.
public static T RadianToDegree<T>(this T angleInRadians) where T : INumber<T>
Parameters
angleInRadians
TThe angle in radians
Returns
- T
The angle in degrees
Type Parameters
T
Any numeric type that implements INumber<T>
ToByte<T>(T)
Converts a numeric value to a byte, clamping to valid byte range (0-255) for any numeric type.
public static byte ToByte<T>(this T input) where T : INumber<T>
Parameters
input
TThe numeric value to convert
Returns
- byte
A byte value clamped to the range 0-255
Type Parameters
T
Any numeric type that implements INumber<T>
ValueToPercentage<T>(T, T, T)
Converts a value within a range to a percentage (0.0 to 1.0) for any numeric type.
public static T ValueToPercentage<T>(this T value, T minBound, T maxBound) where T : INumber<T>
Parameters
value
TThe value to convert
minBound
TThe minimum bound of the source range
maxBound
TThe maximum bound of the source range
Returns
- T
The percentage representation (0.0 to 1.0)
Type Parameters
T
Any numeric type that implements INumber<T>
Exceptions
- ArgumentOutOfRangeException
Thrown when minBound is greater than maxBound