tung-tea 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,7 +56,7 @@ Below is a simple bouncing circle demo.
56
56
  dy = -dy if y - r < 0 || y + r >= Tea::Screen.h
57
57
 
58
58
  Tea::Screen.clear
59
- Tea::Screen.circle x, y, r, 0xff00ffff
59
+ Tea::Screen.circle x, y, r, Tea::Color::MAGENTA
60
60
  Tea::Screen.update
61
61
 
62
62
  sleep 0.001
@@ -8,9 +8,9 @@ You should see a red square in a cyan square for 2 seconds.
8
8
  TEST
9
9
 
10
10
  Tea.init
11
- a = Tea::Bitmap.new(200, 200, 0x00ffffff)
11
+ a = Tea::Bitmap.new(200, 200, Tea::Color::CYAN)
12
12
  Tea::Screen.set_mode 400, 300
13
- b = Tea::Bitmap.new(150, 150, 0xff0000ff)
13
+ b = Tea::Bitmap.new(150, 150, Tea::Color::RED)
14
14
 
15
15
  Tea::Screen.blit a, 100, 50
16
16
  Tea::Screen.blit b, 125, 75
@@ -15,9 +15,9 @@ TEST
15
15
  Tea.init
16
16
  Tea::Screen.set_mode 400, 300
17
17
 
18
- Tea::Screen.circle 200, 150, 100, 0xffffffff
19
- Tea::Screen.circle 100, 150, 50, 0x00ff00ff, :antialias => true
20
- Tea::Screen.circle 300, 150, 50, 0xff0000ff, :outline => true
18
+ Tea::Screen.circle 200, 150, 100, Tea::Color::WHITE
19
+ Tea::Screen.circle 100, 150, 50, Tea::Color::GREEN, :antialias => true
20
+ Tea::Screen.circle 300, 150, 50, Tea::Color::RED, :outline => true
21
21
 
22
22
  Tea::Screen.update
23
23
 
@@ -23,18 +23,19 @@ TEST
23
23
  Tea.init
24
24
  Tea::Screen.set_mode 400, 300
25
25
 
26
- Tea::Screen.rect 100, 75, 200, 150, 0xffffffff
26
+ Tea::Screen.rect 100, 75, 200, 150, Tea::Color::WHITE
27
27
 
28
- Tea::Screen.circle 100, 75, 25, 0x00ff0080, :outline => false, :antialias => false, :mix => :blend
29
- Tea::Screen.circle 200, 75, 25, 0x00ff0080, :outline => false, :antialias => false, :mix => :replace
30
- Tea::Screen.circle 300, 75, 25, 0x00ff0080, :outline => false, :antialias => true, :mix => :blend
28
+ translucent_green = Tea::Color.mix(0, 255, 0, 128)
29
+ Tea::Screen.circle 100, 75, 25, translucent_green, :outline => false, :antialias => false, :mix => :blend
30
+ Tea::Screen.circle 200, 75, 25, translucent_green, :outline => false, :antialias => false, :mix => :replace
31
+ Tea::Screen.circle 300, 75, 25, translucent_green, :outline => false, :antialias => true, :mix => :blend
31
32
 
32
- Tea::Screen.circle 100, 150, 25, 0x00ff0080, :outline => false, :antialias => true, :mix => :replace
33
- Tea::Screen.circle 300, 150, 25, 0x00ff0080, :outline => true, :antialias => false, :mix => :blend
33
+ Tea::Screen.circle 100, 150, 25, translucent_green, :outline => false, :antialias => true, :mix => :replace
34
+ Tea::Screen.circle 300, 150, 25, translucent_green, :outline => true, :antialias => false, :mix => :blend
34
35
 
35
- Tea::Screen.circle 100, 225, 25, 0x00ff0080, :outline => true, :antialias => false, :mix => :replace
36
- Tea::Screen.circle 200, 225, 25, 0x00ff0080, :outline => true, :antialias => true, :mix => :blend
37
- Tea::Screen.circle 300, 225, 25, 0x00ff0080, :outline => true, :antialias => true, :mix => :replace
36
+ Tea::Screen.circle 100, 225, 25, translucent_green, :outline => true, :antialias => false, :mix => :replace
37
+ Tea::Screen.circle 200, 225, 25, translucent_green, :outline => true, :antialias => true, :mix => :blend
38
+ Tea::Screen.circle 300, 225, 25, translucent_green, :outline => true, :antialias => true, :mix => :replace
38
39
 
