Class: Compass::Core::SassExtensions::Functions::GradientSupport::ColorStop

Inherits:
Sass::Script::Value::Base show all
Includes:
Sass::Script::Value::Helpers
Defined in:
lib/compass/core/sass_extensions/functions/gradient_support.rb

Constant Summary

Constants inherited from Sass::Script::Value::Base

Sass::Script::Value::Base::NO_CHILDREN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sass::Script::Value::Base

#opts

Constructor Details

#initialize(color, stop = nil) ⇒ ColorStop

Returns a new instance of ColorStop.



152
153
154
155
156
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 152

def initialize(color, stop = nil)
  assert_legal_color! color
  assert_legal_color_stop! stop if stop
  self.color, self.stop = color, stop
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



148
149
150
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 148

def color
  @color
end

#stopObject

Returns the value of attribute stop.



148
149
150
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 148

def stop
  @stop
end

Class Method Details

.color_to_s(c) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 203

def self.color_to_s(c)
  if c.is_a?(Sass::Script::Value::String)
    c.value.dup
  else
    c.inspect.dup
  end
end

.color_to_svg_alpha(c) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 191

def self.color_to_svg_alpha(c)
  # svg doesn't support the "transparent" keyword; we need to manually
  # refactor it into "transparent black"
  if c.is_a?(Sass::Script::Value::String) && c.value == "transparent"
    0
  elsif c.is_a?(Sass::Script::Value::String) && c.value == "currentColor"
    1
  else
    c.alpha
  end
end

.color_to_svg_s(c) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 179

def self.color_to_svg_s(c)
  # svg doesn't support the "transparent" keyword; we need to manually
  # refactor it into "transparent black"
  if c.is_a?(Sass::Script::Value::String) && c.value == "transparent"
    "black"
  elsif c.is_a?(Sass::Script::Value::String)
    c.value.dup
  else
    self.color_to_s(c.with(:alpha => 1))
  end
end

Instance Method Details



161
162
163
164
165
166
167
168
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 161

def assert_legal_color!(color)
  unless Sass::Script::Value::Color === color ||
         Sass::Script::Tree::Funcall === color ||
         (Sass::Script::Value::String === color && color.value == "currentColor")||
         (Sass::Script::Value::String === color && color.value == "transparent")
    raise Sass::SyntaxError, "Expected a color. Got: #{color}"
  end
end

Raises:

  • (Sass::SyntaxError)


169
170
171
172
173
174
175
176
177
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 169

def assert_legal_color_stop!(stop)
  case stop
  when Sass::Script::Value::String
    return stop.value.start_with?("calc(")
  when Sass::Script::Value::Number
    return true
  end
  raise Sass::SyntaxError, "Expected a number or numerical expression. Got: #{stop.inspect}"
end

#childrenObject



149
150
151
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 149

def children
  [color, stop].compact
end

#inspectObject



157
158
159
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 157

def inspect
  to_s
end

#to_s(options = self.options) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 211

def to_s(options = self.options)
  s = self.class.color_to_s(color)
  if stop
    s << " "
    if stop.respond_to?(:unitless?) && stop.unitless?
      s << stop.times(number(100, "%")).inspect
    else
      s << stop.to_s
    end
  end
  s
end

#to_sass(options = nil) ⇒ Object



224
225
226
# File 'lib/compass/core/sass_extensions/functions/gradient_support.rb', line 224

def to_sass(options = nil)
  identifier("color-stop(#{color.to_sass rescue nil}, #{stop.to_sass rescue nil})")
end