diff --git a/images/drake_egon.png b/images/drake_egon.png new file mode 100644 index 0000000..49d64ff Binary files /dev/null and b/images/drake_egon.png differ diff --git a/main.lua b/main.lua index 588b1db..91dd937 100644 --- a/main.lua +++ b/main.lua @@ -239,32 +239,63 @@ local function droste_positions_from_topics (topics, places) return droste_poss end -local function generate_distracted_boyfriend (topics) - assert(type(topics) == 'table' and #topics == 2) - - local font_name = choose_random_font() - - local base_img = imlib.image.load './images/distracted_boyfriend.jpg' - - local HEAD_W, HEAD_H = 150, 150 - - local DISTRACTED_BOYFRIEND_POS = { - { x = 640 + math.random(-20, 20), y = 50 + math.random(-20, 20), w = HEAD_W, h = HEAD_H }, - { x = 180 + math.random(-20, 20), y = 50 + math.random(-20, 20), w = HEAD_W, h = HEAD_H }, - } - - -- Paste topic onto head - for index, pos in ipairs(DISTRACTED_BOYFRIEND_POS) do - paste_topic_onto_image(base_img, topics[index], pos.x, pos.y, pos.w, pos.h, nil, font_name) - end - - -- Droste - base_img = draw_droste_effect(base_img, droste_positions_from_topics(topics, DISTRACTED_BOYFRIEND_POS)) - - -- - return save_to_cloud(base_img) +local function shallow_copy (t0) + local t = {} + for k,v in pairs(t0) do t[k] = v end + return t end +local function generate_comparison_meme_generator (positions) + assert(type(positions) == 'table') + assert(type(positions.base_img_path) == 'string') + + return function (topics) + assert(type(topics) == 'table' and #topics == 2) + + local font_name = choose_random_font() + + local base_img = imlib.image.load(positions.base_img_path) + + -- Randomize pos + local rand_positions = {} + for i, pos in ipairs(positions) do + rand_positions[i] = shallow_copy(pos) + rand_positions[i].x = pos.x + math.random(-(pos.xr or 0), pos.xr or 0) + rand_positions[i].y = pos.y + math.random(-(pos.yr or 0), pos.yr or 0) + end + + -- Paste topic onto head + for index, pos in ipairs(rand_positions) do + paste_topic_onto_image(base_img, topics[index], pos.x, pos.y, pos.w, pos.h, nil, font_name) + end + + -- Droste + base_img = draw_droste_effect(base_img, droste_positions_from_topics(topics, rand_positions)) + + -- + return save_to_cloud(base_img) + end +end + +local generate_distracted_boyfriend = generate_comparison_meme_generator { + base_img_path = './images/distracted_boyfriend.jpg', + + { x = 640, xr = 20, y = 50, yr = 20, w = 150, h = 150 }, + { x = 180, xr = 20, y = 50, yr = 20, w = 150, h = 150 }, +} + +local generate_drake_egon_olsen = generate_comparison_meme_generator { + base_img_path = './images/drake_egon.png', + + { x = 380, xr = 0, y = 0, yr = 0, w = 380, h = 354 }, + { x = 377, xr = 0, y = 354, yr = 0, w = 383, h = 360 }, +} + +local GENERATE_COMPARISON_MEME_OF_2 = { + generate_distracted_boyfriend, + generate_drake_egon_olsen +} + local BRAIN_ROW_HEIGHT = 150 local BRAIN_ROW_WIDTH = 200 @@ -302,6 +333,11 @@ local function generate_brain_explosion_image (topics) return save_to_cloud(base_img) end +local function choose(l) + assert(type(l) == 'table') + return l[math.random(#l)] +end + -------------------------------------------------------------------------------- ---- @@ -493,8 +529,8 @@ local function handle_message(bot, user, channel, message) local img_link, error_message -- - if #topics == 2 and math.random() <= 0.5 then - img_link, error_message = generate_distracted_boyfriend(topics) + if #topics == 2 and math.random() <= 0.7 then + img_link, error_message = choose(GENERATE_COMPARISON_MEME_OF_2)(topics) else img_link, error_message = generate_brain_explosion_image(topics) end