39
40
  Tea::Screen.update
40
41
 
@@ -24,21 +24,22 @@ TEST
24
24
  Tea.init
25
25
  Tea::Screen.set_mode 400, 300
26
26
 
27
- Tea::Screen.rect 100, 75, 200, 150, 0xffffffff
27
+ Tea::Screen.rect 100, 75, 200, 150, Tea::Color::WHITE
28
28
 
29
- b = Tea::Bitmap.new(Tea::Screen.w, Tea::Screen.h, 0x00000000)
30
- b.rect 105, 80, 190, 140, 0x0000ffff
29
+ b = Tea::Bitmap.new(Tea::Screen.w, Tea::Screen.h, Tea::Color.mix(0, 0, 0, 0))
30
+ b.rect 105, 80, 190, 140, Tea::Color.mix(0, 0, 255)
31
31
 
32
- b.circle 100, 75, 25, 0x00ff0080, :outline => false, :antialias => false, :mix => :blend
33
- b.circle 200, 75, 25, 0x00ff0080, :outline => false, :antialias => false, :mix => :replace
34
- b.circle 300, 75, 25, 0x00ff0080, :outline => false, :antialias => true, :mix => :blend
32
+ translucent_green = Tea::Color.mix(0, 255, 0, 128)
33
+ b.circle 100, 75, 25, translucent_green, :outline => false, :antialias => false, :mix => :blend
34
+ b.circle 200, 75, 25, translucent_green, :outline => false, :antialias => false, :mix => :replace
35
+ b.circle 300, 75, 25, translucent_green, :outline => false, :antialias => true, :mix => :blend
35
36
 
36
- b.circle 100, 150, 25, 0x00ff0080, :outline => false, :antialias => true, :mix => :replace
37
- b.circle 300, 150, 25, 0x00ff0080, :outline => true, :antialias => false, :mix => :blend
37
+ b.circle 100, 150, 25, translucent_green, :outline => false, :antialias => true, :mix => :replace
38
+ b.circle 300, 150, 25, translucent_green, :outline => true, :antialias => false, :mix => :blend
38
39
 
39
- b.circle 100, 225, 25, 0x00ff0080, :outline => true, :antialias => false, :mix => :replace
40
- b.circle 200, 225, 25, 0x00ff0080, :outline => true, :antialias => true, :mix => :blend
41
- b.circle 300, 225, 25, 0x00ff0080, :outline => true, :antialias => true, :mix => :replace
40
+ b.circle 100, 225, 25, translucent_green, :outline => true, :antialias => false, :mix => :replace
41
+ b.circle 200, 225, 25, translucent_green, :outline => true, :antialias => true, :mix => :blend
42
+ b.circle 300, 225, 25, translucent_green, :outline => true, :antialias => true, :mix => :replace
42
43
 
43
44
  Tea::Screen.blit b, 0, 0
44
45
 
@@ -10,8 +10,8 @@ TEST
10
10
  Tea.init
11
11
  Tea::Screen.set_mode 320, 240
12
12
 
13
- b = Tea::Bitmap.new(320, 240, 0x00000000)
14
- b.circle 160, 120, 100, 0xffffffff, :mix => :replace
13
+ b = Tea::Bitmap.new(320, 240, Tea::Color.mix(0, 0, 0, 0))
14
+ b.circle 160, 120, 100, Tea::Color::WHITE, :mix => :replace
15
15
  Tea::Screen.blit b, 0, 0
16
16
 
17
17
  Tea::Screen.update
@@ -25,13 +25,13 @@ Tea::Screen.set_mode 400, 300
25
25
  grid_x = Tea::Screen.w / 10
26
26
  grid_y = Tea::Screen.h / 10
27
27
 
