Class: Zyps::Color

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/zyps.rb

Overview

An object’s color. Has red, green, and blue components, each ranging from 0 to 1.

  • Red: Color.new(1, 0, 0)

  • Green: Color.new(0, 1, 0)

  • Blue: Color.new(0, 0, 1)

  • White: Color.new(1, 1, 1)

  • Black: Color.new(0, 0, 0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red = 1, green = 1, blue = 1) ⇒ Color

Returns a new instance of Color.



408
409
410
# File 'lib/zyps.rb', line 408

def initialize (red = 1, green = 1, blue = 1)
	self.red, self.green, self.blue = red, green, blue
end

Instance Attribute Details

#blueObject

Components which range from 0 to 1, which combine to form the Color.



406
407
408
# File 'lib/zyps.rb', line 406

def blue
  @blue
end

#greenObject

Components which range from 0 to 1, which combine to form the Color.



406
407
408
# File 'lib/zyps.rb', line 406

def green
  @green
end

#redObject

Components which range from 0 to 1, which combine to form the Color.



406
407
408
# File 'lib/zyps.rb', line 406

def red
  @red
end

Instance Method Details

#+(color2) ⇒ Object

Averages each component of this Color with the corresponding component of color2, returning a new Color.



429
430
431
432
433
434
435
# File 'lib/zyps.rb', line 429

def +(color2)
	Color.new(
		(self.red + color2.red) / 2.0,
		(self.green + color2.green) / 2.0,
		(self.blue + color2.blue) / 2.0
	)
end

#<=>(other) ⇒ Object

Compares this Color with another to see which is brighter. The sum of all components (red + green + blue) for each color determines which is greater.



424
425
426
# File 'lib/zyps.rb', line 424

def <=>(other)
	@red + @green + @blue <=> other.red + other.green + other.blue
end

#copyObject

Make a deep copy.



413
# File 'lib/zyps.rb', line 413

def copy; self.clone; end