File: checkerboard.rb

package info (click to toggle)
libimlib2-ruby 0.5.2-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,552 kB
  • sloc: ansic: 4,671; ruby: 250; makefile: 7
file content (66 lines) | stat: -rwxr-xr-x 2,136 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/ruby

########################################################################
# checkerboard.rb - draw a checkerboard                                #
# (c) 2002 Paul Duncan <pabs@pablotron.org>                            #
#                                                                      #
# - test Imlib2::Image#fill_rect, Imlib2::Image#save, and color        #
#   constants (Imlib2::Color::BLACK, Imlib2::Color::WHITE, etc)        #
########################################################################

# load imlib2 bindings
require "imlib2"

# number, width, and height of boxes
w, h, bw, bh = 10, 10, 48, 48

# foreground (gradient), background, top piece, and bottom piece colors
fg = [Imlib2::Color::DARKGREY, Imlib2::Color::LIGHTGREY]
bg = Imlib2::Color::BLACK
tc, bc = Imlib2::Color::RED, Imlib2::Color::BLUE

# set output filename (use command-line argument if specified)
filename = "checkerboard.png"
filename = ARGV.pop if ARGV.length > 0

# allocate the new image and fill it with the background color
im = Imlib2::Image.new w * bw, h * bh
im.fill_rect [0, 0], [w * bw, h * bh], bg

# create foreground gradient
gradient = Imlib2::Gradient.new [0, fg[0]], [1, fg[1]]

# fill squares with foreground gradient
puts "Generating checkerboard..."
0.upto(w / 2) { |x|
  0.upto(h / 2) { |y|
    (0..1).each { |i|
      rect = [x * bw * 2 + bw * i, y * bh * 2 + bh * i, bw, bh]
      im.fill_gradient gradient, rect, 135.0
    }
  }
}

# draw checker pieces
puts "Generating pieces..."
0.upto(w - 1) { |x|
  0.upto(2) { |y|
    # draw top piece
    rect = [x * bw + bw/2, y * bh + bh/2, bw * 2/5, bh * 2/5]
    # Imlib2 bug (works properly w/ draw_ellipse, but not w/ fill_ellipse)
    im.fill_ellipse rect, tc if (x - (y % 2)) % 2 == 1

    # draw bottom piece
    rect[1] = h * bh - rect[1]
    # Imlib2 bug (works properly w/ draw_ellipse, but not w/ fill_ellipse)
    im.fill_ellipse rect, bc if (x - (y % 2)) % 2 == 0
  }
}

# save image (with error checking)
puts "Saving to \"#{filename}\"."
begin
  im.save filename
rescue Imlib2::FileError
  $stderr.puts "Couldn't save \"#{filename}\": " + $!
end