class Less::Node::Color

rgb(255, 0, 0) f0f0f0

Attributes

a[R]
b[R]
g[R]
r[R]

Public Class Methods

new(r, g, b, a = 1.0) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 18
def initialize r, g, b, a = 1.0
  @r, @g, @b = [r, g, b].map do |c|
    normalize(c.is_a?(String) ? c.to_i(16) : c)
  end
  @a = normalize(a, 1.0)
end

Public Instance Methods

*(other;) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 44
def * other; operate :*, other end
+(other;) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 42
def + other; operate :+, other end
-(other;) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 43
def - other; operate :-, other end
/(other;) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 45
def / other; operate :/, other end
alpha(v) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 25
def alpha v
  self.class.new r, g, b, v
end
coerce(other) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 47
def coerce other
  return self, other
end
inspect() click to toggle source
# File lib/less/engine/nodes/literal.rb, line 59
def inspect
  if a < 1.0
    "rgba(#{r}, #{g}, #{b}, #{a})"
  else
    "rgb(#{r}, #{g}, #{b})"
  end
end
operate(op, other) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 33
def operate op, other
  color = if other.is_a? Numeric
    rgb.map {|c| c.send(op, other) }
  else
    rgb.zip(other.rgb).map {|a, b| a.send(op, b) }
  end
  self.class.new *[color, @a].flatten # Ruby 1.8 hack
end
rgb() click to toggle source
# File lib/less/engine/nodes/literal.rb, line 29
def rgb
  [r, g, b]
end
to_css() click to toggle source
# File lib/less/engine/nodes/literal.rb, line 67
def to_css
  to_s
end
to_ruby() click to toggle source
# File lib/less/engine/nodes/literal.rb, line 71
def to_ruby
  "#{self.class}.new(#{r},#{g},#{b},#{a})"
end
to_s() click to toggle source
# File lib/less/engine/nodes/literal.rb, line 51
def to_s
  if a < 1.0
    "rgba(#{r.to_i}, #{g.to_i}, #{b.to_i}, #{a})"
  else
    "#%02x%02x%02x" % [r, g, b]
  end
end

Protected Instance Methods

normalize(v, max = 255, min = 0) click to toggle source
# File lib/less/engine/nodes/literal.rb, line 76
def normalize(v, max = 255, min = 0)
  [[min, v].max, max].min
end