Class: Rubygame::Color::ColorRGB

Inherits:
Object
  • Object
show all
Includes:
ColorBase
Defined in:
lib/rubygame/color/models/rgb.rb

Overview

Represents color in the RGB (Red, Green, Blue) color space.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ColorBase

#*, #+, #-, #/, #average, #over

Constructor Details

#initialize(color) ⇒ ColorRGB

call-seq:

new( [r,g,b,a] )  ->  ColorRGB
new( [r,g,b] )  ->  ColorRGB
new( color )  ->  ColorRGB

Create a new instance from an Array or an existing color (of any type). If the alpha (opacity) component is omitted from the array, full opacity will be used.

All color components range from 0.0 to 1.0.



42
43
44
45
46
47
48
49
# File 'lib/rubygame/color/models/rgb.rb', line 42

def initialize( color )
	if color.kind_of?(Array)
		@r, @g, @b, @a = color.collect { |i| i.to_f }
		@a = 1.0 unless @a
	elsif color.respond_to?(:to_rgba_ary)
		@r, @g, @b, @a = color.to_rgba_ary
	end
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



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

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



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

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g.



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

def g
  @g
end

#rObject (readonly)

Returns the value of attribute r.



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

def r
  @r
end

Class Method Details

.new_from_rgba(rgba) ⇒ Object



67
68
69
# File 'lib/rubygame/color/models/rgb.rb', line 67

def new_from_rgba( rgba )
	new( rgba )
end

.new_from_sdl_rgba(rgba) ⇒ Object



71
72
73
# File 'lib/rubygame/color/models/rgb.rb', line 71

def new_from_sdl_rgba( rgba )
	new_from_rgba( rgba.collect { |i| i / 255.0 } )
end

Instance Method Details

#to_rgba_aryObject



57
58
59
# File 'lib/rubygame/color/models/rgb.rb', line 57

def to_rgba_ary
	return [@r, @g, @b, @a]
end

#to_sObject Also known as: inspect



61
62
63
# File 'lib/rubygame/color/models/rgb.rb', line 61

def to_s
	"#<#{self.class} [#{@r}, #{@g}, #{@b}, #{@a}]>"
end

#to_sdl_rgba_aryObject

Converts the color to an RGBA array of integers ranging from 0 to 255, as SDL wants.



53
54
55
# File 'lib/rubygame/color/models/rgb.rb', line 53

def to_sdl_rgba_ary
	self.to_rgba_ary.collect { |i| (i * 255).to_i }
end