28
- lines = [[1, 1, 1, 9, 0xff0000ff],
29
- [2, 1, 2, 9, 0x00ff00ff],
30
- [3, 1, 3, 9, 0x0000ffff],
31
- [6, 1, 9, 1, 0xffff00ff],
32
- [6, 2, 9, 2, 0xff00ffff],
33
- [6, 3, 9, 3, 0x00ffffff],
34
- [6, 9, 9, 6, 0x808080ff]]
28
+ lines = [[1, 1, 1, 9, Tea::Color::RED],
29
+ [2, 1, 2, 9, Tea::Color::GREEN],
30
+ [3, 1, 3, 9, Tea::Color::BLUE],
31
+ [6, 1, 9, 1, Tea::Color::YELLOW],
32
+ [6, 2, 9, 2, Tea::Color::MAGENTA],
33
+ [6, 3, 9, 3, Tea::Color::CYAN],
34
+ [6, 9, 9, 6, Tea::Color::DARK_GRAY]]
35
35
 
36
36
  lines.each do |line|
37
37
  Tea::Screen.line grid_x * line[0], grid_y * line[1],
@@ -41,7 +41,7 @@ end
41
41
 
42
42
  Tea::Screen.line grid_x * 6, grid_y * 6,
43
43
  grid_x * 9, grid_y * 9,
44
- 0xffffffff,
44
+ Tea::Color::WHITE,
45
45
  :antialias => true
46
46
 
47
47
  Tea::Screen.update
@@ -16,15 +16,15 @@ TEST
16
16
  Tea.init
17
17
  Tea::Screen.set_mode 400, 300
18
18
 
19
- b = Tea::Bitmap.new(250, 250, 0x00000000)
19
+ b = Tea::Bitmap.new(250, 250, Tea::Color.mix(0, 0, 0, 0))
20
20
  CENTER_X = b.w / 2
21
21
  CENTER_Y = b.h / 2
22
22
  LINE_CENTER_CLEARANCE = 10
23
23
  LINE_LENGTH = 100
24
- LINE_COLOR = 0x00ff0080
24
+ LINE_COLOR = Tea::Color.mix(0, 255, 0, 128)
25
25
  SPOKES = 16
26
26
 
27
- Tea::Screen.rect 150, 100, 100, 100, 0x404040ff
27
+ Tea::Screen.rect 150, 100, 100, 100, Tea::Color.mix(64, 64, 64)
28
28
 
29
29
  b.line CENTER_X, CENTER_Y, CENTER_X, CENTER_Y, LINE_COLOR, :antialias => true, :mix => :replace
30
30
 
@@ -20,10 +20,14 @@ TEST
20
20
  Tea.init
21
21
  Tea::Screen.set_mode 400, 300
22
22
 
23
- Tea::Screen.rect 200, 0, 200, 300, 0xff0000ff
24
- 20.times { |n| Tea::Screen.line 10, 10 + n, 390, 10 + n, 0x00ff0080 }
25
- 20.times { |n| Tea::Screen.line 10, 100 + n, 390, 100 + n, 0x0000ff80, :mix => :blend }
26
- 20.times { |n| Tea::Screen.line 10, 200 + n, 390, 200 + n, 0xffffff80, :mix => :replace }
23
+ tl_green = Tea::Color.mix( 0, 255, 0, 128)
24
+ tl_blue = Tea::Color.mix( 0, 0, 255, 128)
25
+ tl_white = Tea::Color.mix(255, 255, 255, 128)
26
+
27
+ Tea::Screen.rect 200, 0, 200, 300, Tea::Color::RED
28
+ 20.times { |n| Tea::Screen.line 10, 10 + n, 390, 10 + n, tl_green }
29
+ 20.times { |n| Tea::Screen.line 10, 100 + n, 390, 100 + n, tl_blue, :mix => :blend }
30
+ 20.times { |n| Tea::Screen.line 10, 200 + n, 390, 200 + n, tl_white, :mix => :replace }
27
31
 
28
32
  Tea::Screen.update
29
33
  sleep 5
@@ -16,10 +16,10 @@ TEST
16
16
  Tea.init
17
17
  Tea::Screen.set_mode 400, 300
18
18
 
