rubygame 2.2.0-i586-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. data/CREDITS +60 -0
  2. data/LICENSE +504 -0
  3. data/NEWS +201 -0
  4. data/README +139 -0
  5. data/ROADMAP +43 -0
  6. data/Rakefile +409 -0
  7. data/doc/extended_readme.rdoc +49 -0
  8. data/doc/getting_started.rdoc +47 -0
  9. data/doc/macosx_install.rdoc +70 -0
  10. data/doc/windows_install.rdoc +123 -0
  11. data/ext/rubygame/MANIFEST +25 -0
  12. data/ext/rubygame/rubygame_core.so +0 -0
  13. data/ext/rubygame/rubygame_event.c +644 -0
  14. data/ext/rubygame/rubygame_event.h +48 -0
  15. data/ext/rubygame/rubygame_event.o +0 -0
  16. data/ext/rubygame/rubygame_gfx.c +942 -0
  17. data/ext/rubygame/rubygame_gfx.h +101 -0
  18. data/ext/rubygame/rubygame_gfx.o +0 -0
  19. data/ext/rubygame/rubygame_gfx.so +0 -0
  20. data/ext/rubygame/rubygame_gl.c +154 -0
  21. data/ext/rubygame/rubygame_gl.h +32 -0
  22. data/ext/rubygame/rubygame_gl.o +0 -0
  23. data/ext/rubygame/rubygame_image.c +108 -0
  24. data/ext/rubygame/rubygame_image.h +41 -0
  25. data/ext/rubygame/rubygame_image.o +0 -0
  26. data/ext/rubygame/rubygame_image.so +0 -0
  27. data/ext/rubygame/rubygame_joystick.c +247 -0
  28. data/ext/rubygame/rubygame_joystick.h +41 -0
  29. data/ext/rubygame/rubygame_joystick.o +0 -0
  30. data/ext/rubygame/rubygame_main.c +155 -0
  31. data/ext/rubygame/rubygame_main.h +33 -0
  32. data/ext/rubygame/rubygame_main.o +0 -0
  33. data/ext/rubygame/rubygame_mixer.c +764 -0
  34. data/ext/rubygame/rubygame_mixer.h +62 -0
  35. data/ext/rubygame/rubygame_mixer.o +0 -0
  36. data/ext/rubygame/rubygame_mixer.so +0 -0
  37. data/ext/rubygame/rubygame_screen.c +448 -0
  38. data/ext/rubygame/rubygame_screen.h +43 -0
  39. data/ext/rubygame/rubygame_screen.o +0 -0
  40. data/ext/rubygame/rubygame_shared.c +209 -0
  41. data/ext/rubygame/rubygame_shared.h +60 -0
  42. data/ext/rubygame/rubygame_shared.o +0 -0
  43. data/ext/rubygame/rubygame_surface.c +1147 -0
  44. data/ext/rubygame/rubygame_surface.h +62 -0
  45. data/ext/rubygame/rubygame_surface.o +0 -0
  46. data/ext/rubygame/rubygame_time.c +183 -0
  47. data/ext/rubygame/rubygame_time.h +32 -0
  48. data/ext/rubygame/rubygame_time.o +0 -0
  49. data/ext/rubygame/rubygame_ttf.c +599 -0
  50. data/ext/rubygame/rubygame_ttf.h +69 -0
  51. data/ext/rubygame/rubygame_ttf.o +0 -0
  52. data/ext/rubygame/rubygame_ttf.so +0 -0
  53. data/lib/rubygame/MANIFEST +12 -0
  54. data/lib/rubygame/clock.rb +128 -0
  55. data/lib/rubygame/color/models/base.rb +106 -0
  56. data/lib/rubygame/color/models/hsl.rb +153 -0
  57. data/lib/rubygame/color/models/hsv.rb +149 -0
  58. data/lib/rubygame/color/models/rgb.rb +78 -0
  59. data/lib/rubygame/color/palettes/css.rb +49 -0
  60. data/lib/rubygame/color/palettes/palette.rb +100 -0
  61. data/lib/rubygame/color/palettes/x11.rb +177 -0
  62. data/lib/rubygame/color.rb +79 -0
  63. data/lib/rubygame/constants.rb +238 -0
  64. data/lib/rubygame/event.rb +313 -0
  65. data/lib/rubygame/ftor.rb +370 -0
  66. data/lib/rubygame/hotspot.rb +265 -0
  67. data/lib/rubygame/keyconstants.rb +237 -0
  68. data/lib/rubygame/mediabag.rb +94 -0
  69. data/lib/rubygame/queue.rb +288 -0
  70. data/lib/rubygame/rect.rb +612 -0
  71. data/lib/rubygame/sfont.rb +223 -0
  72. data/lib/rubygame/sprite.rb +511 -0
  73. data/lib/rubygame.rb +41 -0
  74. data/samples/FreeSans.ttf +0 -0
  75. data/samples/GPL.txt +340 -0
  76. data/samples/README +40 -0
  77. data/samples/chimp.bmp +0 -0
  78. data/samples/chimp.rb +313 -0
  79. data/samples/demo_gl.rb +151 -0
  80. data/samples/demo_gl_tex.rb +197 -0
  81. data/samples/demo_music.rb +75 -0
  82. data/samples/demo_rubygame.rb +284 -0
  83. data/samples/demo_sfont.rb +52 -0
  84. data/samples/demo_ttf.rb +193 -0
  85. data/samples/demo_utf8.rb +53 -0
  86. data/samples/fist.bmp +0 -0
  87. data/samples/load_and_blit.rb +22 -0
  88. data/samples/panda.png +0 -0
  89. data/samples/punch.wav +0 -0
  90. data/samples/ruby.png +0 -0
  91. data/samples/song.ogg +0 -0
  92. data/samples/term16.png +0 -0
  93. data/samples/whiff.wav +0 -0
  94. metadata +152 -0
