Class: Gosu::Color

Inherits:
Object show all
Defined in:
lib/fidgit/gosu_ext/color.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.argb(alpha, red, green, blue) ⇒ Color

ARGB in 0..255 format (equivalent to Color.new, but explicit)

Parameters:

  • alpha (Integer)
  • red (Integer)
  • green (Integer)
  • blue (Integer)

Returns:


34
35
36
# File 'lib/fidgit/gosu_ext/color.rb', line 34

def self.argb(alpha, red, green, blue)
  new(alpha, red, green, blue)
end

.from_tex_play(color) ⇒ Color

Convert from an RGBA array, as used by TexPlay.

Parameters:

  • color (Array<Float>)

    TexPlay color [r, g, b, a] in range 0.0..1.0

Returns:


67
68
69
# File 'lib/fidgit/gosu_ext/color.rb', line 67

def self.from_tex_play(color)
  rgba(*color.map {|c| (c * 255).to_i })
end

.hsv(hue, saturation, value) ⇒ Color

HSV format (alpha assumed to be 255)

Parameters:

  • hue (Float)

    0.0..360.0

  • saturation (Float)

    0.0..1.0

  • value (Float)

    0.0..1.0

Returns:


44
45
46
# File 'lib/fidgit/gosu_ext/color.rb', line 44

def self.hsv(hue, saturation, value)
  from_hsv(hue, saturation, value)
end

.hsva(hue, saturation, value, alpha) ⇒ Color

HSVA format

Parameters:

  • hue (Float)

    0.0..360.0

  • saturation (Float)

    0.0..1.0

  • value (Float)

    0.0..1.0

  • alpha (Integer)

    1..255

Returns:


55
56
57
# File 'lib/fidgit/gosu_ext/color.rb', line 55

def self.hsva(hue, saturation, value, alpha)
  from_ahsv(alpha, hue, saturation, value)
end

.rgb(red, green, blue) ⇒ Color

RGB in 0..255 format (Alpha assumed 255)

Parameters:

  • red (Integer)
  • green (Integer)
  • blue (Integer)

Returns:


14
15
16
# File 'lib/fidgit/gosu_ext/color.rb', line 14

def self.rgb(red, green, blue)
  new(255, red, green, blue)
end

.rgba(red, green, blue, alpha) ⇒ Color

RGBA in 0..255 format

Parameters:

  • red (Integer)
  • green (Integer)
  • blue (Integer)
  • alpha (Integer)

Returns:


25
26
27
# File 'lib/fidgit/gosu_ext/color.rb', line 25

def self.rgba(red, green, blue, alpha)
  new(alpha, red, green, blue)
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (ArgumentError)

100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fidgit/gosu_ext/color.rb', line 100

def +(other)
  raise ArgumentError, "Can only add another #{self.class}" unless other.is_a? Color

  copy = Color.new(0)

  copy.red = [red + other.red, 255].min
  copy.green = [green + other.green, 255].min
  copy.blue = [blue + other.blue, 255].min
  copy.alpha = [alpha + other.alpha, 255].min

  copy
end

#-(other) ⇒ Object

Raises:

  • (ArgumentError)

113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fidgit/gosu_ext/color.rb', line 113

def -(other)
  raise ArgumentError, "Can only take away another #{self.class}" unless other.is_a? Color

  copy = Color.new(0)

  copy.red = [red - other.red, 0].max
  copy.green = [green - other.green, 0].max
  copy.blue = [blue - other.blue, 0].max
  copy.alpha = [alpha - other.alpha, 0].max

  copy
end

#==(other) ⇒ Object


126
127
128
129
130
131
132
# File 'lib/fidgit/gosu_ext/color.rb', line 126

def ==(other)
  if other.is_a? Color
    red == other.red and green == other.green and blue == other.blue and alpha == other.alpha
  else
    false
  end
end

#colorize(text) ⇒ Object

Colorize text for in-line rendering by Gosu. e.g. “frog” => “<c=00ff9a>frog</c>”


87
88
89
# File 'lib/fidgit/gosu_ext/color.rb', line 87

def colorize(text)
  "<c=#{to_hex}>#{text}</c>"
end

#opaque?Boolean

Is the color completely opaque?

Returns:

  • (Boolean)

6
# File 'lib/fidgit/gosu_ext/color.rb', line 6

def opaque?; alpha == 255; end

#to_hexString

Convert to a 6-digit hexadecimal value, appropriate for passing to the Gosu <c> tag. The alpha channel is ignored.

Returns:

  • (String)

    RGB hexadecimal string, such as “AABB00”


81
82
83
# File 'lib/fidgit/gosu_ext/color.rb', line 81

def to_hex
  "%02x%02x%02x" % [red, green, blue]
end

#to_sObject

Show the Color as <RGBA [0, 0, 0, 0]> or, if opaque, <RGB [0, 0, 0]> (Gosu default is '(ARGB:0/0/0/0)')


92
93
94
95
96
97
98
# File 'lib/fidgit/gosu_ext/color.rb', line 92

def to_s
  if opaque?
    "<RGB [#{red}, #{green}, #{blue}]>"
  else
    "<RGBA [#{red}, #{green}, #{blue}, #{alpha}]>"
  end
end

#to_tex_playArray<Float>

Convert to an RGBA array, as used by TexPlay.

Returns:

  • (Array<Float>)

    TexPlay color array [r, g, b, a] in range 0.0..1.0


74
75
76
# File 'lib/fidgit/gosu_ext/color.rb', line 74

def to_tex_play
  [red / 255.0, green / 255.0, blue / 255.0,  alpha / 255.0]
end

#transparent?Boolean

Is the color completely transparent?

Returns:

  • (Boolean)

4
# File 'lib/fidgit/gosu_ext/color.rb', line 4

def transparent?; alpha == 0; end