Class: Turing::Image::Blending

Inherits:
Turing::Image show all
Defined in:
lib/turing/image_plugins/blending.rb

Overview

Blending Turing test

Blends text with background (highly imperfect for some backgrounds).

Instance Method Summary collapse

Methods inherited from Turing::Image

#initialize

Constructor Details

This class inherits a constructor from Turing::Image

Instance Method Details

#generate(img, word, bg = nil) ⇒ Object

contract method - generate the challenge



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
# File 'lib/turing/image_plugins/blending.rb', line 15

def generate(img, word, bg = nil) # {{{
	if bg.nil?
		possible = Dir[File.join(@options[:bgdir], '*')]
		bg = possible[rand(possible.size)]
	else
		unless FileTest.exists?(bg)
			raise ArgumentError, "Wrong background!"
		end
	end

	img_tmp = GD2::Image.load(File.open(bg, 'r'))

	if img_tmp.width < img.width || img_tmp.height < img.height
		raise "Background has insufficient dimensions"
	end

	img.copy_from(img_tmp, 0, 0, 0, 0, img.width, img.height)

	# XXX: equivalent of img_tmp.destroy ?
	img_tmp = nil

	r = rand(32)
	fg = GD2::Color[r, r, r, 40.percent]

	write_string(img, 'georgiai.ttf', fg, word, 40)

end