module Sass::Script::Functions
def abs(value)
-
(Sass::SyntaxError)
- if `value` isn't a number
Returns:
-
(Number)
- The absolute value
Parameters:
-
value
(Number
) -- The number
def abs(value) numeric_transformation(value) {|n| n.abs} end
def ceil(value)
-
(Sass::SyntaxError)
- if `value` isn't a number
Returns:
-
(Number)
- The rounded number
Parameters:
-
value
(Number
) -- The number
def ceil(value) numeric_transformation(value) {|n| n.ceil} end
def floor(value)
-
(Sass::SyntaxError)
- if `value` isn't a number
Returns:
-
(Number)
- The rounded number
Parameters:
-
value
(Number
) -- The number
def floor(value) numeric_transformation(value) {|n| n.floor} end
def hsl(hue, saturation, lightness)
-
(ArgumentError)
- if `saturation` or `lightness` are out of bounds
Returns:
-
(Color)
- The resulting color
Parameters:
-
lightness
(Number
) -- The lightness of the color. -
saturation
(Number
) -- The saturation of the color. -
hue
(Number
) -- The hue of the color.
def hsl(hue, saturation, lightness) original_s = saturation original_l = lightness # This algorithm is from http://www.w3.org/TR/css3-color#hsl-color h, s, l = [hue, saturation, lightness].map { |a| a.value } raise ArgumentError.new("Saturation #{s} must be between 0% and 100%") if s < 0 || s > 100 raise ArgumentError.new("Lightness #{l} must be between 0% and 100%") if l < 0 || l > 100 h = (h % 360) / 360.0 s /= 100.0 l /= 100.0 m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s m1 = l * 2 - m2 Color.new([hue_to_rgb(m1, m2, h + 1.0/3), hue_to_rgb(m1, m2, h), hue_to_rgb(m1, m2, h - 1.0/3)].map { |c| (c * 0xff).round }) end
def hue_to_rgb(m1, m2, h)
def hue_to_rgb(m1, m2, h) h += 1 if h < 0 h -= 1 if h > 1 return m1 + (m2 - m1) * h * 6 if h * 6 < 1 return m2 if h * 2 < 1 return m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2 return m1 end
def numeric_transformation(value)
another numeric value with the same units.
This method implements the pattern of transforming a numeric value into
def numeric_transformation(value) unless value.is_a?(Sass::Script::Number) calling_function = caller.first.scan(/`([^']+)'/).first.first raise Sass::SyntaxError.new("#{value} is not a number for `#{calling_function}'") end Sass::Script::Number.new(yield(value.value), value.numerator_units, value.denominator_units) end
def percentage(value)
-
(ArgumentError)
- If `value` isn't a unitless number
Returns:
-
(Number)
- The percentage
Parameters:
-
value
(Number
) -- The decimal number to convert to a percentage
def percentage(value) unless value.is_a?(Sass::Script::Number) && value.unitless? raise ArgumentError.new("#{value} is not a unitless number") end Sass::Script::Number.new(value.value * 100, ['%']) end
def rgb(red, green, blue)
-
blue
() --
-
green
() --
-
red
() --
def rgb(red, green, blue) [red.value, green.value, blue.value].each do |v| next unless v < 0 || v > 255 raise ArgumentError.new("Color value #{v} must be between 0 and 255 inclusive") end Color.new([red.value, green.value, blue.value]) end
def round(value)
-
(Sass::SyntaxError)
- if `value` isn't a number
Returns:
-
(Number)
- The rounded number
Parameters:
-
value
(Number
) -- The number
def round(value) numeric_transformation(value) {|n| n.round} end