Skip to content

Instantly share code, notes, and snippets.

@anechunaev
Created January 31, 2019 13:59
Show Gist options
  • Save anechunaev/4c8a8fdd66f615e4d7f0c23f6ee53851 to your computer and use it in GitHub Desktop.
Save anechunaev/4c8a8fdd66f615e4d7f0c23f6ee53851 to your computer and use it in GitHub Desktop.
function linearToLogarithmic(linearValue: number, minValue: number, maxValue: number) {
const range = maxValue - minValue;
let value = Math.round(Math.pow(range + 1, linearValue) + minValue - 1);
if (value < minValue) {
value = minValue;
} else if (value > maxValue) {
value = maxValue;
}
return value;
};
function logarithmicToLinear(scale: number, minValue: number, maxValue: number) {
const range = maxValue - minValue;
const normalizedValue = scale - minValue + 1;
if (normalizedValue <= 0) {
return 0;
} else if (scale >= maxValue) {
return 1;
} else {
return Math.log(normalizedValue) / Math.log(range + 1);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment