/// Returns a color based on an equally aligned gradient of colors
///
/// @example
///     // this is the same as: mix(green, red, 70%)
///     background-color: color-mix(0.7, red, green);
///
///     // pick a random rainbow color
///     background-color: color-mix(random(), red, yellow, lime, cyan, blue, magenta, red);
///
/// @param {number}  $weight    Weight inside the gradient 0..1
/// @param {color[]} $colors    One or multiple colors
///
/// @return {color}
///
@function color-mix($weight, $colors...) {
    $steps: length($colors) - 1;

    @if $steps == 0 {
        @return nth($colors, 1);
    }

    @if unit($weight) == '%' {
        $weight: $weight / 100%;
    }

    $step: max(1, ceil($weight * $steps));
    $step-weight: max(0, min(1, ($weight - ($step - 1) * (1 / $steps)) / (1 / $steps)));

    @return mix(nth($colors, $step + 1), nth($colors, $step), $step-weight * 100%);
}


// MATH mixins
@function pi() {
    @return 3.14159265359;
}

@function pow($number, $exp) {
    $value: 1;

    @if $exp > 0 {
        @for $i from 1 through $exp {
            $value: $value * $number;
        }
    }
    @else if $exp < 0 {
        @for $i from 1 through -$exp {
            $value: $value / $number;
        }
    }

    @return $value;
}

@function fact($number) {
    $value: 1;

    @if $number > 0 {
        @for $i from 1 through $number {
            $value: $value * $i;
        }
    }

    @return $value;
}

@function rad($angle) {
    $unit: unit($angle);
    $unitless: $angle / ($angle * 0 + 1);

    // if the angle has 'deg' as unit, convert to radians
    @if $unit == deg {
        $unitless: $unitless / 180 * pi();
    }

    @return $unitless;
}

@function sin($angle) {
    $sin: 0;
    $angle: rad($angle);

    // iterate a bunch of times
    @for $i from 0 through 10 {
        $sin: $sin + pow(-1, $i) * pow($angle, (2 * $i + 1)) / fact(2 * $i + 1);
    }

    @return $sin;
}

@function cos($angle) {
    $cos: 0;
    $angle: rad($angle);

    // iterate a bunch of times
    @for $i from 0 through 10 {
        $cos: $cos + pow(-1, $i) * pow($angle, 2 * $i) / fact(2 * $i);
    }

    @return $cos;
}

@function tan($angle) {
    @return sin($angle) / cos($angle);
}
