diff --git a/doc-src/content/reference/compass/helpers.haml b/doc-src/content/reference/compass/helpers.haml index f2b75453..967f5e01 100644 --- a/doc-src/content/reference/compass/helpers.haml +++ b/doc-src/content/reference/compass/helpers.haml @@ -23,8 +23,9 @@ layout: core * [adjust-saturation()](/reference/compass/helpers/colors/#adjust-saturation) * [append-selector()](/reference/compass/helpers/selectors/#append-selector) * [color-stops()](/reference/compass/helpers/color-stops/) - * [cos()](/reference/compass/helpers/trig/#cos) + * [cos()](/reference/compass/helpers/math/#cos) * [css2-fallback()](/reference/compass/helpers/cross-browser/#css2-fallback) + * [e()](/reference/compass/helpers/math/#e) * [elements-of-type()](/reference/compass/helpers/display/) * [enumerate()](/reference/compass/helpers/selectors/#enumerate) * [font-files()](/reference/compass/helpers/font-files/) @@ -35,14 +36,17 @@ layout: core * [image-url()](/reference/compass/helpers/urls/#image-url) * [inline-font-files()](/reference/compass/helpers/inline-data/#inline-font-files) * [inline-image()](/reference/compass/helpers/inline-data/#inline-image) + * [log()](/reference/compass/helpers/math/#log) * [nest()](/reference/compass/helpers/selectors/#nest) + * [pow()](/reference/compass/helpers/math/#pow) * [prefix()](/reference/compass/helpers/cross-browser/#prefix) * [prefixed()](/reference/compass/helpers/cross-browser/#prefixed) - * [pi()](/reference/compass/helpers/trig/#pi) - * [sin()](/reference/compass/helpers/trig/#sin) + * [pi()](/reference/compass/helpers/math/#pi) + * [sin()](/reference/compass/helpers/math/#sin) + * [sqrt()](/reference/compass/helpers/math/#sqrt) * [stylesheet-url()](/reference/compass/helpers/urls/#stylesheet-url) * [scale-lightness()](/reference/compass/helpers/colors/#scale-lightness) - * [tan()](/reference/compass/helpers/trig/#tan) + * [tan()](/reference/compass/helpers/math/#tan) * [-css2()](/reference/compass/helpers/cross-browser/#-css2) * [-moz()](/reference/compass/helpers/cross-browser/#-moz) * [-ms()](/reference/compass/helpers/cross-browser/#-ms) diff --git a/doc-src/content/reference/compass/helpers/trig.haml b/doc-src/content/reference/compass/helpers/math.haml similarity index 61% rename from doc-src/content/reference/compass/helpers/trig.haml rename to doc-src/content/reference/compass/helpers/math.haml index 2147d2b2..202152a8 100644 --- a/doc-src/content/reference/compass/helpers/trig.haml +++ b/doc-src/content/reference/compass/helpers/math.haml @@ -1,8 +1,8 @@ --- -title: Compass Trigonometric Helpers -crumb: Trig +title: Compass Math Helpers +crumb: Math framework: compass -meta_description: Helper functions for working with angles. +meta_description: Helper math functions. layout: core classnames: - reference @@ -13,12 +13,15 @@ documented_functions: - "sin" - "cos" - "tan" + - "e" + - "log" + - "sqrt" + - "pow" --- -%h1 Compass Trig Helpers +%h1 Compass Math Helpers :markdown - Trigonometric functions can help you manage complex calculations involving angles. - Or maybe they can be used to create pretty pictures. Who knows? + Sass math functions are sufficient for most cases, but in those moments of extreme geekiness these additional functions can really come in handy. #pi.helper @@ -50,6 +53,7 @@ documented_functions: then it will return a unitless result. Degrees will first be converted to radians. If the number is any other unit, the units will be passed thru to the result, and the number will be treated as radians. + #tan.helper %h3 %a(href="#tan") @@ -60,3 +64,35 @@ documented_functions: then it will return a unitless result. Degrees will first be converted to radians. If the number is any other unit, the units will be passed thru to the result, and the number will be treated as radians. + +#e.helper + %h3 + %a(href="#e") + e() + .details + %p + Returns the value of e. + +#log.helper + %h3 + %a(href="#log") + log($number, [$base]) + .details + %p + Calculates the logarithm of a number to a base. Base defaults to e. + +#sqrt.helper + %h3 + %a(href="#sqrt") + sqrt($number) + .details + %p + Calculates the square root of a number. + +#pow.helper + %h3 + %a(href="#pow") + pow($number, $exponent) + .details + %p + Calculates the value of a number raised to the power of an exponent. diff --git a/lib/compass/sass_extensions/functions.rb b/lib/compass/sass_extensions/functions.rb index 99cf687a..de4aff08 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 constants gradient_support - font_files lists colors trig sprites cross_browser_support + font_files lists colors math sprites cross_browser_support ).each do |func| require "compass/sass_extensions/functions/#{func}" end @@ -21,7 +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 + include Compass::SassExtensions::Functions::Math include Compass::SassExtensions::Functions::Sprites include Compass::SassExtensions::Functions::CrossBrowserSupport end diff --git a/lib/compass/sass_extensions/functions/math.rb b/lib/compass/sass_extensions/functions/math.rb new file mode 100644 index 00000000..539e6f10 --- /dev/null +++ b/lib/compass/sass_extensions/functions/math.rb @@ -0,0 +1,47 @@ +module Compass::SassExtensions::Functions::Math + + 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 + + def e() + Sass::Script::Number.new(Math::E) + end + + def log(number, base = Sass::Script::Number.new(Math::E)) + assert_type number, :Number + assert_type base, :Number + Sass::Script::Number.new(Math.log(number.value, base.value), number.numerator_units, number.denominator_units) + end + + def sqrt(number) + numeric_transformation(number) { |n| Math.sqrt(n) } + end + + def pow(number, exponent) + assert_type number, :Number + assert_type exponent, :Number + Sass::Script::Number.new(number.value**exponent.value, number.numerator_units, number.denominator_units) + 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 / 180)) + else + Sass::Script::Number.new(Math.send(operation, number.value), number.numerator_units, number.denominator_units) + end + end +end diff --git a/lib/compass/sass_extensions/functions/trig.rb b/lib/compass/sass_extensions/functions/trig.rb deleted file mode 100644 index 8e87ab4b..00000000 --- a/lib/compass/sass_extensions/functions/trig.rb +++ /dev/null @@ -1,27 +0,0 @@ -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 / 180)) - else - Sass::Script::Number.new(Math.send(operation, number.value), number.numerator_units, number.denominator_units) - end - end -end