Class: Less::Node::Color
- Inherits:
-
Object
- Object
- Less::Node::Color
- Includes:
- Literal
- Defined in:
- lib/less/engine/nodes/literal.rb
Overview
rgb(255, 0, 0) #f0f0f0
Instance Attribute Summary collapse
-
#a ⇒ Object
readonly
Returns the value of attribute a.
-
#b ⇒ Object
readonly
Returns the value of attribute b.
-
#g ⇒ Object
readonly
Returns the value of attribute g.
-
#r ⇒ Object
readonly
Returns the value of attribute r.
Attributes included from Entity
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #alpha(v) ⇒ Object
- #coerce(other) ⇒ Object
-
#initialize(r, g, b, a = 1.0) ⇒ Color
constructor
A new instance of Color.
- #inspect ⇒ Object
- #operate(op, other) ⇒ Object
- #rgb ⇒ Object
- #to_css ⇒ Object
- #to_ruby ⇒ Object
- #to_s ⇒ Object
Methods included from Literal
Methods included from Entity
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
#a ⇒ Object (readonly)
Returns the value of attribute a.
16 17 18 |
# File 'lib/less/engine/nodes/literal.rb', line 16 def a @a end |
#b ⇒ Object (readonly)
Returns the value of attribute b.
16 17 18 |
# File 'lib/less/engine/nodes/literal.rb', line 16 def b @b end |
#g ⇒ Object (readonly)
Returns the value of attribute g.
16 17 18 |
# File 'lib/less/engine/nodes/literal.rb', line 16 def g @g end |
#r ⇒ Object (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 |
#inspect ⇒ Object
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 |
#rgb ⇒ Object
29 30 31 |
# File 'lib/less/engine/nodes/literal.rb', line 29 def rgb [r, g, b] end |
#to_css ⇒ Object
67 68 69 |
# File 'lib/less/engine/nodes/literal.rb', line 67 def to_css to_s end |
#to_ruby ⇒ Object
71 72 73 |
# File 'lib/less/engine/nodes/literal.rb', line 71 def to_ruby "#{self.class}.new(#{r},#{g},#{b},#{a})" end |
#to_s ⇒ Object
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 |