Class: Hornetseye::RGB

Inherits:
Object show all
Defined in:
lib/multiarray/rgb.rb

Overview

Representation for colour pixel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, g, b) ⇒ RGB

Constructor

Create new RGB object.

Parameters:

  • r (Object)

    Red colour component.

  • g (Object)

    Green colour component.

  • b (Object)

    Blue colour component.



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

#bObject

Access blue channel

Returns:

  • (Object)

    Value of blue channel.



94
95
96
# File 'lib/multiarray/rgb.rb', line 94

def b
  @b
end

#gObject

Access green channel

Returns:

  • (Object)

    Value of green channel.



89
90
91
# File 'lib/multiarray/rgb.rb', line 89

def g
  @g
end

#rObject

Access red channel

Returns:

  • (Object)

    Value of red channel.



84
85
86
# File 'lib/multiarray/rgb.rb', line 84

def r
  @r
end

Class Method Details

.define_binary_op(op) ⇒ Proc

Defines a binary operation

This method uses meta-programming to define channel-wise binary operations.

Parameters:

  • op (Symbol, String)

    Operation to define channel-wise operation.

Returns:

  • (Proc)

    The new method.



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

.define_unary_op(op) ⇒ Proc

Defines a unary operation

This method uses meta-programming to define channel-wise unary operations.

Parameters:

  • op (Symbol, String)

    Operation to define channel-wise operation.

Returns:

  • (Proc)

    The new method.



49
50
51
52
53
# File 'lib/multiarray/rgb.rb', line 49

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.

Parameters:

  • value (Object)

    The other Ruby object.

Returns:

  • (Boolean)

    Returns false if Ruby object requires coercion.



36
37
38
# File 'lib/multiarray/rgb.rb', line 36

def generic?(value)
  value.is_a?(Numeric) or value.is_a?(GCCValue)
end

Instance Method Details

#+@RGB

This operation has no effect

Returns:

  • (RGB)

    Returns self.



150
151
152
# File 'lib/multiarray/rgb.rb', line 150

def +@
  self
end

#==(other) ⇒ Boolean

Test on equality

Parameters:

  • other (Object)

    Object to compare with.

Returns:

  • (Boolean)

    Returns boolean indicating whether objects are equal or not.



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

Parameters:

  • value (Object)

    New value for this object.

Returns:



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

Parameters:

  • other (RGB)

    Other object.

Returns:



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.

Returns:

  • (Node)

    An array with the three channel values as elements.



217
218
219
# File 'lib/multiarray/rgb.rb', line 217

def decompose(i)
  [ @r, @g, @b ][ i ]
end

#inspectString

Return string with information about this object

Returns:

  • (String)

    Returns a string (e.g. “RGB(1,2,3)”).



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

Returns:



183
184
185
# File 'lib/multiarray/rgb.rb', line 183

def nonzero?
  @r.nonzero?.or( @g.nonzero? ).or( @b.nonzero? )
end

#swap_rgbRGB

Swap colour channels

Returns:

  • (RGB)

    The result.



190
191
192
# File 'lib/multiarray/rgb.rb', line 190

def swap_rgb
  RGB.new @b, @g, @r
end

#to_sString

Return string with information about this object

Returns:

  • (String)

    Returns a string (e.g. “RGB(1,2,3)”).



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

Returns:



176
177
178
# File 'lib/multiarray/rgb.rb', line 176

def zero?
  @r.zero?.and( @g.zero? ).and( @b.zero? )
end