Added trigonometry functions for fun and profit.

Closes GH-201.
This commit is contained in:
Chris Eppstein 2010-11-12 15:14:37 -08:00
parent c0533a01d9
commit 463301e983
4 changed files with 53 additions and 1 deletions

View File

@ -37,6 +37,13 @@ COMPASS CHANGELOG
make it easier to construct apis that manipulate these color attributes. make it easier to construct apis that manipulate these color attributes.
* The `elements-of-type()` helper now returns html5 elements when the display is `block` * The `elements-of-type()` helper now returns html5 elements when the display is `block`
and also will return only html5 elements for `elements-of-type(html5)` and also will return only html5 elements for `elements-of-type(html5)`
* Compass now provides several helper functions related to trigonometry.
There's no practical use, but it's hoped that users will find fun things to
do with these for technology demonstrations:
* `sin($number)` - Takes the sine of the number.
* `cos($number)` - Takes the cosine of the number.
* `tan($number)` - Takes the tangent of the number.
* `pi()` - Returns the value of π.
### Rails ### Rails

View File

@ -4,7 +4,7 @@ end
%w( %w(
selectors enumerate urls display selectors enumerate urls display
inline_image image_size gradient_support inline_image image_size gradient_support
font_files constants lists colors font_files constants lists colors trig
).each do |func| ).each do |func|
require "compass/sass_extensions/functions/#{func}" require "compass/sass_extensions/functions/#{func}"
end end
@ -21,6 +21,7 @@ module Sass::Script::Functions
include Compass::SassExtensions::Functions::Constants include Compass::SassExtensions::Functions::Constants
include Compass::SassExtensions::Functions::Lists include Compass::SassExtensions::Functions::Lists
include Compass::SassExtensions::Functions::Colors include Compass::SassExtensions::Functions::Colors
include Compass::SassExtensions::Functions::Trig
end end
# Wierd that this has to be re-included to pick up sub-modules. Ruby bug? # Wierd that this has to be re-included to pick up sub-modules. Ruby bug?

View File

@ -0,0 +1,27 @@
module Compass::SassExtensions::Functions::Trig
def pi()
Sass::Script::Number.new(Math::PI)
end
def sin(number)
trig(:sin, number)
end
def cos(number)
trig(:cos, number)
end
def tan(number)
trig(:tan, number)
end
private
def trig(operation, number)
if number.numerator_units == ["deg"] && number.denominator_units == []
Sass::Script::Number.new(Math.send(operation, Math::PI * number.value / 360))
else
Sass::Script::Number.new(Math.send(operation, number.value), number.numerator_units, number.denominator_units)
end
end
end

View File

@ -59,6 +59,23 @@ class SassExtensionsTest < Test::Unit::TestCase
assert_equal "25%", evaluate("saturation(adjust-saturation(hsl(50deg, 50%, 50%), -25%))") assert_equal "25%", evaluate("saturation(adjust-saturation(hsl(50deg, 50%, 50%), -25%))")
end end
def test_trig_functions
assert_equal "0.841px", evaluate("sin(1px)")
assert_equal "0.0", evaluate("sin(pi())")
assert_equal "1", evaluate("sin(pi() / 2)")
assert_equal "1", evaluate("sin(180deg)")
assert_equal "-1", evaluate("sin(3* pi() / 2)")
assert_equal "-1", evaluate("cos(pi())")
assert_equal "-1", evaluate("cos(360deg)")
assert_equal "1", evaluate("cos(2*pi())")
assert_equal "0.0", evaluate("cos(pi() / 2)")
assert_equal "0.0", evaluate("cos(3* pi() / 2)")
assert_equal "0.0", evaluate("tan(pi())")
assert_equal "0.0", evaluate("tan(360deg)")
assert evaluate("tan(pi()/2 - 0.0001)").to_f > 1000, evaluate("tan(pi()/2 - 0.0001)")
assert evaluate("tan(pi()/2 + 0.0001)").to_f < -1000, evaluate("tan(pi()/2 - 0.0001)")
end
protected protected
def evaluate(value) def evaluate(value)
Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s