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.



369
370
371
# File 'lib/zyps.rb', line 369

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.



367
368
369
# File 'lib/zyps.rb', line 367

def blue
  @blue
end

#greenObject

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



367
368
369
# File 'lib/zyps.rb', line 367

def green
  @green
end

#redObject

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



367
368
369
# File 'lib/zyps.rb', line 367

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.



390
391
392
393
394
395
396
# File 'lib/zyps.rb', line 390

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.



385
386
387
# File 'lib/zyps.rb', line 385

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

#copyObject

Make a deep copy.



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

def copy; self.clone; end