Class: Rubytracer::Colour

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytracer/colour.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, g, b) ⇒ Colour

Returns a new instance of Colour.



5
6
7
8
9
# File 'lib/rubytracer/colour.rb', line 5

def initialize(r,g,b)
  @r = r
  @g = g
  @b = b
end

Instance Attribute Details

#bObject (readonly)

Returns the value of attribute b.



3
4
5
# File 'lib/rubytracer/colour.rb', line 3

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g.



3
4
5
# File 'lib/rubytracer/colour.rb', line 3

def g
  @g
end

#rObject (readonly)

Returns the value of attribute r.



3
4
5
# File 'lib/rubytracer/colour.rb', line 3

def r
  @r
end

Instance Method Details

#*(other) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rubytracer/colour.rb', line 19

def * other
  case other
  when Colour
    Colour.new(@r * other.r, @g * other.g, @b * other.b)
  else
    Colour.new(@r * other, @g * other, @b * other)
  end
end

#**(power) ⇒ Object



28
29
30
# File 'lib/rubytracer/colour.rb', line 28

def ** power
  Colour.new(@r ** power, @g ** power, @b ** power)
end

#+(other) ⇒ Object



15
16
17
# File 'lib/rubytracer/colour.rb', line 15

def + other
  Colour.new(@r + other.r, @g + other.g, @b + other.b)
end

#+@Object



36
37
38
# File 'lib/rubytracer/colour.rb', line 36

def +@
  self
end

#-(other) ⇒ Object



11
12
13
# File 'lib/rubytracer/colour.rb', line 11

def - other
  Colour.new(@r - other.r, @g - other.g, @b - other.b)
end

#-@Object



40
41
42
# File 'lib/rubytracer/colour.rb', line 40

def -@
  Colour.new(-@r, -@g, -@b)
end

#/(scale) ⇒ Object



32
33
34
# File 'lib/rubytracer/colour.rb', line 32

def / scale
  self * (1.0/scale)
end

#to_intObject



44
45
46
# File 'lib/rubytracer/colour.rb', line 44

def to_int
  [[[0, @r * 256].max, 255].min, [[0, @g * 256].max, 255].min, [[0, @b * 256].max, 255].min]
end