1 | // Foundation for Sites by ZURB
|
2 | // foundation.zurb.com
|
3 | // Licensed under MIT Open Source
|
4 |
|
5 | ////
|
6 | /// @group functions
|
7 | ////
|
8 |
|
9 | /// Finds the greatest common divisor of two integers.
|
10 | ///
|
11 | /// @param {Number} $a - First number to compare.
|
12 | /// @param {Number} $b - Second number to compare.
|
13 | ///
|
14 | /// @returns {Number} The greatest common divisor.
|
15 | @function gcd($a, $b) {
|
16 | // From: http://rosettacode.org/wiki/Greatest_common_divisor#JavaScript
|
17 | @if ($b != 0) {
|
18 | @return gcd($b, $a % $b);
|
19 | }
|
20 | @else {
|
21 | @return abs($a);
|
22 | }
|
23 | }
|
24 |
|
25 | /// Handles decimal exponents by trying to convert them into a fraction and then use a nth-root-algorithm for parts of the calculation
|
26 | ///
|
27 | /// @param {Number} $base - The base number.
|
28 | /// @param {Number} $exponent - The exponent.
|
29 | ///
|
30 | /// @returns {Number} The product of the exponentiation.
|
31 | @function pow($base, $exponent, $prec: 16) {
|
32 | @if (floor($exponent) != $exponent) {
|
33 | $prec2 : pow(10, $prec);
|
34 | $exponent: round($exponent * $prec2);
|
35 | $denominator: gcd($exponent, $prec2);
|
36 | @return nth-root(pow($base, $exponent / $denominator), $prec2 / $denominator, $prec);
|
37 | }
|
38 |
|
39 | $value: $base;
|
40 | @if $exponent > 1 {
|
41 | @for $i from 2 through $exponent {
|
42 | $value: $value * $base;
|
43 | }
|
44 | }
|
45 | @else if $exponent < 1 {
|
46 | @for $i from 0 through -$exponent {
|
47 | $value: $value / $base;
|
48 | }
|
49 | }
|
50 |
|
51 | @return $value;
|
52 | }
|
53 |
|
54 | @function nth-root($num, $n: 2, $prec: 12) {
|
55 | // From: http://rosettacode.org/wiki/Nth_root#JavaScript
|
56 | $x: 1;
|
57 |
|
58 | @for $i from 0 through $prec {
|
59 | $x: 1 / $n * (($n - 1) * $x + ($num / pow($x, $n - 1)));
|
60 | }
|
61 |
|
62 | @return $x;
|
63 | }
|
64 |
|
65 | /// Calculates the height as a percentage of the width for a given ratio.
|
66 | /// @param {List} $ratio - Ratio to use to calculate the height, formatted as `x by y`.
|
67 | /// @return {Number} A percentage value for the height relative to the width of a responsive container.
|
68 | @function ratio-to-percentage($ratio) {
|
69 | $w: nth($ratio, 1);
|
70 | $h: nth($ratio, 3);
|
71 | @return $h / $w * 100%;
|
72 | }
|