Module: Rubygame::Color::ColorBase

Included in:
ColorHSL, ColorHSV, ColorRGB
Defined in:
lib/rubygame/color/models/base.rb

Overview

A mix-in module defining color arithmetic operations.

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

Perform color multiplication with another color of any type. The alpha of the new color will be equal to the alpha of the receiver.



43
44
45
# File 'lib/rubygame/color/models/base.rb', line 43

def *(other)
	wrap( simple_op(other)  { |a,b|  a * b } )
end

#+(other) ⇒ Object

Perform color addition with another color of any type. The alpha of the new color will be equal to the alpha of the receiver.



29
30
31
# File 'lib/rubygame/color/models/base.rb', line 29

def +(other)
	wrap( simple_op(other)  { |a,b| a + b } )
end

#-(other) ⇒ Object

Perform color subtraction with another color of any type. The alpha of the new color will be equal to the alpha of the receiver.



36
37
38
# File 'lib/rubygame/color/models/base.rb', line 36

def -(other)
	wrap( simple_op(other)  { |a,b|  a - b } )
end

#/(other) ⇒ Object

Perform color division with another color of any type. The alpha of the new color will be equal to the alpha of the receiver.



50
51
52
# File 'lib/rubygame/color/models/base.rb', line 50

def /(other)
	wrap( simple_op(other)  { |a,b|  a / b } )
end

#average(other, weight = 0.5) ⇒ Object

Average this color with another color. (Linear weighted average)

A weight of 0.0 means 0% of this color, 100% of the other. A weight of 1.0 means 100% of this color, 0% of the other. A weight of 0.5 means 50% of each color.



74
75
76
77
78
79
80
81
82
# File 'lib/rubygame/color/models/base.rb', line 74

def average(other, weight=0.5)
	c1, c2 = self.to_rgba_ary, other.to_rgba_ary

	rgba = [0,1,2,3].collect do |i| 
		clamp( c1.at(i)*weight + c2.at(i)*(1-weight) )
	end
	
	wrap( rgba )
end

#over(other) ⇒ Object

Layer this color over another color.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubygame/color/models/base.rb', line 55

def over(other)
	c1, c2 = self.to_rgba_ary, other.to_rgba_ary
	a1, a2 = c1[3], c2[3]

	rgba = [0,1,2].collect do |i| 
		clamp( a1*c1.at(i) + a2*c2.at(i)*(1-a1) )
	end
	
	rgba << ( a1 + a2*(1-a1) )
	
	wrap( rgba )
end