Class: Less::Node::Color

Inherits:
Object show all
Includes:
Literal
Defined in:
lib/less/engine/nodes/literal.rb

Overview

rgb(255, 0, 0) #f0f0f0

Instance Attribute Summary collapse

Attributes included from Entity

#parent

Instance Method Summary collapse

Methods included from Literal

#unit

Methods included from Entity

#path, #root

Constructor Details

#initialize(r, g, b, a = 1.0) ⇒ Color

Returns a new instance of Color.



18
19
20
21
22
23
# 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

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



16
17
18
# File 'lib/less/engine/nodes/literal.rb', line 16

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



16
17
18
# File 'lib/less/engine/nodes/literal.rb', line 16

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g.



16
17
18
# File 'lib/less/engine/nodes/literal.rb', line 16

def g
  @g
end

#rObject (readonly)

Returns the value of attribute r.



16
17
18
# File 'lib/less/engine/nodes/literal.rb', line 16

def r
  @r
end

Instance Method Details

#*(other) ⇒ Object



44
# File 'lib/less/engine/nodes/literal.rb', line 44

def * other; operate :*, other end

#+(other) ⇒ Object



42
# File 'lib/less/engine/nodes/literal.rb', line 42

def + other; operate :+, other end

#-(other) ⇒ Object



43
# File 'lib/less/engine/nodes/literal.rb', line 43

def - other; operate :-, other end

#/(other) ⇒ Object



45
# File 'lib/less/engine/nodes/literal.rb', line 45

def / other; operate :/, other end

#alpha(v) ⇒ Object



25
26
27
# File 'lib/less/engine/nodes/literal.rb', line 25

def alpha v
  self.class.new r, g, b, v
end

#coerce(other) ⇒ Object



47
48
49
# File 'lib/less/engine/nodes/literal.rb', line 47

def coerce other
  return self, other
end

#inspectObject



59
60
61
62
63
64
65
# 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) ⇒ Object



33
34
35
36
37
38
39
40
# 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

#rgbObject



29
30
31
# File 'lib/less/engine/nodes/literal.rb', line 29

def rgb
  [r, g, b]
end

#to_cssObject



67
68
69
# File 'lib/less/engine/nodes/literal.rb', line 67

def to_css
  to_s
end

#to_rubyObject



71
72
73
# File 'lib/less/engine/nodes/literal.rb', line 71

def to_ruby
  "#{self.class}.new(#{r},#{g},#{b},#{a})"
end

#to_sObject



51
52
53
54
55
56
57
# 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