1require 'compass'
2Compass::Frameworks.register("sassy-math", :path => "#{File.dirname(__FILE__)}/..")
3
4# Sassy math Functions
5module Sass::Script::Functions
6  # Exponents
7  def exponent(base, powerNum, powerDen)
8    base = base.value.to_f
9    powerNum = powerNum.value.to_f
10    powerDen = powerDen.value.to_f
11    result = base ** (powerNum / powerDen)
12    Sass::Script::Number.new(result)
13  end
14  def power(base, exponent)
15    base = base.value.to_f
16    exponent = exponent.value.to_f
17    result = base ** exponent
18    Sass::Script::Number.new(result)
19  end
20  def sqrt(number)
21    number = number.value.to_f
22    result = Math.sqrt(number)
23    Sass::Script::Number.new(result)
24  end
25  def nth_root(number, root)
26    number = number.value.to_f
27    root = root.value.to_f
28    result = number ** (1.0 / root)
29    Sass::Script::Number.new(result)
30  end
31  # Logarithms
32  def ln(num)
33    result = Math.log(num.value)
34    Sass::Script::Number.new(result)
35  end
36  def log10(num)
37    result = Math.log10(num.value)
38    Sass::Script::Number.new(result)
39  end
40  # Miscellaneous
41  def factorial(number)
42    result = 1
43    number = number.value
44    if number > 0
45      (1..number).each do |i|
46        result = result * i
47      end
48    end
49    Sass::Script::Number.new(result)
50  end
51  def random(max = Sass::Script::Number.new(100)) ## shamelessly taken from here: https://gist.github.com/1561650
52    Sass::Script::Number.new(rand(max.value), max.numerator_units, max.denominator_units)
53  end
54  def hypot(a, b)
55    a = a.value.to_f
56    b = b.value.to_f
57    result = Math.hypot(a, b)
58    Sass::Script::Number.new(result)
59  end
60  # Constants
61  def pi
62    pi = Math::PI
63    Sass::Script::Number.new(pi)
64  end
65  def e
66    e = Math::E
67    Sass::Script::Number.new(e)
68  end
69  def golden_ratio()
70    result = (1.0 / 2.0) + (Math.sqrt(5) / 2.0)
71    Sass::Script::Number.new(result)
72  end
73  # Comparative Functions
74  def is_int(number)
75    number = number.value.to_f
76    if number - number.floor != 0
77      result = false
78    else
79      result = true
80    end
81    Sass::Script::Bool.new(result)
82  end
83  def is_float(number)
84    number = number.value
85    if number - number.floor != 0
86      result = true
87    else
88      result = false
89    end
90    Sass::Script::Bool.new(result)
91  end
92  # Trigonometric Functions
93  def deg_to_rad(degree)
94    result = degree.value.to_f * Math::PI / 180
95    Sass::Script::Number.new(result)
96  end
97  def rad_to_deg(rad)
98    result = rad.value.to_f * 180 / Math::PI
99    Sass::Script::Number.new(result)
100  end
101  def cosh(rad)
102    rad = rad.value.to_f
103    result = Math.cosh(rad)
104    Sass::Script::Number.new(result)
105  end
106  def acos(rad)
107    rad = rad.value.to_f
108    result = Math.acos(rad)
109    Sass::Script::Number.new(result)
110  end
111  def acosh(rad)
112    rad = rad.value.to_f
113    result = Math.acosh(rad)
114    Sass::Script::Number.new(result)
115  end
116  def sinh(rad)
117    rad = rad.value.to_f
118    result = Math.sinh(rad)
119    Sass::Script::Number.new(result)
120  end
121  def asin(rad)
122    rad = rad.value.to_f
123    result = Math.asin(rad)
124    Sass::Script::Number.new(result)
125  end
126  def asinh(rad)
127    rad = rad.value.to_f
128    result = Math.asinh(rad)
129    Sass::Script::Number.new(result)
130  end
131  def tanh(rad)
132    rad = rad.value.to_f
133    result = Math.tanh(rad)
134    Sass::Script::Number.new(result)
135  end
136  def atan(rad)
137    rad = rad.value.to_f
138    result = Math.atan(rad)
139    Sass::Script::Number.new(result)
140  end
141  def atan2(y, x)
142    y = y.value.to_f
143    x = x.value.to_f
144    result = Math.atan2(y, x)
145    Sass::Script::Number.new(result)
146  end
147  def atanh(rad)
148    rad = rad.value.to_f
149    result = Math.atanh(rad)
150    Sass::Script::Number.new(result)
151  end
152end
153
154module SassyMath
155
156  VERSION = "1.5"
157  DATE = "2012-07-29"
158
159end