From 27b0a3dbda13a553a12eb57ac9d272e9367b3d2c Mon Sep 17 00:00:00 2001 From: Stormwind Date: Wed, 18 Feb 2015 23:45:53 +0100 Subject: [PATCH] Be able to create an image from data At the moment the image is simply shown to the user. But we need something better. The user must be able to decide, what happens. Also I want support for other colors. --- lib/rubella.rb | 3 ++- lib/rubella/output/image.rb | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/rubella.rb b/lib/rubella.rb index 4c26ef1..d290f0e 100644 --- a/lib/rubella.rb +++ b/lib/rubella.rb @@ -56,7 +56,8 @@ module Rubella def process inpt weight = @weighting.new 10 - weight.parse inpt + outpt = @output.new + outpt.create weight.parse(inpt) end def input_json diff --git a/lib/rubella/output/image.rb b/lib/rubella/output/image.rb index aa09a4b..cbae72a 100644 --- a/lib/rubella/output/image.rb +++ b/lib/rubella/output/image.rb @@ -1,7 +1,53 @@ +require 'RMagick' + module Rubella module Output class Image + attr_accessor :field_size + + def initialize field_size = 15 + @field_size = field_size + end + + def create parsed_list + buckets = parsed_list[0].length + columns = parsed_list.length + + # image size + x = columns*@field_size + y = buckets*@field_size + + # start drawing the damn thing + loadImg = Magick::Image.new(x, y) { self.background_color = "white" } + + i = 0 + parsed_list.each do |point| + j = 0 + point.reverse.each do |part| + # draw a red rectangle on the white background + core = Magick::Draw.new + + idensity = 127.5 * part + # Fix for float stupidity + idensity = 127.5 if idensity > 127.5 + + # Get the correct value + l = (255-idensity).round + + # Draw + core.fill(Magick::Pixel.from_hsla(0, 255, l, 1).to_color) + core.rectangle((i*@field_size), (j*@field_size), ((i+1)*@field_size), ((j+1)*@field_size)) + core.draw loadImg + j = j + 1 + end + i = i + 1 + end + + loadImg.display + end + + end end