-
Notifications
You must be signed in to change notification settings - Fork 2
/
coalesce.rb.html
83 lines (73 loc) · 2.54 KB
/
coalesce.rb.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE public PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta name="generator" content="ex2html.rb" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/popup.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>
hljs.configure({ cssSelector: "pre" });
hljs.highlightAll();
</script>
<title>RMagick example: coalesce.rb</title>
</head>
<body>
<h1>coalesce.rb</h1>
<div class="bodybox">
<div class="bodyfloat">
<pre class="language-ruby">
# frozen_string_literal: true
require 'rmagick'
buttons = Magick::ImageList.new
# Read 25 alphabet image files, scale to 1/4 size
letter = +'A'
26.times do
if letter != 'M' # "M" is not the same size as the other letters
tiny = Magick::Image.read('images/Button_' + letter + '.gif').first
tiny.scale! 0.25
buttons << tiny
end
letter.succ!
end
# Create a image that will hold the alphabet images in 5 rows and 5 columns.
cells = Magick::ImageList.new
cells.new_image buttons.columns * 5, buttons.rows * 5 do |options|
options.background_color = '#000000ff' # transparent
end
cells.alpha(Magick::ActivateAlphaChannel)
offset = Magick::Rectangle.new(0, 0, 0, 0)
# Create 2 arrays from which we can randomly choose row,col pairs
row = [0] * 5 + [1] * 5 + [2] * 5 + [3] * 5 + [4] * 5
col = (0..4).to_a * 5
# The coalesce method composites the 2nd image over the 1st, the 3rd image
# over the result, and so forth, respecting the page offset of the images.
srand 1234
25.times do |i|
# Randomly select a row and column for this copy of the "tinya" image.
# Compute the x,y position of this copy in pixels and store the
# result in the image's page attribute. Append a copy of the image
# to the image array in "a".
n = rand row.length
x = row.delete_at n
y = col.delete_at n
button = buttons[i]
offset.x = x * button.columns
offset.y = y * button.rows
button.page = offset
button.alpha(Magick::SetAlphaChannel)
cells << button
end
puts 'This may take a few seconds...'
cells.delay = 10
cells.iterations = 10_000
res = cells.coalesce
res.write 'coalesce_anim.gif'
res[25].write 'coalesce.gif'
exit
</pre>
</div>
</div>
<div id="close"><a href="javascript:window.close();">Close window</a></div>
</body>
</html>