mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-10 02:05:08 +00:00
40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Crestron.SimplSharp;
|
|
|
|
namespace PepperDash.Essentials.Core
|
|
{
|
|
public class NumericalHelpers
|
|
{
|
|
/// <summary>
|
|
/// Scales a value
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <param name="inMin"></param>
|
|
/// <param name="inMax"></param>
|
|
/// <param name="outMin"></param>
|
|
/// <param name="outMax"></param>
|
|
/// <returns></returns>
|
|
public static double Scale(double input, double inMin, double inMax, double outMin, double outMax)
|
|
{
|
|
//Debug.Console(2, this, "Scaling (double) input '{0}' with min '{1}'/max '{2}' to output range min '{3}'/max '{4}'", input, inMin, inMax, outMin, outMax);
|
|
|
|
double inputRange = inMax - inMin;
|
|
|
|
if (inputRange <= 0)
|
|
{
|
|
throw new ArithmeticException(string.Format("Invalid Input Range '{0}' for Scaling. Min '{1}' Max '{2}'.", inputRange, inMin, inMax));
|
|
}
|
|
|
|
double outputRange = outMax - outMin;
|
|
|
|
var output = (((input - inMin) * outputRange) / inputRange) + outMin;
|
|
|
|
// Debug.Console(2, this, "Scaled output '{0}'", output);
|
|
|
|
return output;
|
|
}
|
|
}
|
|
} |