@neoncitylights/scale v0.4.0
@neoncitylights/scale
A mathematical library in TypeScript for working with progressions - sequences of numbers that convey a certain pattern.
Install
npm install @neoncitylights/scale
Documentation
Auto-generated API documentation is available.
There are 3 types of a progression in math:
- Arithmetic progression: A sequence of numbers where the consecutive difference between each term is a constant.^arith-wolfram E.g., an arithmetic progression of 5 numbers, with 2 as the common difference, starting at 1:
[1, 3, 5, 7, 9]
- Geometric progression: A sequence of numbers where there is a common ratio between each term.^geo-wolfram That ratio is found by multiplying the previous term by a non-zero number. E.g., a geometric progression of 6 numbers, with 2 as the common ratio, starting at 5:
[5, 10, 20, 40, 80, 160]
- Harmonic progression: A sequence of numbers where each term is the harmonic mean of its previous term and next term.^harmonic-wiki E.g., a harmonic progression of 6 numbers, with 2 as the scaling value, starting at 1:
[1, 1/2, 1/3, 1/4, 1/5, 1/6]
API
Generators
- # generators.newArithmeticProgression(startNumber, length, step): number[] • source, docs
- # generators.newGeometricProgression(startNumber, length, scale): number[] • source, docs
- # generators.newHarmonicProgression(startNumber, length, scale): number[] • source, docs
Predicates
- # predicates.isArithmeticProgression(numbers): ProgressionResult • source, docs
- # predicates.isGeometricProgression(numbers): ProgressionResult • source, docs
- # predicates.isHarmonicProgression(numbers): ProgressionResult • source, docs
Totals (sums and products)
- # total.getProductFromArray(factors, lambda): number • source
- # total.getProductFromBounds(lower, upper, lambda): number • source
- # total.getSumFromArray(summands, lambda): number • source
- # total.getSumFromBounds(lower, upper, lambda): number • source
Constants
There are constants for certain music intervals that can be used coincide with the generator functions.
- # MinorSecond
- # MajorSecond
- # MinorThird
- # MajorThird
- # PerfectThird
- # AugmentedFourth
- # PerfectFifth
- # GoldenRatio
- # MajorSixth
- # MajorSeventh
- # EmptySum
- # EmptyProduct
Examples
Typographic scales
This library can be used to generate a typography scale for web projects.
For example, to generate a scale on a major second where the base font size is 16px
:
newGeometricProgression(16, MajorSecond, 6).map(n => `${n.toFixed(3)}px`);
// ["16.000px", "18.000px", "20.250px", "22.781px", "25.629px", "28.833px"]
These floating points aren't easy to remember, so we have the option to modify this scale by rounding each value:
newGeometricProgression(16, MajorSecond, 6).map(n => `${Math.round(n)}px`);
// ["16px", "18px", "20px", "23px", "26px", "29px"]
License
This library is licensed under the MIT License.
^arith-wolfram: Weisstein, Eric W. "Arithmetic Progression." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/ArithmeticProgression.html ^geo-wolfram: Weisstein, Eric W. "Geometric Sequence." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/GeometricSequence.html ^arithm-wiki: Wikipedia contributors. (2021, October 22). Arithmetic progression. In Wikipedia, The Free Encyclopedia. Retrieved 21:13, October 30, 2021, from https://en.wikipedia.org/w/index.php?title=Arithmetic_progression&oldid=1051249503 ^geo-wiki: Wikipedia contributors. (2021, September 9). Geometric progression. In Wikipedia, The Free Encyclopedia. Retrieved 21:11, October 30, 2021, from https://en.wikipedia.org/w/index.php?title=Geometric_progression&oldid=1043380314 ^harmonic-wiki: Wikipedia contributors. (2021, April 28). Harmonic progression (mathematics). In Wikipedia, The Free Encyclopedia. Retrieved 21:14, October 30, 2021, from https://en.wikipedia.org/w/index.php?title=Harmonicprogression(mathematics)&oldid=1020361383