19
- Tea::Screen.point 100, 75, 0xff0000ff
20
- Tea::Screen.point 300, 75, 0x00ff00ff
21
- Tea::Screen.point 100, 225, 0x0000ffff
22
- Tea::Screen.point 300, 225, 0xffffffff
19
+ Tea::Screen[100, 75] = Tea::Color::RED
20
+ Tea::Screen[300, 75] = Tea::Color::GREEN
21
+ Tea::Screen[100, 225] = Tea::Color::BLUE
22
+ Tea::Screen[300, 225] = Tea::Color::WHITE
23
23
 
24
24
  Tea::Screen.update
25
25
 
@@ -9,7 +9,7 @@ TEST
9
9
 
10
10
  Tea.init
11
11
  Tea::Screen.set_mode 400, 300
12
- Tea::Screen.rect 50, 50, 300, 200, 0xffff00ff
12
+ Tea::Screen.rect 50, 50, 300, 200, Tea::Color::YELLOW
13
13
  Tea::Screen.update
14
14
 
15
15
  sleep 2
@@ -25,23 +25,27 @@ TEST
25
25
  Tea.init
26
26
  Tea::Screen.set_mode 400, 300
27
27
 
28
+ TL_GREEN = Tea::Color.mix( 0, 255, 0, 128)
29
+ TL_BLUE = Tea::Color.mix( 0, 0, 255, 128)
30
+ TL_WHITE = Tea::Color.mix(255, 255, 255, 128)
31
+
28
32
  x, y = 205, 15
29
33
  up, down, left, right = false, false, false, false
30
34
  wait_for_event = true
31
35
 
32
- b = Tea::Bitmap.new(180, 130, 0x00000000)
33
- b.rect 45, 32, 90, 65, 0xff0000ff
34
- b.rect 0, 0, 90, 65, 0x00ff0080, :mix => :blend
35
- b.rect 90, 65, 90, 65, 0x0000ff80, :mix => :replace
36
- b.rect 0, 65, 90, 65, 0xffffff80
36
+ b = Tea::Bitmap.new(180, 130, Tea::Color::CLEAR)
37
+ b.rect 45, 32, 90, 65, Tea::Color::RED
38
+ b.rect 0, 0, 90, 65, TL_GREEN, :mix => :blend
39
+ b.rect 90, 65, 90, 65, TL_BLUE, :mix => :replace
40
+ b.rect 0, 65, 90, 65, TL_WHITE
37
41
 
38
42
  begin
39
43
  Tea::Screen.clear
40
44
 
41
- Tea::Screen.rect 100, 75, 200, 150, 0xff0000ff
42
- Tea::Screen.rect 0, 0, 200, 150, 0x00ff0080, :mix => :blend
43
- Tea::Screen.rect 200, 150, 200, 150, 0x0000ff80, :mix => :replace
44
- Tea::Screen.rect 0, 150, 200, 150, 0xffffff80
45
+ Tea::Screen.rect 100, 75, 200, 150, Tea::Color::RED
46
+ Tea::Screen.rect 0, 0, 200, 150, TL_GREEN, :mix => :blend
47
+ Tea::Screen.rect 200, 150, 200, 150, TL_BLUE, :mix => :replace
48
+ Tea::Screen.rect 0, 150, 200, 150, TL_WHITE
45
49
 
46
50
  Tea::Screen.blit b, x, y
47
51
 
@@ -14,6 +14,39 @@ This is the exception class raised when Bad Things happen in any of Tea's object
14
14
 
15
15
  h1. Graphics
16
16
 
