diff --git a/doc-src/content/CHANGELOG.markdown b/doc-src/content/CHANGELOG.markdown index 0e1dd5fd..f5dbb112 100644 --- a/doc-src/content/CHANGELOG.markdown +++ b/doc-src/content/CHANGELOG.markdown @@ -37,6 +37,13 @@ COMPASS CHANGELOG 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` 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 diff --git a/lib/compass/sass_extensions/functions.rb b/lib/compass/sass_extensions/functions.rb index 083ea6da..922268e4 100644 --- a/lib/compass/sass_extensions/functions.rb +++ b/lib/compass/sass_extensions/functions.rb @@ -4,7 +4,7 @@ end %w( selectors enumerate urls display inline_image image_size gradient_support - font_files constants lists colors + font_files constants lists colors trig ).each do |func| require "compass/sass_extensions/functions/#{func}" end @@ -21,6 +21,7 @@ module Sass::Script::Functions include Compass::SassExtensions::Functions::Constants include Compass::SassExtensions::Functions::Lists include Compass::SassExtensions::Functions::Colors + include Compass::SassExtensions::Functions::Trig end # Wierd that this has to be re-included to pick up sub-modules. Ruby bug? diff --git a/lib/compass/sass_extensions/functions/trig.rb b/lib/compass/sass_extensions/functions/trig.rb new file mode 100644 index 00000000..95c38508 --- /dev/null +++ b/lib/compass/sass_extensions/functions/trig.rb @@ -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 diff --git a/test/sass_extensions_test.rb b/test/sass_extensions_test.rb index 7d8e018a..601ff0c4 100644 --- a/test/sass_extensions_test.rb +++ b/test/sass_extensions_test.rb @@ -59,6 +59,23 @@ class SassExtensionsTest < Test::Unit::TestCase assert_equal "25%", evaluate("saturation(adjust-saturation(hsl(50deg, 50%, 50%), -25%))") 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 def evaluate(value) Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s