Skip to content
Permalink
Browse files

Add Utils.modulate to map number ranges

  • Loading branch information
koenbok committed May 15, 2014
1 parent a05732a commit 216a271ea33672c2def0190403006eabca17eddb
Showing with 30 additions and 0 deletions.
  1. +16 −0 framer/Utils.coffee
  2. +14 −0 test/tests/UtilsTest.coffee
@@ -207,6 +207,22 @@ Utils.round = (value, decimals) ->
Utils.mapRange = (value, fromLow, fromHigh, toLow, toHigh) ->
toLow + (((value - fromLow) / (fromHigh - fromLow)) * (toHigh - toLow))

# Kind of similar as above but with a better syntax and a limiting option
Utils.modulate = (value, rangeA, rangeB, limit=false) ->

[fromLow, fromHigh] = rangeA
[toLow, toHigh] = rangeB

result = toLow + (((value - fromLow) / (fromHigh - fromLow)) * (toHigh - toLow))

if limit is true
return toLow if result < toLow
return toHigh if result > toHigh

result



######################################################
# STRING FUNCTIONS

@@ -181,6 +181,20 @@ describe "Utils", ->
test = -> Utils.domLoadDataSync("static/nonexisting.txt")
test.should.throw()

describe "modulate", ->

it "should have the right results", ->
Utils.modulate(0.5, [0, 1], [0, 100]).should.equal 50
Utils.modulate(1, [0, 1], [0, 100]).should.equal 100

Utils.modulate(2, [0, 1], [0, 100], true).should.equal 100
Utils.modulate(2, [0, 1], [0, 100], false).should.equal 200

Utils.modulate(0, [1, 2], [0, 100], true).should.equal 0
Utils.modulate(0, [1, 2], [0, 100], false).should.equal -100






0 comments on commit 216a271

Please sign in to comment.
You can’t perform that action at this time.