diff options
Diffstat (limited to 'src/_math.scss')
| -rw-r--r-- | src/_math.scss | 62 | 
1 files changed, 62 insertions, 0 deletions
| diff --git a/src/_math.scss b/src/_math.scss new file mode 100644 index 0000000..9b71bf6 --- /dev/null +++ b/src/_math.scss | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | //// | ||
| 2 | /// Basic mathematical functions. | ||
| 3 | /// | ||
| 4 | /// @group Math functions | ||
| 5 | /// | ||
| 6 | /// @access public | ||
| 7 | //// | ||
| 8 | |||
| 9 | /// | ||
| 10 | /// Perform exponentiation. Only integer exponents are supported. | ||
| 11 | /// | ||
| 12 | /// @param {number} $base | ||
| 13 | /// @param {number} $exp | ||
| 14 | /// | ||
| 15 | /// @return {number} | ||
| 16 | /// | ||
| 17 | /// @example scss - Exponentiation with a positive exponent | ||
| 18 | /// $result: iro-math-pow(3, 2); // The value of $result is 3^2 = 9 | ||
| 19 | /// | ||
| 20 | /// @example scss - Exponentiation with a negative exponent | ||
| 21 | /// $result: iro-math-pow(2, -3); // The value of $result is 1/(2^3) = 1/8 | ||
| 22 | /// | ||
| 23 | @function iro-math-pow($base, $exp) { | ||
| 24 | $value: 1; | ||
| 25 | |||
| 26 | @if $exp > 0 { | ||
| 27 | @for $i from 1 through $exp { | ||
| 28 | $value: $value * $base; | ||
| 29 | } | ||
| 30 | } @else if $exp < 0 { | ||
| 31 | @for $i from 1 through -$exp { | ||
| 32 | $value: $value / $base; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | @return $value; | ||
| 37 | } | ||
| 38 | |||
| 39 | /// | ||
| 40 | /// Clamp a number between a minimum and maximum value. | ||
| 41 | /// | ||
| 42 | /// @param {number} $value - Value to clamp | ||
| 43 | /// @param {number} $min - Minimum value | ||
| 44 | /// @param {number} $max - Maximum value | ||
| 45 | /// | ||
| 46 | /// @return {number} | ||
| 47 | /// | ||
| 48 | /// @example scss | ||
| 49 | /// $result: iro-math-clamp(20, 0, 10); // The value of $result is 10 | ||
| 50 | /// | ||
| 51 | /// @example scss | ||
| 52 | /// $result: iro-math-clamp(50, 20, 100); // The value of $result is 50 | ||
| 53 | /// | ||
| 54 | @function iro-math-clamp($value, $min, $max) { | ||
| 55 | @if $value < $min { | ||
| 56 | @return $min; | ||
| 57 | } | ||
| 58 | @if $value > $max { | ||
| 59 | @return $max; | ||
| 60 | } | ||
| 61 | @return $value; | ||
| 62 | } | ||