17
+ h2. Tea::Color
18
+
19
+ This module holds utility methods for working with colours. It exists for convenience; colours can still be passed around in the form 0xRRGGBBAA.
20
+
21
+ There are some pre-mixed colour constants available in it:
22
+
23
+ |_. Color |_. Red |_. Green |_. Blue |_. Alpha |
24
+ | CLEAR |>. 0 |>. 0 |>. 0 |>. 0 |
25
+ | BLACK |>. 0 |>. 0 |>. 0 |>. 255 |
26
+ | DARK_RED |>. 128 |>. 0 |>. 0 |>. 255 |
27
+ | DARK_GREEN |>. 0 |>. 128 |>. 0 |>. 255 |
28
+ | DARK_YELLOW |>. 128 |>. 128 |>. 0 |>. 255 |
29
+ | DARK_BLUE |>. 0 |>. 0 |>. 128 |>. 255 |
30
+ | DARK_MAGENTA |>. 128 |>. 0 |>. 128 |>. 255 |
31
+ | DARK_CYAN |>. 0 |>. 128 |>. 128 |>. 255 |
32
+ | DARK_GRAY |>. 128 |>. 128 |>. 128 |>. 255 |
33
+ | GRAY |>. 192 |>. 192 |>. 192 |>. 255 |
34
+ | RED |>. 255 |>. 0 |>. 0 |>. 255 |
35
+ | GREEN |>. 0 |>. 255 |>. 0 |>. 255 |
36
+ | YELLOW |>. 255 |>. 255 |>. 0 |>. 255 |
37
+ | BLUE |>. 0 |>. 0 |>. 255 |>. 255 |
38
+ | MAGENTA |>. 255 |>. 0 |>. 255 |>. 255 |
39
+ | CYAN |>. 0 |>. 255 |>. 255 |>. 255 |
40
+ | WHITE |>. 255 |>. 255 |>. 255 |>. 255 |
41
+
42
+ h3. Tea::Color.mix(red, green, blue, alpha=255)
43
+
44
+ Create a colour of the form 0xRRGGBBAA. Each argument should be an integer between 0..255 inclusive. The colours returned by this method can be used throughout the graphics API.
45
+
46
+ h3. Tea::Color.split(color)
47
+
48
+ Split a colour of the form 0xRRGGBBAA into its red, green, blue and alpha parts. A 4-element array is returned, of the form [red, green, blue, alpha], where each element is between 0..255 inclusive.
49
+
17
50
  h2. Tea::Bitmap
18
51
 
19
52
  A Bitmap is a grid of pixels that holds graphics. It can be drawn onto and drawn with.
@@ -44,12 +77,13 @@ The save method saves the Bitmap as an image file. Supported formats: BMP, PNG.
44
77
 
45
78
  Tea::Primitive:
46
79
  * clear()
47
- * point(x, y, color)
80
+ * [x, y]
81
+ * [x, y] = color
48
82
  * rect(x, y, w, h, color[, {:mix => :blend (alt. :replace)}])
49
83
  * line(x1, y1, x2, y2, color[, {:antialias => false, :mix => :blend (alt. :replace)}])
50
84
  * circle(x, y, radius, color[, {:outline => false, :antialias => false, :mix => :blend (alt. :replace)}])
51
85
 
52
- Note that colours are of the form 0xRRGGBBAA.
86
+ Note that colours are of the form 0xRRGGBBAA, and can be conveniently made using Tea::Color.mix().
53
87
 
54
88
  h3. Tea::Bitmap.new(*args)
55
89
 
@@ -98,12 +132,13 @@ The save method saves the Screen as an image file. Supported formats: BMP, PNG.
98
132
 
99
133
  Tea::Primitive:
100
134
  * clear()
101
- * point(x, y, color)
135
+ * [x, y]
136
+ * [x, y] = color
102
137
  * rect(x, y, w, h, color[, {:mix => :blend (alt. :replace)}])
103
138
  * line(x1, y1, x2, y2, color[, {:antialias => false, :mix => :blend (alt. :replace)}])
104
139
  * circle(x, y, radius, color[, {:outline => false, :antialias => false, :mix => :blend (alt. :replace)}])
105
140
 
106
- Note that colours are of the form 0xRRGGBBAA.
141
+ Note that colours are of the form 0xRRGGBBAA, and can be conveniently made with Tea::Color.mix().
107
142
 
108
143
  h3. Tea::Screen.set_mode(width, height)
109
144
 
@@ -182,7 +217,9 @@ require 'tea'
182
217
 
183
218
  class MyEventHandler
184
219
  attr_reader :done
185
- def initialize; @done = false; end
220
+ def initialize
221
+ @done = false
222
+ end
186
223
 
187
224
  include Tea::Event::Dispatch
188
225
 