@@ -0,0 +1,128 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2004-2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ module Rubygame
21
+ # Clock provides class methods for tracking running time and delaying
22
+ # execution of the program for specified time periods. This is used to
23
+ # provide a consistent framerate, prevent the program from using
24
+ # all the processor time, etc.
25
+ #
26
+ # Clock also provides instance methods to make it convenient to
27
+ # monitor and limit application framerate. See #tick.
28
+ class Clock
29
+ # The runtime when the Clock was initialized.
30
+ attr_reader :start
31
+ # The number of times #tick has been called.
32
+ attr_reader :ticks
33
+
34
+ # Create a new Clock instance.
35
+ def initialize()
36
+ @start = Clock.runtime()
37
+ @last_tick = @start
38
+ @ticks = 0
39
+ @target_frametime = nil
40
+ yield self if block_given?
41
+ end
42
+
43
+ # The target frametime (milliseconds/frame). See #tick
44
+ attr_accessor :target_frametime
45
+
46
+ # Returns the current target framerate (frames/second).
47
+ # This is an alternate way to access @target_frametime.
48
+ # Same as: 1000.0 / #target_frametime
49
+ def target_framerate
50
+ if @target_frametime
51
+ 1000.0 / @target_frametime
52
+ else
53
+ nil
54
+ end
55
+ rescue ZeroDivisionError
56
+ return nil
57
+ end
58
+
59
+ # Sets the target number of frames per second to +framerate+.
60
+ # This is an alternate way to access @target_frametime.
61
+ # Same as: #target_frametime = 1000.0 / framerate
62
+ def target_framerate=( framerate )
63
+ if framerate
64
+ @target_frametime = 1000.0 / framerate
65
+ else
66
+ @target_frametime = nil
67
+ end
68
+ rescue ZeroDivisionError
69
+ @target_frametime = nil
70
+ end
71
+
72
+ # call-seq: lifetime() -> Numeric
73
+ #
74
+ # Returns time in milliseconds since this Clock instance was created.
75
+ def lifetime
76
+ Clock.runtime() - @start
77
+ end
78
+
79
+ # call-seq: framerate() -> Numeric
80
+ #
81
+ # Return the actual framerate (frames per second) recorded by the Clock.
82
+ # See #tick.
83
+ #
84
+ # TODO: sample only a few seconds in the past, instead of the
85
+ # entire lifetime of the Clock.
86
+ def framerate
87
+ # below is same as: return @ticks / (lifetime / 1000.0)
88
+ return 1000.0 * @ticks / lifetime()
89
+ rescue ZeroDivisionError
90
+ return 0
91
+ end
92
+
93
+ # Returns the number of milliseconds since you last called this method.
94
+ #
95
+ # You must call this method once per frame (i.e. per iteration of
96
+ # your main loop) if you want to use the framerate monitoring and/or
97
+ # framerate limiting features.
98
+ #
99
+ # Framerate monitoring allows you to check the framerate (frames per
100
+ # second) with the #framerate method.
101
+ #
102
+ # Framerate limiting allows you to prevent the application from running
103
+ # too fast (and using 100% of processor time) by pausing the program
104
+ # very briefly each frame. The pause duration is calculated each frame
105
+ # to maintain a constant framerate.
106
+ #
107
+ # Framerate limiting is only enabled if you have set the
108
+ # #target_framerate= or #target_frametime=.
109
+ # If you have done that, this method will automatically perform the
110
+ # delay each time you call it.
111
+ #
112
+ # (Please note that no effort is made to correct a framerate
113
+ # which is *slower* than the target framerate. Clock can't
114
+ # make your code run faster, only slow it down if it is
115
+ # running too fast.)
116
+ def tick()
117
+ passed = Clock.runtime() - @last_tick # how long since the last tick?
118
+ if @target_frametime
119
+ return Clock.delay(@target_frametime - passed) + passed
120
+ end
121
+ return passed
122
+ ensure
123
+ @last_tick = Clock.runtime()
124
+ @ticks += 1
125
+ end
126
+
127
+ end # class Clock
128
+ end #module Rubygame
@@ -0,0 +1,106 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+
21
+ # A mix-in module defining color arithmetic operations.
22
+ module ColorBase
23
+
24
+ # Perform color addition with another color of any type.
25
+ # The alpha of the new color will be equal to the alpha
26
+ # of the receiver.
27
+ def +(other)
28
+ wrap( simple_op(other) { |a,b| a + b } )
29
+ end
30
+
31
+ # Perform color subtraction with another color of any type.
32
+ # The alpha of the new color will be equal to the alpha
33
+ # of the receiver.
34
+ def -(other)
35
+ wrap( simple_op(other) { |a,b| a - b } )
36
+ end
37
+
38
+ # Perform color multiplication with another color of any type.
39
+ # The alpha of the new color will be equal to the alpha
40
+ # of the receiver.
41
+ def *(other)
42
+ wrap( simple_op(other) { |a,b| a * b } )
43
+ end
44
+
45
+ # Perform color division with another color of any type.
46
+ # The alpha of the new color will be equal to the alpha
47
+ # of the receiver.
48
+ def /(other)
49
+ wrap( simple_op(other) { |a,b| a / b } )
50
+ end
51
+
52
+ # Layer this color over another color.
53
+ def over(other)
54
+ c1, c2 = self.to_rgba_ary, other.to_rgba_ary
55
+ a1, a2 = c1[3], c2[3]
56
+
57
+ rgba = [0,1,2].collect do |i|
58
+ clamp( a1*c1.at(i) + a2*c2.at(i)*(1-a1) )
59
+ end
60
+
61
+ rgba << ( a1 + a2*(1-a1) )
62
+
63
+ wrap( rgba )
64
+ end
65
+
66
+ # Average this color with another color. (Linear weighted average)
67
+ #
68
+ # A weight of 0.0 means 0% of this color, 100% of the other.
69
+ # A weight of 1.0 means 100% of this color, 0% of the other.
70
+ # A weight of 0.5 means 50% of each color.
71
+ #
72
+ def average(other, weight=0.5)
73
+ c1, c2 = self.to_rgba_ary, other.to_rgba_ary
74
+
75
+ rgba = [0,1,2,3].collect do |i|
76
+ clamp( c1.at(i)*weight + c2.at(i)*(1-weight) )
77
+ end
78
+
79
+ wrap( rgba )
80
+ end
81
+
82
+ private
83
+
84
+ def wrap( rgba )
85
+ self.class.new_from_rgba( rgba )
86
+ end
87
+
88
+ def simple_op(other, &block)
89
+ c1, c2 = self.to_rgba_ary, other.to_rgba_ary
90
+ a1, a2 = c1[3], c2[3]
91
+
92
+ rgba = [0,1,2].collect do |i|
93
+ clamp( block.call( a1*c1.at(i), a2*c2.at(i) ) )
94
+ end
95
+
96
+ rgba << a1
97
+
98
+ return rgba
99
+ end
100
+
101
+ def clamp(v, min=0.0, max=1.0)
102
+ v = min if v < min
103
+ v = max if v > max
104
+ return v
105
+ end
106
+ end
@@ -0,0 +1,153 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ require 'rubygame/color/models/base'
21
+
22
+ module Rubygame
23
+ module Color
24
+
25
+ # Represents color in the HSL (Hue, Saturation, Luminosity) color space.
26
+ class ColorHSL
27
+ include ColorBase
28
+
29
+ attr_reader :h, :s, :l, :a
30
+
31
+ # call-seq:
32
+ # new( [h,s,l,a] ) -> ColorHSL
33
+ # new( [h,s,l] ) -> ColorHSL
34
+ # new( color ) -> ColorHSL
35
+ #
36
+ # Create a new instance from an Array or an existing color
37
+ # (of any type). If the alpha (opacity) component is omitted
38
+ # from the array, full opacity will be used.
39
+ #
40
+ # All color components range from 0.0 to 1.0.
41
+ #
42
+ def initialize( color )
43
+ if color.kind_of?(Array)
44
+ @h, @s, @l, @a = color.collect { |i| i.to_f }
45
+ @a = 1.0 unless @a
46
+ elsif color.respond_to?(:to_rgba_ary)
47
+ @h, @s, @l, @a = self.class.rgba_to_hsla( *color.to_rgba_ary )
48
+ end
49
+ end
50
+
51
+ # Return an Array with the red, green, blue, and alpha components
52
+ # of the color (converting the color to the RGBA model first).
53
+ def to_rgba_ary
54
+ return self.class.hsla_to_rgba( @h, @s, @l, @a )
55
+ end
56
+
57
+ def to_s
58
+ "#<#{self.class} [#{@h}, #{@s}, #{@l}, #{@a}]>"
59
+ end
60
+ alias :inspect :to_s
61
+
62
+ class << self
63
+
64
+ def new_from_rgba( rgba )
65
+ new( rgba_to_hsla(*rgba) )
66
+ end
67
+
68
+ def new_from_sdl_rgba( rgba )
69
+ new_from_rgba( rgba.collect { |i| i / 255.0 } )
70
+ end
71
+
72
+ # Convert the red, green, blue, and alpha to the
73
+ # equivalent hue, saturation, luminosity, and alpha.
74
+ def rgba_to_hsla( r, g, b, a ) # :nodoc:
75
+ rgb_arr = [r, g, b]
76
+ max = rgb_arr.max
77
+ min = rgb_arr.min
78
+
79
+ # Calculate lightness.
80
+ l = (max + min) / 2.0
81
+
82
+ # Calculate saturation.
83
+ if l == 0.0 or max == min
84
+ s = 0
85
+ elsif 0 < l and l <= 0.5
86
+ s = (max - min) / (max + min)
87
+ else # l > 0.5
88
+ s = (max - min) / (2 - (max + min))
89
+ end
90
+
91
+ # Calculate hue.
92
+ if min == max
93
+ h = 0
94
+ # Undefined in this case, but set it to zero
95
+ elsif max == r and g >= b
96
+ h = (1.quo(6) * (g - b) / (max - min)) + 0
97
+ elsif max == r and g < b
98
+ h = (1.quo(6) * (g - b) / (max - min)) + 1.0
99
+ elsif max == g
100
+ h = (1.quo(6) * (b - r) / (max - min)) + 1.quo(3)
101
+ elsif max == b
102
+ h = (1.quo(6) * (r - g) / (max - min)) + 2.quo(3)
103
+ else
104
+ raise "Should never happen"
105
+ end
106
+
107
+ return [h,s,l,a]
108
+ end
109
+
110
+ # Convert the hue, saturation, luminosity, and alpha
111
+ # to the equivalent red, green, blue, and alpha.
112
+ def hsla_to_rgba( h, s, l, a ) # :nodoc:
113
+ # If the color is achromatic, return already with the lightness value for all components
114
+ if s == 0.0
115
+ return [l, l, l, a]
116
+ end
117
+
118
+ # Otherwise, we have to do the long, hard calculation
119
+
120
+ # q helper value
121
+ q = (l < 0.5) ? (l * (1.0 + s)) : (l + s - l * s)
122
+
123
+ # p helper value
124
+ p = (2.0 * l) - q
125
+
126
+ r = calculate_component( p, q, h + 1.quo(3) )
127
+ g = calculate_component( p, q, h )
128
+ b = calculate_component( p, q, h - 1.quo(3) )
129
+
130
+ return [r,g,b,a]
131
+ end
132
+
133
+ private
134
+
135
+ # Perform some arcane math to calculate a color component.
136
+ def calculate_component(p, q, tc) # :nodoc:
137
+ tc %= 1.0
138
+ if tc < 1.quo(6)
139
+ p + (q - p) * tc * 6.0
140
+ elsif tc < 0.5
141
+ q
142
+ elsif tc < 2.quo(3)
143
+ p + (q - p) * (2.quo(3) - tc) * 6.0
144
+ else
145
+ p
146
+ end
147
+ end
148
+
149
+ end
150
+
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,149 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ require 'rubygame/color/models/base'
21
+
22
+ module Rubygame
23
+ module Color
24
+
25
+ # Represents color in the HSV (Hue, Saturation, Value) color space.
26
+ class ColorHSV
27
+ include ColorBase
28
+
29
+ attr_reader :h, :s, :v, :a
30
+
31
+ # call-seq:
32
+ # new( [h,s,v,a] ) -> ColorHSV
33
+ # new( [h,s,v] ) -> ColorHSV
34
+ # new( color ) -> ColorHSV
35
+ #
36
+ # Create a new instance from an Array or an existing color
37
+ # (of any type). If the alpha (opacity) component is omitted
38
+ # from the array, full opacity will be used.
39
+ #
40
+ # All color components range from 0.0 to 1.0.
41
+ #
42
+ def initialize( color )
43
+ if color.kind_of?(Array)
44
+ @h, @s, @v, @a = color.collect { |i| i.to_f }
45
+ @a = 1.0 unless @a
46
+ elsif color.respond_to?(:to_rgba_ary)
47
+ @h, @s, @v, @a = self.class.rgba_to_hsva( *color.to_rgba_ary )
48
+ end
49
+ end
50
+
51
+ # Return an Array with the red, green, blue, and alpha components
52
+ # of the color (converting the color to the RGBA model first).
53
+ def to_rgba_ary
54
+ return self.class.hsva_to_rgba( @h, @s, @v, @a )
55
+ end
56
+
57
+ def to_s
58
+ "#<#{self.class} [#{@h}, #{@s}, #{@v}, #{@a}]>"
59
+ end
60
+ alias :inspect :to_s
61
+
62
+ class << self
63
+
64
+ def new_from_rgba( rgba )
65
+ new( rgba_to_hsva(*rgba) )
66
+ end
67
+
68
+ def new_from_sdl_rgba( rgba )
69
+ new_from_rgba( rgba.collect { |i| i / 255.0 } )
70
+ end
71
+
72
+ # Convert the red, green, blue, and alpha to the
73
+ # equivalent hue, saturation, value, and alpha.
74
+ def rgba_to_hsva( r, g, b, a ) # :nodoc:
75
+ rgb_arr = [r, g, b]
76
+ max = rgb_arr.max
77
+ min = rgb_arr.min
78
+
79
+ # Calculate hue.
80
+ if min == max
81
+ h = 0
82
+ # Undefined in this case, but set it to zero
83
+ elsif max == r and g >= b
84
+ h = (1.quo(6) * (g - b) / (max - min)) + 0
85
+ elsif max == r and g < b
86
+ h = (1.quo(6) * (g - b) / (max - min)) + 1.0
87
+ elsif max == g
88
+ h = (1.quo(6) * (b - r) / (max - min)) + 1.quo(3)
89
+ elsif max == b
90
+ h = (1.quo(6) * (r - g) / (max - min)) + 2.quo(3)
91
+ else
92
+ raise "Should never happen"
93
+ end
94
+
95
+ # Calulate value.
96
+ v = max
97
+
98
+ # Calculate saturation.
99
+ if max == 0.0
100
+ s = 0.0
101
+ else
102
+ s = 1.0 - (min / max)
103
+ end
104
+
105
+ return [h,s,v,a]
106
+ end
107
+
108
+ # Convert the hue, saturation, value, and alpha
109
+ # to the equivalent red, green, blue, and alpha.
110
+ def hsva_to_rgba( h, s, v, a ) # :nodoc:
111
+ # Determine what part of the "color hexagon" the hue is in.
112
+ hi = (h * 6).floor % 6
113
+
114
+ # Fractional part
115
+ f = (h * 6) - hi
116
+
117
+ # Helper values
118
+ p = v * (1.0 - s)
119
+ q = v * (1.0 - (f * s))
120
+ t = v * (1.0 - ((1.0 - f) * s))
121
+
122
+ # Finally calculate the rgb values
123
+ r, g, b = calculate_rgb(hi, v, p, t, q)
124
+
125
+ return [r, g, b, a]
126
+ end
127
+
128
+ private
129
+
130
+ def calculate_rgb(hi, v, p, t, q) # :nodoc:
131
+ case hi
132
+ when 0
133
+ return v, t, p
134
+ when 1
135
+ return q, v, p
136
+ when 2
137
+ return p, v, t
138
+ when 3
139
+ return p, q, v
140
+ when 4
141
+ return t, p, v
142
+ when 5
143
+ return v, p, q
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,78 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ require 'rubygame/color/models/base'
21
+
22
+ module Rubygame
23
+ module Color
24
+
25
+ # Represents color in the RGB (Red, Green, Blue) color space.
26
+ class ColorRGB
27
+ include ColorBase
28
+
29
+ attr_reader :r, :g, :b, :a
30
+
31
+ # call-seq:
32
+ # new( [r,g,b,a] ) -> ColorRGB
33
+ # new( [r,g,b] ) -> ColorRGB
34
+ # new( color ) -> ColorRGB
35
+ #
36
+ # Create a new instance from an Array or an existing color
37
+ # (of any type). If the alpha (opacity) component is omitted
38
+ # from the array, full opacity will be used.
39
+ #
40
+ # All color components range from 0.0 to 1.0.
41
+ #
42
+ def initialize( color )
43
+ if color.kind_of?(Array)
44
+ @r, @g, @b, @a = color.collect { |i| i.to_f }
45
+ @a = 1.0 unless @a
46
+ elsif color.respond_to?(:to_rgba_ary)
47
+ @r, @g, @b, @a = color.to_rgba_ary
48
+ end
49
+ end
50
+
51
+ # Converts the color to an RGBA array of integers
52
+ # ranging from 0 to 255, as SDL wants.
53
+ def to_sdl_rgba_ary
54
+ self.to_rgba_ary.collect { |i| (i * 255).to_i }
55
+ end
56
+
57
+ def to_rgba_ary
58
+ return [@r, @g, @b, @a]
59
+ end
60
+
61
+ def to_s
62
+ "#<#{self.class} [#{@r}, #{@g}, #{@b}, #{@a}]>"
63
+ end
64
+ alias :inspect :to_s
65
+
66
+ class << self
67
+ def new_from_rgba( rgba )
68
+ new( rgba )
69
+ end
70
+
71
+ def new_from_sdl_rgba( rgba )
72
+ new_from_rgba( rgba.collect { |i| i / 255.0 } )
73
+ end
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,49 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ require 'rubygame/color'
21
+ require 'rubygame/color/palettes/palette'
22
+ require 'rubygame/color/palettes/x11'
23
+
24
+ module Rubygame
25
+ module Color
26
+
27
+ # :enddoc:
28
+
29
+ # The CSS module contains all the colors in the CSS/HTML palette
30
+ # by symbol name, e.g. :alice_blue, :dark_olive_green, etc.
31
+ #
32
+ # NOTE: The CSS palette is identical to the X11 palette except for
33
+ # four colors: gray, green, maroon, and purple.
34
+ #
35
+ # Differences between CSS and X11 derived from
36
+ # http://en.wikipedia.org/wiki/X11_color_names
37
+ # as accessed on 2007-12-17
38
+ #
39
+ CSS = Palette.new({
40
+ :gray => ColorRGB.new( [0.50196, 0.50196, 0.50196] ),
41
+ :green => ColorRGB.new( [0.00000, 0.50196, 0.00000] ),
42
+ :maroon => ColorRGB.new( [0.50196, 0.00000, 0.00000] ),
43
+ :purple => ColorRGB.new( [0.50196, 0.00000, 0.50196] )
44
+ })
45
+
46
+ CSS.include X11
47
+
48
+ end
49
+ end