https://github.com/arirusso/analog
A Ruby helper for scaling numbers
https://github.com/arirusso/analog
math numbers range ranges ruby ruby-helpers scale scaling-numbers
Last synced: 8 months ago
JSON representation
A Ruby helper for scaling numbers
- Host: GitHub
- URL: https://github.com/arirusso/analog
- Owner: arirusso
- License: other
- Created: 2014-04-05T03:11:20.000Z (about 12 years ago)
- Default Branch: main
- Last Pushed: 2022-08-26T19:18:15.000Z (almost 4 years ago)
- Last Synced: 2025-03-24T12:56:20.523Z (about 1 year ago)
- Topics: math, numbers, range, ranges, ruby, ruby-helpers, scale, scaling-numbers
- Language: Ruby
- Homepage:
- Size: 26.4 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Analog
A Ruby helper for scaling numbers
#### Background
Provides a quick way to scale a number from one range or set to another.
A simple example is:
You'd like to plot a point on a 500px graph using a data that lies in the 0..1 range.
```ruby
Scale.transform(0.5).from(0..1).to(0..500)
=> 250
```
It's commonly useful for preparing data to be consumed by generalized APIs and communication protocols.
Personally, I've used it in graphics, audio and for converting music data between OSC and MIDI.
#### Usage
```ruby
require "scale"
```
This example will scale a number down by 1/10th
```ruby
Scale.transform(22).from(0..150).to(0..15)
=> 2
```
Output a float by using a float in the destination range
```ruby
Scale.transform(22).from(0..150).to(0..15.0)
=> 2.2
```
Use Arrays and Sets
```ruby
Scale.transform(0.40).from(0..1).to([0, 2, 4, 8, 12, 16, 32, 64, 128, 512])
=> 8
Scale.transform(8).from(Set.new([0, 2, 4, 8, 16, 64])).to(0..10)
=> 6
```
See the [examples](https://github.com/arirusso/analog/tree/master/examples) for more
###### Core Extension
There is a Numeric extension that you can optionally include.
```ruby
require "scale/core_ext"
0.40.scaled_from(0..1).to([0, 2, 4, 8, 12, 16, 32, 64, 128, 512])
=> 8
```
See the [core_ext example](https://github.com/arirusso/analog/blob/master/examples/core_ext.rb) for more examples.
#### Installation
gem install analog
or with Bundler
gem "analog"
#### License
Licensed under Apache 2.0, See the file LICENSE
Copyright (c) 2014-2017 [Ari Russo](http://arirusso.com)