data/lib/tea.rb CHANGED
@@ -5,6 +5,7 @@ require 'sdl'
5
5
  require 'tea/c_bitmap'
6
6
  require 'tea/c_error'
7
7
  require 'tea/c_sound'
8
+ require 'tea/m_color'
8
9
  require 'tea/m_event'
9
10
  require 'tea/o_screen'
10
11
 
@@ -0,0 +1,50 @@
1
+ # This file holds the Color module.
2
+
3
+ #
4
+ module Tea
5
+
6
+ # The Color module holds utility methods that can mix and split colors.
7
+ module Color
8
+
9
+ # Create a color from red, green, blue and optionally alpha parts.
10
+ # All values passed in should be within 0..255 inclusive.
11
+ def Color.mix(red, green, blue, alpha=255)
12
+ ((red & 0xff) << 24) |
13
+ ((green & 0xff) << 16) |
14
+ ((blue & 0xff) << 8) |
15
+ (alpha & 0xff)
16
+ end
17
+
18
+ # Break a colour up into its red, green, blue and alpha parts.
19
+ # Returns a 4-element array of the form [red, green, blue, alpha], where
20
+ # each element is within 0..255 inclusive.
21
+ def Color.split(color)
22
+ [(color & 0xff000000) >> 24,
23
+ (color & 0x00ff0000) >> 16,
24
+ (color & 0x0000ff00) >> 8,
25
+ (color & 0x000000ff)]
26
+ end
27
+
28
+ CLEAR = Color.mix( 0, 0, 0, 0)
29
+
30
+ BLACK = Color.mix( 0, 0, 0)
31
+ DARK_RED = Color.mix(128, 0, 0)
32
+ DARK_GREEN = Color.mix( 0, 128, 0)
33
+ DARK_YELLOW = Color.mix(128, 128, 0)
34
+ DARK_BLUE = Color.mix( 0, 0, 128)
35
+ DARK_MAGENTA = Color.mix(128, 0, 128)
36
+ DARK_CYAN = Color.mix( 0, 128, 128)
37
+ DARK_GRAY = Color.mix(128, 128, 128)
38
+
39
+ GRAY = Color.mix(192, 192, 192)
40
+ RED = Color.mix(255, 0, 0)
41
+ GREEN = Color.mix( 0, 255, 0)
42
+ YELLOW = Color.mix(255, 255, 0)
43
+ BLUE = Color.mix( 0, 0, 255)
44
+ MAGENTA = Color.mix(255, 0, 255)
45
+ CYAN = Color.mix( 0, 255, 255)
46
+ WHITE = Color.mix(255, 255, 255)
47
+
48
+ end
49
+
50
+ end
@@ -4,6 +4,7 @@
4
4
  require 'sdl'
5
5
 
6
6
  require 'tea/c_bitmap'
7
+ require 'tea/m_color'
7
8
 
8
9
  #
9
10
  module Tea
@@ -48,12 +49,11 @@ module Tea
48
49
  raise ArgumentError, "wrong number of arguments (#{box.length} for 0 or 4)", caller
49
50
  end
50
51
 
51
- bitmap = Bitmap.new(w, h, 0x00000000)
52
+ bitmap = Bitmap.new(w, h, Tea::Color::CLEAR)
52
53
 
53
- for buf_y in y..(y + h)
54
- for buf_x in x..(x + w)
55
- red, green, blue, alpha = buffer.get_rgba(buffer[buf_x, buf_y])
56
- bitmap.point buf_x - x, buf_y - y, ((red << 24) | (green << 16) | (blue << 8) | alpha)
54
+ for buf_y in y...(y + h)
55
+ for buf_x in x...(x + w)
56
+ bitmap[buf_x - x, buf_y - y] = Color.mix(*buffer.get_rgba(buffer[buf_x, buf_y]))
57
57
  end
58
58
  end
59
59
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'sdl'
4
4
 
5
+ require 'tea/m_color'
6
+
5
7
  #
6
8
  module Tea
7
9
 
@@ -24,16 +26,30 @@ module Tea
24
26
  primitive_color(0x000000ff)
25
27
  end
26
28
 
