// ----
// Utilities
// ----

/// Power function
/// @param {Number} $x
/// @param {Number} $n
/// @return {Number}
/// @group utilities
/// @source https://github.com/adambom/Sass-Math/blob/master/math.scss Sass-Math
@function pow($x, $n) {
  $result: 1;

  @if $n >= 0 {
    @for $i from 1 through $n {
      $result: $result * $x;
    }
  } @else {
    @for $i from $n to 0 {
      $result: $result / $x;
      }
    }
  @return $result;
}

///
/// Calculates and returns the rem value based on px input. Default base font
/// size is 16px, but can be changed with a global `$base-font-size` variable.
///
/// @param {variable} $size - Size in pixels
///
/// @group utilities
/// @example
///   font-size: rem(30px);
///   // returns font-size: 1.875rem;

@function rem($size) {
  $rem-size: $size / $base-font-size;
  @return #{$rem-size}rem;
}

//- Function: Strip Unit
///
/// Sometimes it's just easier to do calculations with the unit is stripped.
///
/// @param {number} $number - Number that has a unit
///
/// @group utilities
/// @example
///   $length: 42px;
///   $value: strip-unit($length);
///   // -> 42
/// @link https://css-tricks.com/snippets/sass/strip-unit-function/

@function strip-unit($number) {
  @if type-of($number) == 'number' and not unitless($number) {
    @return $number / ($number * 0 + 1);
  }
  @return $number;
}

//- Mixin: Debug
///
/// Debugging tool - adds a border to the current element, its children,
/// grandchildren, etc so you can see what's up – great for precise layout
/// tweaks.  It will also add flags if you made mistakes like put in inline
/// styles, forgot an alt on an image, left the alt blank, etc. Not to be used
/// in production, obviously.
///
/// @group utilities
/// @example
///   .container-elment {
///     @include debug();
///   }
/// @link http://codepen.io/dbox/pen/GJZzYo?editors=110

@mixin debug() {> * {  border: 1px solid green;} > * > * {border: 1px solid tomato; } > * > * > * { border: 1px solid DeepSkyBlue; } > * > * > * > * { border: 1px solid DeepPink; } > * > * > * > * > * { border: 1px solid MediumSlateBlue;}}

// Mixin: Triangle
//
// Makes a little css triangle for you. Pass it a direction (up, down, left,
// right), size (in pixels), and a color.
//
// ex. @include triangle()
// ex. @include triangle('down', 15px, blue)

//- Mixin: Triangle
///
/// Makes a little css triangle for you.
///
/// @param {variable} $direction [up] - Direction of the triangle: up, down left
/// or right
/// @param {variable} $size [10px] - Size of the triangle
/// @param {variable} $color [#000] - Color of the triangle
///
/// @group utilities
/// @example
///   .element:after {
///     @include triangle();
///   }
///   span {
///     @include triangle(right, 8px, red);
///   }

@mixin triangle($direction:'up', $size:10px, $color:#000) {
  width: 0;
  height: 0;

  @if $direction == 'up' {
    border-bottom: $size solid $color;
    border-left: $size solid transparent;
    border-right: $size solid transparent;
  } @else if $direction == 'down' {
    border-left: $size solid transparent;
    border-right: $size solid transparent;
    border-top: $size solid $color;
  } @else if $direction == 'left' {
    border-bottom: $size solid transparent;
    border-right: $size solid $color;
    border-top: $size solid transparent;
  } @else if $direction == 'right' {
    border-bottom: $size solid transparent;
    border-left: $size solid $color;
    border-top: $size solid transparent;
  }
}

/// Returns the luminance of `$color` as a float (between 0 and 1)
/// 1 is pure white, 0 is pure black
/// @group utilities
/// @param {Color} $color - Color
/// @return {Number}
/// @link http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef Reference
/// @author Hugo Giradel

@function luminance($color) {
  $colors: (
    'red': red($color),
    'green': green($color),
    'blue': blue($color)
  );

  @each $name, $value in $colors {
    $adjusted: 0;
    $value: $value / 255;

    @if $value < 0.03928 {
      $value: $value / 12.92;
    } @else {
      $value: ($value + .055) / 1.055;
      $value: pow($value, 2);
    }

    $colors: map-merge($colors, ($name: $value));
  }

  @return (map-get($colors, 'red') * .2126) + (map-get($colors, 'green') * .7152) + (map-get($colors, 'blue') * .0722);
}
