Convert values within a range to values within another range


/ Published in: ActionScript 3
Save to your folder(s)

I’m sharing with you a little utility that I find myself using a lot lately. This utility is a value convertor that you can use in those times when you need to make a volume slider or something similar where you’ll need to change a value and a value range.


Copy this code and paste it in your HTML
  1. package com.vamapaull.utils
  2. {
  3. public class ValueConvertor
  4. {
  5. public static function convertRange(originalStart:Number,
  6. originalEnd:Number,
  7. newStart:Number,
  8. newEnd:Number,
  9. value:Number):Number
  10. {
  11. var originalRange:Number = originalEnd - originalStart;
  12. var newRange:Number = newEnd - newStart;
  13. var ratio:Number = newRange / originalRange;
  14. var newValue:Number = value * ratio;
  15. var finalValue:Number = newValue + newStart;
  16. return finalValue;
  17. }
  18. }
  19. }

URL: http://blog.vamapaull.com/convert-values-within-a-range-to-values-within-another-range/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.