Last active
April 8, 2016 12:33
-
-
Save IainIsCreative/49cc58dafe6fe4397c1c to your computer and use it in GitHub Desktop.
Sass Power Math Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Power function in Sass. | |
* | |
* For power calculations in Sass. | |
* Place a number as a base, then a number to multiply from. Finally, add an | |
* exponent number. | |
* | |
* Simple example of such math being the speed of light: | |
* power(3, 10, 8); | |
* Output: | |
* 300000000 (300,000,000m a second - the speed of light). | |
* | |
* Could be useful in terms of grids or layout following a simple grid space? | |
* example - 20 pixels multiplied by 2 to the power of 6: | |
* power(20, 2, 6); | |
* Output: | |
* 1280 (for a 1280px wide element). | |
* | |
**/ | |
@function power($base, $multiplier, $exponent) { | |
/** | |
* Set the total variable to return false if it fails. | |
*/ | |
$total: false; | |
/** | |
* Check if all arguments are numbers. | |
*/ | |
@if type-of($base) != number | |
or type-of($multiplier) != number | |
or type-of($exponent) != number { | |
/** | |
* Return an error if they arguments are not numbers. | |
*/ | |
@error 'The `power()` function only accepts numbers as arguments. Please try again.'; | |
} | |
/** | |
* If no errors, proceed. | |
*/ | |
@else { | |
/** | |
* Set number to total as a base. | |
*/ | |
$total: $base; | |
/** | |
* Loop through the number of times it has to power through the | |
* multiplication, by multipling the power total with the multipling | |
* number the specified number of times by the exponent. | |
*/ | |
@for $i from 1 through $exponent { | |
$total: $total * $multiplier; | |
} | |
} | |
/** | |
* Return total. | |
*/ | |
@return $total; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment