analog 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/scale.rb +1 -1
- data/lib/scale/scheme.rb +63 -21
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fa67d3ea0f497fdb0f6c4e0d795d539963f4b09
|
4
|
+
data.tar.gz: deed6727fb227a1f7f59efd3b6ac09d3c915ea52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49e2004c03e8cbc8288feff90b472efca61cfc5dc84c9df5cbcdadaed0b5d9ead6ae231056f7919af3d56784a7198d8711294bfdeb95fcbeb3a645d1e15f59dd
|
7
|
+
data.tar.gz: 0ecb3626a87261d3d6c5c693a83420d000df25158a16ef055e89fef2d846d9244519b9afa8e1d80b2944ecb6b0221fbcec408c404d57c7c6ff3510e4e79089cc
|
data/README.md
CHANGED
@@ -4,15 +4,15 @@ A Ruby helper for scaling numbers.
|
|
4
4
|
|
5
5
|
#### Background
|
6
6
|
|
7
|
-
|
7
|
+
Provides a quick way to scale a number from one range or set to another.
|
8
8
|
|
9
|
-
|
9
|
+
This is useful for converting data from raw values to a range used for display or other kinds of output.
|
10
10
|
|
11
|
-
I use
|
11
|
+
I use it all the time in music programming, converting OSC messages to musical notes and things like that and figure it might be useful to others.
|
12
12
|
|
13
13
|
A simple example is:
|
14
14
|
|
15
|
-
|
15
|
+
You want to plot a point on a 500px graph using a data that lies in the 0..1 range.
|
16
16
|
|
17
17
|
```ruby
|
18
18
|
Scale.transform(0.5).using(0..1, 0..500)
|
data/lib/scale.rb
CHANGED
data/lib/scale/scheme.rb
CHANGED
@@ -4,7 +4,29 @@ module Scale
|
|
4
4
|
# @param [Numeric] input
|
5
5
|
# @return [Scale::Scheme]
|
6
6
|
def self.transform(input)
|
7
|
-
Scheme.new(
|
7
|
+
Scheme.new.scale(input)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Build a scaling scheme starting with the given source
|
11
|
+
# @param [::Enumerable] source
|
12
|
+
# @return [Scale::Scheme]
|
13
|
+
def self.from(source)
|
14
|
+
Scheme.new.from(source)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Build a scaling scheme starting with the given destination
|
18
|
+
# @param [::Enumerable] destination
|
19
|
+
# @return [Scale::Scheme]
|
20
|
+
def self.to(destination)
|
21
|
+
Scheme.new.to(destination)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Build a scaling scheme starting with the given source and destination
|
25
|
+
# @param [::Enumerable] source
|
26
|
+
# @param [::Enumerable] destination
|
27
|
+
# @return [Scale::Scheme]
|
28
|
+
def self.using(source, destination)
|
29
|
+
Scheme.new.using(:source => source, :destination => destination)
|
8
30
|
end
|
9
31
|
|
10
32
|
# Describes a particular scaling scenario
|
@@ -13,8 +35,8 @@ module Scale
|
|
13
35
|
attr_reader :destination, :input, :source
|
14
36
|
|
15
37
|
# @param [Hash] options
|
16
|
-
# @option options [
|
17
|
-
# @option options [
|
38
|
+
# @option options [::Enumerable] :destination The destination for this scaling scenario
|
39
|
+
# @option options [::Enumerable] :source The source for this scaling scenario
|
18
40
|
def initialize(options = {})
|
19
41
|
@input = options[:input]
|
20
42
|
@source = Source.new(options[:source]) unless options[:source].nil?
|
@@ -25,39 +47,59 @@ module Scale
|
|
25
47
|
# has all of its needed properties, the scaled value will be returned. Otherwise
|
26
48
|
# this method will return the updated Scheme object.
|
27
49
|
#
|
28
|
-
# @param [
|
29
|
-
# @return [
|
50
|
+
# @param [::Enumerable] source
|
51
|
+
# @return [Numeric, Scale::Scheme]
|
30
52
|
def from(source)
|
31
53
|
@source = Source.new(source)
|
32
|
-
|
33
|
-
self
|
34
|
-
else
|
35
|
-
@destination.scale(@input, @source)
|
36
|
-
end
|
54
|
+
scale? ? result : self
|
37
55
|
end
|
38
56
|
|
39
57
|
# Set the destination for this scaling scenario. If on calling this method, the
|
40
58
|
# scenario has all of its needed properties, the scaled value will be returned.
|
41
59
|
# Otherwise this method will return the updated Scheme object.
|
42
60
|
#
|
43
|
-
# @param [
|
44
|
-
# @return [
|
61
|
+
# @param [::Enumerable] destination
|
62
|
+
# @return [Numeric, Scale::Scheme]
|
45
63
|
def to(destination)
|
46
64
|
@destination = Destination.new(destination)
|
47
|
-
|
48
|
-
self
|
49
|
-
else
|
50
|
-
@destination.scale(@input, @source)
|
51
|
-
end
|
65
|
+
scale? ? result : self
|
52
66
|
end
|
53
67
|
|
54
|
-
# Set both the source and destination on this scheme
|
55
|
-
#
|
56
|
-
#
|
68
|
+
# Set both the source and destination on this scheme. If on calling this method, the
|
69
|
+
# scenario has all of its needed properties, the scaled value will be returned.
|
70
|
+
# Otherwise this method will return the updated Scheme object.
|
71
|
+
#
|
72
|
+
# @param [::Enumerable] source
|
73
|
+
# @param [::Enumerable] destination
|
74
|
+
# @return [Numeric, Scale::Scheme]
|
57
75
|
def using(source, destination)
|
58
76
|
@source = Source.new(source)
|
59
77
|
@destination = Destination.new(destination)
|
60
|
-
|
78
|
+
scale? ? result : self
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
# Set the input of this scaling scenario. If on calling this method, the
|
83
|
+
# scenario has all of its needed properties, the scaled value will be returned.
|
84
|
+
# Otherwise this method will return the updated Scheme object.
|
85
|
+
#
|
86
|
+
# @param [Numeric] number
|
87
|
+
# @return [Numeric, Scale::Scheme]
|
88
|
+
def scale(number)
|
89
|
+
@input = number
|
90
|
+
scale? ? result : self
|
91
|
+
end
|
92
|
+
|
93
|
+
# Scan this scaling scenario be run?
|
94
|
+
# @return [Boolean] Whether this scaling scenario can be run
|
95
|
+
def scale?
|
96
|
+
!@input.nil? && !@source.nil? && !@destination.nil?
|
97
|
+
end
|
98
|
+
|
99
|
+
# Get the result of this scaling scenario
|
100
|
+
# @return [Numeric, nil]
|
101
|
+
def result
|
102
|
+
@result ||= @destination.scale(@input, @source)
|
61
103
|
end
|
62
104
|
|
63
105
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: analog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Russo
|
@@ -52,8 +52,7 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: Provides a quick way to
|
56
|
-
destination Array, Range, Set, etc
|
55
|
+
description: Provides a quick way to scale a number from one range or set to another
|
57
56
|
email:
|
58
57
|
- ari.russo@gmail.com
|
59
58
|
executables: []
|