@import compass

=harsh($color: random, $background: white, $transparency: .5, $number-of-gradients: 10, $angle: random, $nuance: true, $nuance-strength: 10) 
  +background(gradient-combiner($color, $background, $transparency, $number-of-gradients, $angle, $nuance, $nuance-strength))

=harsh-text($color: random, $transparency: .5, $number-of-gradients: 10, $angle: random, $nuance: true, $nuance-strength: 10, $background: false) 
  +background-image(gradient-combiner($color, $background, $transparency, $number-of-gradients, $angle, $nuance, $nuance-strength))
  -webkit-background-clip: text
  -webkit-text-fill-color: transparent

@function gradient-combiner($color, $background, $transparency, $number-of-gradients, $angle, $nuance, $nuance-strength)
  $all: ()
  @if type-of($color) == list
    @each $grad-color in $color
      $all: append($all, gradient-builder($grad-color, $transparency, $angle, $nuance, $nuance-strength), comma)
  @else
    @for $i from 1 through $number-of-gradients
      $all: append($all, gradient-builder($color, $transparency, $angle, $nuance, $nuance-strength), comma)
  @if $background != false 
    $all: append($all, $background, comma)
  @return $all

@function gradient-builder($color, $transparency, $angle, $nuance, $nuance-strength)  
  $base-color: base-finder($color, $nuance, $nuance-strength)
  $transparent-color: fade-out($base-color, $transparency)
  $colorstop: harsh_random()*1%
  $gradient: linear-gradient(angle-finder($angle), $transparent-color $colorstop, transparent $colorstop+.000001) //.001 to fix Compass bug/error where second color stop isn't larger than first
  @return $gradient
 
@function base-finder($color, $nuance, $nuance-strength)
  @if $color == random
    @return harsh_random(color)
  @else if type-of($color) == color
    @if $nuance
      @return nuance($color, $nuance, $nuance-strength)
    @else
      @return $color

@function angle-finder($angle) //http://dev.w3.org/csswg/css3-images/#linear-gradients
  @if $angle == random
    @return  harsh_random()*1% harsh_random()*1%
  @if $angle == random-fixed
    @return harsh_random()*1deg
  @else 
    @return $angle

@function nuance($color, $nuance, $nuance-strength)
  @if $nuance
    @return saturate(desaturate(lighten(darken($color, harsh_random(number, $nuance-strength)*1% ),harsh_random(number, $nuance-strength)*1%),harsh_random(number, $nuance-strength)*1%),harsh_random(number, $nuance-strength)*1%)
  @else
    @return $color