29
+ # Get the color (0xRRGGBBAA) at the point (x, y) on the Bitmap.
30
+ #
31
+ # Raises Tea::Error if (x, y) is outside of the Bitmap's area.
32
+ def [](x, y)
33
+ buffer = primitive_buffer
34
+ w = buffer.w
35
+ h = buffer.h
36
+ if x < 0 || x >= w || y < 0 || y >= h
37
+ raise Tea::Error, "can't get color at (#{x}, #{y}), not within #{w}x#{h}", caller
38
+ end
39
+ Color.mix(*buffer.get_rgba(buffer[x, y]))
40
+ end
41
+
27
42
  # Plot a point at (x, y) with the given color (0xRRGGBBAA) on the Bitmap.
28
43
  #
29
- # Raises Tea::Error if (x, y) is outside of the drawing buffer.
30
- def point(x, y, color)
31
- w = primitive_buffer.w
32
- h = primitive_buffer.h
33
- if x < 0 || x > w || y < 0 || y > h
44
+ # Raises Tea::Error if (x, y) is outside of the Bitmap's area.
45
+ def []=(x, y, color)
46
+ buffer = primitive_buffer
47
+ w = buffer.w
48
+ h = buffer.h
49
+ if x < 0 || x >= w || y < 0 || y >= h
34
50
  raise Tea::Error, "can't plot point (#{x}, #{y}), not within #{w}x#{h}", caller
35
51
  end
36
- primitive_buffer[x, y] = primitive_color(color)
52
+ buffer[x, y] = primitive_color(color)
37
53
  end
38
54
 
39
55
  # Mixer for alpha blend mix strategy.
@@ -68,7 +84,7 @@ module Tea
68
84
  raise Tea::Error, "can't draw rectangle of size #{w}x#{h}", caller
69
85
  end
70
86
 
71
- r, g, b, a = primitive_hex_to_rgba(color)
87
+ r, g, b, a = Color.split(color)
72
88
 
73
89
  if options == nil || options[:mix] == nil
74
90
  mix = (a < 0xff) ? :blend : :replace
@@ -105,7 +121,7 @@ module Tea
105
121
  # +:replace+ writes over the RGBA parts of the line
106
122
  # destination pixels.
107
123
  def line(x1, y1, x2, y2, color, options=nil)
108
- r, g, b, a = primitive_hex_to_rgba(color)
124
+ r, g, b, a = Color.split(color)
109
125
 
110
126
  if options == nil
111
127
  antialias = false
@@ -149,7 +165,7 @@ module Tea
149
165
  raise Tea::Error, "can't draw circle with radius #{radius}", caller
150
166
  end
151
167
 
152
- r, g, b, a = primitive_hex_to_rgba(color)
168
+ r, g, b, a = Color.split(color)
153
169
 
154
170
  if options == nil
155
171
  outline = false
@@ -198,16 +214,7 @@ module Tea
198
214
  # Convert hex_color of the form 0xRRGGBBAA to a color value the
199
215
  # primitive_buffer understands.
200
216
  def primitive_color(hex_color)
201
- primitive_rgba_to_color(*primitive_hex_to_rgba(hex_color))
202
- end
203
-
204
- # Break hex_color from the form 0xRRGGBBAA to [red, green, blue, alpha].
205
- def primitive_hex_to_rgba(hex_color)
206
- red = (hex_color & 0xff000000) >> 24
207
- green = (hex_color & 0x00ff0000) >> 16
208
- blue = (hex_color & 0x0000ff00) >> 8
209
- alpha = (hex_color & 0x000000ff)
210
- [red, green, blue, alpha]
217
+ primitive_rgba_to_color(*Color.split(hex_color))
211
218
  end
212
219
 
213
220
  # Generate a colour compatible with the primitive buffer.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tung-tea
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
@@ -80,6 +80,7 @@ files:
80
80
  - lib/tea/c_bitmap.rb
81
81
  - lib/tea/c_error.rb
82
82
  - lib/tea/c_sound.rb
83
+ - lib/tea/m_color.rb
83
84
  - lib/tea/m_event.rb
84
85
  - lib/tea/m_event_app.rb
85
86
  - lib/tea/m_event_dispatch.rb