Class: Gosu::Color

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Color

Monkey-patching the initialize to allow for another Gosu::Color and Strings.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gosu_ext/color.rb', line 9

def initialize(*args)
  if args.length == 1
    value = args.first
    if value.is_a? Gosu::Color
      gosu_initialize value.alpha, value.red, value.green, value.blue
    elsif value.is_a? String
      gosu_initialize *Array(self.class.parse_string(value))
    else
      gosu_initialize value
    end
  else
    gosu_initialize *args
  end
end

Class Method Details

.parse_gosu_color_string(string) ⇒ Object



48
49
50
51
52
# File 'lib/gosu_ext/color.rb', line 48

def self.parse_gosu_color_string(string)
  if string =~ /\(ARGB: ([\d]{1,3})\/([\d]{1,3})\/([\d]{1,3})\/([\d]{1,3})\)/
    [ $1.to_i, $2.to_i, $3.to_i, $4.to_i ]
  end
end

.parse_hex(hex) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/gosu_ext/color.rb', line 40

def self.parse_hex(hex)
  if hex =~ /0x([A-Fa-f0-9]{8})/
    hex.to_i(16)
  elsif hex =~ /#([A-Fa-f0-9]{6})/
    "0xFF#{$1}".to_i(16)
  end
end

.parse_rgb(rgb) ⇒ Object



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

def self.parse_rgb(rgb)
  if rgb =~ /rgb\(([\d]{1,3}),([\d]{1,3}),([\d]{1,3})\)/
    [ 255, $1.to_i, $2.to_i, $3.to_i ]
  end
end

.parse_rgba(rgba) ⇒ Object



28
29
30
31
32
# File 'lib/gosu_ext/color.rb', line 28

def self.parse_rgba(rgba)
  if rgba =~ /rgba\(([\d]{1,3}(?:\.\d*)?),([\d]{1,3}(?:\.\d*)?),([\d]{1,3}(?:\.\d*)?),(\d(?:\.\d*)?)\)/
    [ (255 * $4.to_f).floor.to_i, $1.to_i, $2.to_i, $3.to_i ]
  end
end

.parse_string(value) ⇒ Object



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

def self.parse_string(value)
  parse_hex(value) || parse_rgb(value) || parse_rgba(value) || parse_gosu_color_string(value) || [ 255, 255, 255, 255 ]
end

Instance Method Details

#gosu_initializeObject



3
# File 'lib/gosu_ext/color.rb', line 3

alias_method :gosu_initialize, :initialize

#to_json(*params) ⇒ Object



58
59
60
# File 'lib/gosu_ext/color.rb', line 58

def to_json(*params)
  to_s.to_json
end

#to_sObject



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

def to_s
  "rgba(#{red},#{green},#{blue},#{alpha / 255.to_f})"
end