-
Notifications
You must be signed in to change notification settings - Fork 1
/
doge.rb
65 lines (56 loc) · 1.94 KB
/
doge.rb
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
# https://github.com/pascalj/doge
require 'RMagick'
#require 'Random'
class Doge
attr_reader :image
def initialize(&block)
@image = Magick::ImageList.new(File.expand_path('doge_img.jpg', File.dirname(__FILE__)))
@occupied = []
instance_eval &block if block
end
def wuff(term)
text = image_text
metrics = text.get_type_metrics(@image, term)
horizontal_padding = @image.columns * 0.1
horizontal_top = @image.columns - horizontal_padding - metrics.width
height = metrics.height.round + 5 #padding
#@image.annotate(text, 0,0, (horizontal_padding.round..horizontal_top.round).to_a.choice.round, y_for_height(height), term)
@image.annotate(text, 0,0, Random.rand(horizontal_padding..horizontal_top), y_for_height(height), term)
self
end
#[:such, :wow, :so, :much, :amaze, :very].each do |word|
# define_method(word) { |text = ''| wuff(word.to_s + " " + text) }
#end
private
def random_rgb
#"rgb(#{(0..255).to_a.choice}, #{(0..255).to_a.choice}, #{(0..255).to_a.choice})"
"rgb(#{Random.rand(0..255)}, #{Random.rand(0..255)}, #{Random.rand(0..255)})"
end
def image_text
text = Magick::Draw.new
text.gravity = Magick::NorthWestGravity
text.pointsize = 50
text.font_family = "Comic Sans MS"
text.font_weight = Magick::BoldWeight
text.fill = random_rgb
text
end
def y_for_height(height)
vertical_padding = @image.rows * 0.1
vertical_top = @image.rows - vertical_padding
row = 0
begin
#row = (vertical_padding.round..vertical_top.round).to_a.choice.round
row = Random.rand(vertical_padding..vertical_top).round
end while row_occupied?(row, height)
@occupied << (row..(row + height))
row
end
def row_occupied?(row, height)
@occupied.each do |range|
return true if range.include?(row)
return true if range.include?(row + height)
end
false
end
end