Class: 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 |
# 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
#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
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 |
#inspect ⇒ Object
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 |
#rgb ⇒ Object
27 28 29 |
# File 'lib/less/engine/nodes/literal.rb', line 27 def rgb [r, g, b] end |
#to_css ⇒ Object
75 76 77 |
# File 'lib/less/engine/nodes/literal.rb', line 75 def to_css to_s end |
#to_ruby ⇒ Object
79 80 81 |
# File 'lib/less/engine/nodes/literal.rb', line 79 def to_ruby "Color.new(#{r},#{g},#{b},#{a})" end |
#to_s ⇒ Object
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 |