Class: Compass::Core::SassExtensions::Functions::GradientSupport::CSS3AngleToSVGConverter

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/compass/core/sass_extensions/functions/gradient_support.rb

Constant Summary collapse

TOP =
1
BOTTOM =
2
RIGHT =
4
LEFT =
8
DIR_KEYWORDS_TO_ANGLE =
{
  TOP            => 0,
  TOP    | RIGHT => 45,
           RIGHT => 90,
  BOTTOM | RIGHT => 135,
  BOTTOM         => 180,
  BOTTOM | LEFT  => 225,
           LEFT  => 270,
  TOP    | LEFT  => 315,
}

Constants included from Math

Math::E, Math::PI

Instance Method Summary collapse

Methods included from Math

#acos, #asin, #atan, #cos, #deprecated_random, #e, included, #logarithm, #pi, #pow, #sin, #sqrt, #tan

Methods included from SassDeclarationHelper

#declare

Constructor Details

#initialize(angle) ⇒ CSS3AngleToSVGConverter

Returns a new instance of CSS3AngleToSVGConverter.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Method Details

#handle_keywords(angle) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



141
142
143
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 141

def result(v)
  round6(scale(v))
end

#round6(v) ⇒ Object



137
138
139
# 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) ⇒ Object



133
134
135
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 133

def scale(p)
  (p + 1) / 2.0
end

#sin2(a) ⇒ Object



76
77
78
79
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 76

def sin2(a)
  v = sin(a)
  v * v
end

#xObject



81
82
83
84
85
86
87
# 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

#x1Object



97
98
99
100
101
102
103
104
# 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

#x2Object



115
116
117
118
119
120
121
122
# 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

#yObject



89
90
91
92
93
94
95
# 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

#y1Object



106
107
108
109
110
111
112
113
# 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

#y2Object



124
125
126
127
128
129
130
131
# 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