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) ⇒ Proc
Defines a binary operation.
-
.define_unary_op(op) ⇒ Proc
Defines a unary operation.
-
.generic?(value) ⇒ Boolean
Check compatibility of other type.
Instance Method Summary collapse
-
#+@ ⇒ RGB
This operation has no effect.
-
#==(other) ⇒ Boolean
Test on equality.
-
#assign(value) ⇒ Object
Store new value in this RGB object.
-
#coerce(other) ⇒ Array<RGB>
Coerce with 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, GCCValue
Check whether value is not equal to zero.
-
#swap_rgb ⇒ RGB
Swap colour channels.
-
#to_s ⇒ String
Return string with information about this object.
-
#zero? ⇒ Boolean, GCCValue
Check whether value is equal to zero.
Constructor Details
#initialize(r, g, b) ⇒ RGB
Constructor
Create new RGB object.
103 104 105 |
# File 'lib/multiarray/rgb.rb', line 103 def initialize( r, g, b ) @r, @g, @b = r, g, b end |
Instance Attribute Details
Class Method Details
.define_binary_op(op) ⇒ Proc
Defines a binary operation
This method uses meta-programming to define channel-wise binary operations.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/multiarray/rgb.rb', line 64 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 |
Instance Method Details
#+@ ⇒ RGB
This operation has no effect
150 151 152 |
# File 'lib/multiarray/rgb.rb', line 150 def +@ self end |
#==(other) ⇒ Boolean
Test on equality
200 201 202 203 204 205 206 207 208 |
# File 'lib/multiarray/rgb.rb', line 200 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 |
#assign(value) ⇒ Object
Store new value in this RGB object
128 129 130 |
# File 'lib/multiarray/rgb.rb', line 128 def assign(value) @r, @g, @b = value.r, value.g, value.b end |
#coerce(other) ⇒ Array<RGB>
Coerce with other object
139 140 141 142 143 144 145 |
# File 'lib/multiarray/rgb.rb', line 139 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.
217 218 219 |
# File 'lib/multiarray/rgb.rb', line 217 def decompose(i) [ @r, @g, @b ][ i ] end |
#inspect ⇒ String
Return string with information about this object
110 111 112 |
# File 'lib/multiarray/rgb.rb', line 110 def inspect "RGB(#{@r.inspect},#{@g.inspect},#{@b.inspect})" end |
#nonzero? ⇒ Boolean, GCCValue
Check whether value is not equal to zero
183 184 185 |
# File 'lib/multiarray/rgb.rb', line 183 def nonzero? @r.nonzero?.or( @g.nonzero? ).or( @b.nonzero? ) end |
#swap_rgb ⇒ RGB
Swap colour channels
190 191 192 |
# File 'lib/multiarray/rgb.rb', line 190 def swap_rgb RGB.new @b, @g, @r end |
#to_s ⇒ String
Return string with information about this object
117 118 119 |
# File 'lib/multiarray/rgb.rb', line 117 def to_s "RGB(#{@r.to_s},#{@g.to_s},#{@b.to_s})" end |
#zero? ⇒ Boolean, GCCValue
Check whether value is equal to zero
176 177 178 |
# File 'lib/multiarray/rgb.rb', line 176 def zero? @r.zero?.and( @g.zero? ).and( @b.zero? ) end |