thoughtbot / bitters

Add a dash of pre-defined style to your Bourbon.
https://thoughtbot.com
MIT License
1.39k stars 142 forks source link

Add decimal support to modular_scale #167

Closed drtimofey closed 9 years ago

drtimofey commented 9 years ago

For example, replace the existing modular_scale() function with:

@function modular-scale($increment, $value: $modular-scale-base, $ratio: $modular-scale-ratio) {
  @return $value * (math-pow($ratio, $increment));
}

@function math-pow($number, $exp) {
  @if (round($exp) != $exp) {
    @return math-exp($exp * math-ln($number));
  }

  // Traditional method for integers
  $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 math-factorial($value) {
  @if $value == 0 {
    @return 1;
  }

  $result: 1;

  @for $index from 1 through $value {
    $result: $result * $index;
  }

  @return $result;
}

@function math-summation($iteratee, $input, $initial: 0, $limit: 100) {
  $sum: 0;

  @for $index from $initial to $limit {
    $sum: $sum + call($iteratee, $input, $index);
  }

  @return $sum;
}

@function math-exp-maclaurin($x, $n) {
  $result: math-pow($x, $n) / math-factorial($n);
  @return $result;
}
@function math-exp($value) {
  $result: math-summation(math-exp-maclaurin, $value, 0, 100);
  @return $result;
}

@function math-ln-maclaurin($x, $n) {
  $result: (math-pow(-1, $n + 1) / $n) * (math-pow($x - 1, $n));
  @return $result;
}
@function math-ln($value) {
  $ten-exp: 1;
  $ln-ten: 2.30258509;

  @while ($value > math-pow(10, $ten-exp)) {
    $ten-exp: $ten-exp + 1;
  }

  $value: $value / math-pow(10, $ten-exp);

  $result: math-summation(math-ln-maclaurin, $value, 1, 100);

  @return $result + $ten-exp * $ln-ten;
}

This script is based on https://gist.github.com/davidkpiano/ad6e6771df050ff3727f To see SASS progress on exponents: https://github.com/sass/sass/issues/684

tysongach commented 9 years ago

@drtimofey Hi! The modular-scale function is a feature of Bourbon, not Bitters. Can you file an issue in Bourbon’s repo instead?

Also: Can you clarify use? For example, is this what your hoping to accomplish: modular-scale(1.2)?

drtimofey commented 9 years ago

@tysongach Hi, apologies, I should of filed it under Bourbon! Yes, it allows decimals like modular-scale(1.2).

drtimofey commented 9 years ago

For reference purposes, issue has been reposted to https://github.com/thoughtbot/bourbon/issues/717