namespace BlurHash;
///
/// Utility methods for mathematical calculations
///
public static class MathUtils
{
///
/// Calculates Math.Pow(base, exponent) but retains the sign of base in the result.
///
/// The base of the power. The sign of this value will be the sign of the result
/// The exponent of the power
public static double SignPow(double @base, double exponent) => Math.Sign(@base) * Math.Pow(Math.Abs(@base), exponent);
///
/// Converts an sRGB input value (0 to 255) into a linear double value
///
public static double SRgbToLinear(int value)
{
double v = value / 255.0;
if (v <= 0.04045)
return v / 12.92;
else
return Math.Pow((v + 0.055) / 1.055, 2.4);
}
///
/// Converts a linear double value into an sRGB input value (0 to 255)
///
public static int LinearTosRgb(double value)
{
double v = Math.Max(0.0, Math.Min(1.0, value));
if (v <= 0.0031308)
return (int)(v * 12.92 * 255 + 0.5);
else
return (int)((1.055 * Math.Pow(v, 1 / 2.4) - 0.055) * 255 + 0.5);
}
}