Code:
function fscale(originalMin, originalMax, newBegin, newEnd, inputValue, curve) {
let OriginalRange = 0;
let NewRange = 0;
let zeroRefCurVal = 0;
let normalizedCurVal = 0;
let rangedValue = 0;
let invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1); // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = Math.pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
// Zero Refference the values
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin) {
NewRange = newEnd - newBegin;
} else {
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax) {
return 0;
}
if (invFlag == 0) {
rangedValue = (Math.pow(normalizedCurVal, curve) * NewRange) + newBegin;
} else // invert the ranges
{
rangedValue = newBegin - (Math.pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
}