Module: Doohickey::SassExtensions::Gradients::Functions

Defined in:
lib/doohickey/sass_extensions/gradients.rb

Instance Method Summary collapse

Instance Method Details

#color_stops(*args) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/doohickey/sass_extensions/gradients.rb', line 143

def color_stops(*args)
  List.new(*args.map do |arg|
    case arg
    when Sass::Script::Color
      ColorStop.new(arg)
    when Sass::Script::String
      # We get a string as the result of concatenation
      # So we have to reparse the expression
      color = stop = nil
      expr = Sass::Script::Parser.parse(arg.value, 0, 0)
      case expr
      when Sass::Script::Color
        color = expr
      when Sass::Script::Funcall
        color = expr
      when Sass::Script::Operation
        unless expr.instance_variable_get("@operator") == :concat
          # This should never happen.
          raise Sass::SyntaxError, "Couldn't parse a color stop from: #{arg.value}"
        end
        color = expr.instance_variable_get("@operand1")
        stop = expr.instance_variable_get("@operand2")
      else
        raise Sass::SyntaxError, "Couldn't parse a color stop from: #{arg.value}"
      end
      ColorStop.new(color, stop)
    else
      raise Sass::SyntaxError, "Not a valid color stop: #{arg}"
    end
  end)
end

#first_color_stop(color_list) ⇒ Object

Return the color of the first color stop



65
66
67
68
# File 'lib/doohickey/sass_extensions/gradients.rb', line 65

def first_color_stop(color_list)
  color = color_list.values.first.color.inspect
  Sass::Script::String.new(color)
end

#grad_color_stops(color_list) ⇒ Object

returns color-stop() calls for use in webkit.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/doohickey/sass_extensions/gradients.rb', line 71

def grad_color_stops(color_list)
  assert_list(color_list)
  normalize_stops!(color_list)
  max = color_list.values.last.stop
  color_stops = color_list.values.map do |pos|
    # have to convert absolute units to percentages for use in color stop functions.
    stop = pos.stop
    stop = stop.div(max).times(Sass::Script::Number.new(100,["%"])) if stop.numerator_units == max.numerator_units
    "color-stop(#{stop.inspect}, #{pos.color.inspect})"
  end
  Sass::Script::String.new(color_stops.join(", "))
end

#grad_end_position(color_list, radial = Sass::Script::Bool.new(false)) ⇒ Object

returns the end position of the gradient from the color stop



85
86
87
88
89
# File 'lib/doohickey/sass_extensions/gradients.rb', line 85

def grad_end_position(color_list, radial = Sass::Script::Bool.new(false))
  assert_list(color_list)
  default = Sass::Script::Number.new(100)
  grad_position(color_list, Sass::Script::Number.new(color_list.values.size), default, radial)
end

#grad_opposite_position(position) ⇒ Object

returns the opposite position of a side or corner.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/doohickey/sass_extensions/gradients.rb', line 49

def grad_opposite_position(position)
  opposite = position.value.split(/ +/).map do |pos|
    case pos
    when "top" then "bottom"
    when "bottom" then "top"
    when "left" then "right"
    when "right" then "left"
    when "center" then "center"
    else
      raise Sass::SyntaxError, "Cannot determine the opposite of #{pos}"
    end
  end
  Sass::Script::String.new(opposite.join(" "))
end

#grad_point(position) ⇒ Object

the given a position, return a point in percents



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/doohickey/sass_extensions/gradients.rb', line 117

def grad_point(position)
  position = position.value
  position = if position[" "]
    if position =~ /(top|bottom|center) (left|right|center)/
      "#{$2} #{$1}"
    else
      position
    end
  else
    case position
    when /top|bottom/
      "left #{position}"
    when /left|right/
      "#{position} top"
    else
      position
    end
  end
  Sass::Script::String.new(position.
    gsub(/top/, "0%").
    gsub(/bottom/, "100%").
    gsub(/left/,"0%").
    gsub(/right/,"100%").
    gsub(/center/, "50%"))
end

#grad_position(color_list, index, default, radial = Sass::Script::Bool.new(false)) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/doohickey/sass_extensions/gradients.rb', line 91

def grad_position(color_list, index, default, radial = Sass::Script::Bool.new(false))
  assert_list(color_list)
  stop = color_list.values[index.value - 1].stop
  if stop && radial.to_bool
    orig_stop = stop
    if stop.unitless?
      if stop.value <= 1
        # A unitless number is assumed to be a percentage when it's between 0 and 1
        stop = stop.times(Sass::Script::Number.new(100, ["%"]))
      else
        # Otherwise, a unitless number is assumed to be in pixels
        stop = stop.times(Sass::Script::Number.new(1, ["px"]))
      end
    end
    if stop.numerator_units == ["%"] && color_list.values.last.stop && color_list.values.last.stop.numerator_units == ["px"]
      stop = stop.times(color_list.values.last.stop).div(Sass::Script::Number.new(100, ["%"]))
    end
    stop.div(Sass::Script::Number.new(1, stop.numerator_units, stop.denominator_units))
  elsif stop
    stop
  else
    default
  end
end