Skip to content

Utils.modulate can use an array to clamp #471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions framer/Utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ 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
# Improve limiting option by adding array support for different settings on low and high range
Utils.modulate = (value, rangeA, rangeB, limit=false) ->

[fromLow, fromHigh] = rangeA
Expand All @@ -488,15 +489,27 @@ Utils.modulate = (value, rangeA, rangeB, limit=false) ->

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

if limit is true
if limit is true or _.isEqual(limit, [true, true])
if toLow < toHigh
return toLow if result < toLow
return toHigh if result > toHigh
else
return toLow if result > toLow
return toHigh if result < toHigh

if _.isEqual(limit, [true, false])
if toLow < toHigh
return toLow if result < toLow
else
return toLow if result > toLow

result
if _.isEqual(limit, [false, true])
if toLow < toHigh
return toHigh if result > toHigh
else
return toHigh if result < toHigh

return result



Expand Down
20 changes: 20 additions & 0 deletions test/tests/UtilsTest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,26 @@ describe "Utils", ->
Utils.modulate(0, [1, 2], [100, 0], true).should.equal 100
Utils.modulate(0, [1, 2], [100, 0], false).should.equal 200

# Array support
Utils.modulate(0.5, [0, 1], [0, 100], [true, true]).should.equal 50
Utils.modulate(1, [0, 1], [0, 100], [true, true]).should.equal 100

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

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

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

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

describe "clamp", ->

it "should have the right results", ->
Expand Down