Skip to content

Instantly share code, notes, and snippets.

@rijkvanzanten
Created March 25, 2017 16:20
Show Gist options
  • Save rijkvanzanten/b4c3cb33b3bbb21e9755c54ec0269bf7 to your computer and use it in GitHub Desktop.
Save rijkvanzanten/b4c3cb33b3bbb21e9755c54ec0269bf7 to your computer and use it in GitHub Desktop.
Convert number from one range to another
/**
* Convert value from one range to another
* @param {Number} value value to convert
* @param {Object} oldRange min, max of values range
* @param {Object} newRange min, max of desired range
* @return {Number} value converted to other range
*/
function convertRange(value, oldRange, newRange) {
return ((value - oldRange.min) * (newRange.max - newRange.min)) / (oldRange.max - oldRange.min) + newRange.min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment