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
# File 'lib/less/engine/nodes/literal.rb', line 18

def initialize(r, g, b, a = 1.0)
  r, g, b = [r, g, b].map {|c| c.is_a?(::String) ? c.to_i(16) : c }
  @r, @g, @b, @a = normalize(r), normalize(g), normalize(b), 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



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

def * other
  operate :*, other
end

#+(other) ⇒ Object



39
40
41
# File 'lib/less/engine/nodes/literal.rb', line 39

def + other
  operate :+, other
end

#-(other) ⇒ Object



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

def - other
  operate :-, other
end

#/(other) ⇒ Object



51
52
53
# File 'lib/less/engine/nodes/literal.rb', line 51

def / other
  operate :/, other
end

#alpha(v) ⇒ Object



23
24
25
# File 'lib/less/engine/nodes/literal.rb', line 23

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

#coerce(other) ⇒ Object



55
56
57
# File 'lib/less/engine/nodes/literal.rb', line 55

def coerce other
  return self, other
end

#inspectObject



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

def inspect
  if a < 1.0
    "rgba(#{r}, #{g}, #{b}, #{a})"
  else
    "rgb(#{r}, #{g}, #{b})"
  end
end

#operate(op, other) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/less/engine/nodes/literal.rb', line 31

def operate op, other
  if other.is_a? Numeric
    self.class.new *self.rgb.map {|c| c.send(op, other) }, @a
  else
    self.class.new *self.rgb.zip(other.rgb).map {|a, b| a.send(op, b) }, @a
  end
end

#rgbObject



27
28
29
# File 'lib/less/engine/nodes/literal.rb', line 27

def rgb
  [r, g, b]
end

#to_cssObject



75
76
77
# File 'lib/less/engine/nodes/literal.rb', line 75

def to_css
  to_s
end

#to_rubyObject



79
80
81
# File 'lib/less/engine/nodes/literal.rb', line 79

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

#to_sObject



59
60
61
62
63
64
65
# File 'lib/less/engine/nodes/literal.rb', line 59

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