1// The main function that brings it all together
2@function ms($Value: 0, $Bases: $ms-base, $Ratios: $ms-ratio) {
3
4  // If no multi-base or multi-ratio stuff is going on
5  // then just retrn the basic calculaiton
6  @if length($Bases) == 1 and length($Ratios) == 1 {
7    @return ms-calc($Value, $Bases, $Ratios);
8  }
9
10  // Do calculations directly in Ruby when avalible
11  @if $MS-gem-exists {
12
13    // Remove units from bases
14    $Unit: nth($Bases, 1) * 0 + 1; // Extracts the unit from the base
15    $Unitless-Bases: ();
16    @each $Base in $Bases {
17      $Base: $Base/$Unit;
18      $Unitless-Bases: join($Unitless-Bases, $Base);
19    }
20
21    // Calculate natively in Ruby
22    @return ms-gem-func($Value, $Unitless-Bases, $Ratios) * $Unit;
23  }
24
25  // Generate a list of all possible values
26  $Return: ms-generate-list($Value, $Bases, $Ratios);
27
28  // Sort the generated lists
29  $Return: ms-sort-list($Return);
30
31  // Reverse list if its negitive.
32  @if $Value < 0 {
33    $MS-new-return: ();
34    @each $i in $Return {
35      $MS-new-return: join($i, $MS-new-return);
36    }
37    $Return: $MS-new-return;
38  }
39
40  // Normalize value for counting from 1
41  // Because CSS counts things from 1
42  // So Sass does as well
43  // So I get to write fun stuff like this
44  $Value: abs($Value) + 1;
45
46  // Find the correct value in the list
47  $Return: nth($Return, $Value);
48
49  @return $Return;
50}
51
52// Same function, different name, for good measure.
53@function modular-scale($Value: 0, $Bases: $ms-base, $Ratios: $ms-ratio) {
54  @return ms($Value, $Bases, $Ratios);
55}