class Compass::Core::SassExtensions::Functions::GradientSupport::CSS3AngleToSVGConverter

Constants

BOTTOM
DIR_KEYWORDS_TO_ANGLE
LEFT
TOP

Public Class Methods

new(angle) click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 8
def initialize(angle)
  @original_angle = angle
  @angle = handle_keywords(angle)
  @angle = in_radians(@angle) % (2 * PI)
  @quadrant = (@angle * 2 / PI).to_i
  @angle = case @quadrant
           when 0
             @angle
           when 1
             PI - @angle
           when 2
             @angle - PI
           when 3
             2 * PI  - @angle
           end
end

Public Instance Methods

handle_keywords(angle) click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 41
def handle_keywords(angle)
  if angle.is_a?(Sass::Script::Value::List) || angle.is_a?(Sass::Script::Value::String)
    direction = angle.to_sass
    is_end_point = !!/\bto\b/i.match(direction)
    dir = 0
    dir |= TOP if /\btop\b/i.match(direction)
    dir |= BOTTOM if /\bbottom\b/i.match(direction)
    dir |= RIGHT if /\bright\b/i.match(direction)
    dir |= LEFT if /\bleft\b/i.match(direction)
    if (r = DIR_KEYWORDS_TO_ANGLE[dir])
      r += 180 unless is_end_point
      Sass::Script::Value::Number.new(r, %w(deg), [])
    else
      raise Sass::SyntaxError, "Unknown direction: #{angle.to_sass}"
    end
  else
    angle
  end
end
in_radians(angle) click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 61
def in_radians(angle)
  case angle.unit_str
  when "deg"
    angle.value * PI / 180.0
  when "grad"
    angle.value * PI / 200.0
  when "rad"
    angle.value
  when "turn"
    angle.value * PI * 2
  else
    raise Sass::SyntaxError.new("#{angle.unit_str} is not an angle")
  end
end
result(v) click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 141
def result(v)
  round6(scale(v))
end
round6(v) click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 137
def round6(v)
  (v * 1_000_000).round / 1_000_000.0
end
scale(p) click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 133
def scale(p)
  (p + 1) / 2.0
end
sin2(a) click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 76
def sin2(a)
  v = sin(a)
  v * v
end
x() click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 81
def x
  @x ||= if @angle > 1.570621793869697
           1.0 # avoid floating point rounding error at the asymptote
         else
           tan(@angle) + (1 - tan(@angle)) * sin2(@angle)
         end
end
x1() click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 97
def x1
  result case @quadrant
         when 0, 1
           -x
         when 2, 3
           x
         end
end
x2() click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 115
def x2
  result case @quadrant
         when 0, 1
           x
         when 2, 3
           -x
         end
end
y() click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 89
def y
  @y ||= if @angle < 0.0001
           1.0 # the limit of the expression as we approach 0 is 1.
         else
           x / tan(@angle)
         end
end
y1() click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 106
def y1
  result case @quadrant
         when 0, 3
           y
         when 1, 2
           -y
         end
end
y2() click to toggle source
# File lib/compass/core/sass_extensions/functions/gradient_support.rb, line 124
def y2
  result case @quadrant
         when 0, 3
           -y
         when 1, 2
           y
         end
end