Class: Hornetseye::RGB
Overview
Representation for colour pixel
Instance Attribute Summary collapse
-
#b ⇒ Object
Access blue channel.
-
#g ⇒ Object
Access green channel.
-
#r ⇒ Object
Access red channel.
Class Method Summary collapse
-
.define_binary_op(op) ⇒ Object
Defines a binary operation.
-
.define_unary_op(op) ⇒ Object
Defines a unary operation.
-
.generic?(value) ⇒ Boolean
Check compatibility of other type.
Instance Method Summary collapse
- #+@ ⇒ Object
- #==(other) ⇒ Object
- #coerce(other) ⇒ Object
-
#decompose(i) ⇒ Node
Decompose RGB number.
-
#initialize(r, g, b) ⇒ RGB
constructor
Constructor.
-
#inspect ⇒ String
Return string with information about this object.
- #nonzero? ⇒ Boolean
-
#store(value) ⇒ Object
Store new value in this RGB object.
-
#to_s ⇒ String
Return string with information about this object.
- #zero? ⇒ Boolean
Constructor Details
#initialize(r, g, b) ⇒ RGB
Constructor
Create new RGB object.
93 94 95 |
# File 'lib/multiarray/rgb.rb', line 93 def initialize( r, g, b ) @r, @g, @b = r, g, b end |
Instance Attribute Details
Class Method Details
.define_binary_op(op) ⇒ Object
Defines a binary operation
This method uses meta-programming to define channel-wise binary operations.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/multiarray/rgb.rb', line 54 def define_binary_op( op ) define_method( op ) do |other| if other.is_a? RGB RGB.new r.send( op, other.r ), g.send( op, other.g ), b.send( op, other.b ) elsif RGB.generic? other RGB.new r.send( op, other ), g.send( op, other ), b.send( op, other ) else x, y = other.coerce self x.send op, y end end end |
.define_unary_op(op) ⇒ Object
Defines a unary operation
This method uses meta-programming to define channel-wise unary operations.
43 44 45 46 47 |
# File 'lib/multiarray/rgb.rb', line 43 def define_unary_op( op ) define_method( op ) do RGB.new r.send( op ), g.send( op ), b.send( op ) end end |
.generic?(value) ⇒ Boolean
Check compatibility of other type
This method checks whether binary operations with the other Ruby object can be performed without requiring coercion.
34 35 36 |
# File 'lib/multiarray/rgb.rb', line 34 def generic?( value ) value.is_a?( Numeric ) or value.is_a?( GCCValue ) end |
Instance Method Details
#+@ ⇒ Object
130 131 132 |
# File 'lib/multiarray/rgb.rb', line 130 def +@ self end |
#==(other) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/multiarray/rgb.rb', line 161 def ==( other ) if other.is_a? RGB @r.eq( other.r ).and( @g.eq( other.g ) ).and( @b.eq( other.b ) ) elsif RGB.generic? other @r.eq( other ).and( @g.eq( other ) ).and( @b.eq( other ) ) else false end end |
#coerce(other) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/multiarray/rgb.rb', line 122 def coerce( other ) if other.is_a? RGB return other, self else return RGB.new( other, other, other ), self end end |
#decompose(i) ⇒ Node
Decompose RGB number
This method decomposes the RGB value into an array.
176 177 178 |
# File 'lib/multiarray/rgb.rb', line 176 def decompose( i ) [ @r, @g, @b ][ i ] end |
#inspect ⇒ String
Return string with information about this object.
100 101 102 |
# File 'lib/multiarray/rgb.rb', line 100 def inspect "RGB(#{@r.inspect},#{@g.inspect},#{@b.inspect})" end |
#nonzero? ⇒ Boolean
157 158 159 |
# File 'lib/multiarray/rgb.rb', line 157 def nonzero? @r.nonzero?.or( @g.nonzero? ).or( @b.nonzero? ) end |
#store(value) ⇒ Object
Store new value in this RGB object
118 119 120 |
# File 'lib/multiarray/rgb.rb', line 118 def store( value ) @r, @g, @b = value.r, value.g, value.b end |
#to_s ⇒ String
Return string with information about this object.
107 108 109 |
# File 'lib/multiarray/rgb.rb', line 107 def to_s "RGB(#{@r.to_s},#{@g.to_s},#{@b.to_s})" end |
#zero? ⇒ Boolean
153 154 155 |
# File 'lib/multiarray/rgb.rb', line 153 def zero? @r.zero?.and( @g.zero? ).and( @b.zero? ) end |