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.



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

#bObject

Access blue channel

Returns:

  • (Object)

    Value of blue channel.



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

def b
  @b
end

#gObject

Access green channel

Returns:

  • (Object)

    Value of green channel.



79
80
81
# File 'lib/multiarray/rgb.rb', line 79

def g
  @g
end

#rObject

Access red channel

Returns:

  • (Object)

    Value of red channel.



74
75
76
# File 'lib/multiarray/rgb.rb', line 74

def r
  @r
end

Class Method Details

.define_binary_op(op) ⇒ Object

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.



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.

Parameters:

  • op (Symbol, String)

    Operation to define channel-wise operation.



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.

Parameters:

  • value (Object)

    The other Ruby object.

Returns:

  • (Boolean)

    Returns false if Ruby object requires 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.

Returns:

  • (Node)

    An array with the three channel values as elements.



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

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)”).



100
101
102
# File 'lib/multiarray/rgb.rb', line 100

def inspect
  "RGB(#{@r.inspect},#{@g.inspect},#{@b.inspect})"
end

#nonzero?Boolean

Returns:

  • (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

Parameters:

  • value (Object)

    New value for this object.

Returns:



118
119
120
# File 'lib/multiarray/rgb.rb', line 118

def store( value )
  @r, @g, @b = value.r, value.g, value.b
end

#to_sString

Return string with information about this object.

Returns:

  • (String)

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



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

Returns:

  • (Boolean)


153
154
155
# File 'lib/multiarray/rgb.rb', line 153

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