From f5efd50d9f759e06704500f989b4c3e63fe899d3 Mon Sep 17 00:00:00 2001 From: Stormwind Date: Tue, 24 Feb 2015 20:57:56 +0100 Subject: [PATCH] Improvement of Rubella::Output::Image The class now overwrites the constructor of the basic class to set a default field_size value of 15 pixels. It also returns self in the create method corresponding to the base class. Plus I've added the documentation. --- lib/rubella/output/base.rb | 2 ++ lib/rubella/output/image.rb | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/rubella/output/base.rb b/lib/rubella/output/base.rb index 3a87e73..95d8bcb 100644 --- a/lib/rubella/output/base.rb +++ b/lib/rubella/output/base.rb @@ -17,6 +17,7 @@ module Rubella # # @param field_size int size of one value # @return Rubella::Output::Base + # def initialize field_size @field_size = field_size end @@ -26,6 +27,7 @@ module Rubella # # @param storage Rubella::Storage # @return Rubella::Output::Base + # def create storage raise NotImplementedError "Please override 'create' in your concrete "+ "implementation" diff --git a/lib/rubella/output/image.rb b/lib/rubella/output/image.rb index cbcb6d1..e920a69 100644 --- a/lib/rubella/output/image.rb +++ b/lib/rubella/output/image.rb @@ -4,8 +4,29 @@ require 'RMagick' module Rubella module Output + # Creates a pixel based graphic as representation of the given storage + # data using ImageMagick. + # By the RMagick extention. @data will contain a Magick image for futher + # handling the data please read the RMagick library documentation: + # http://www.imagemagick.org/RMagick/doc/ + # class Image < Base + # Constructor + # Has a default field_size of 15 pixel. + # + # @param field_size int How many pixel one value has + # @return Rubella::Output::Image + # + def initialize field_size = 15 + super field_size + end + + # Creates a pixel based graphic from the given storage data. + # + # @param storage Rubella::Storage + # @return Rubella::Output::Image + # def create storage buckets = storage.data[0].length columns = storage.data.length @@ -15,7 +36,7 @@ module Rubella y = buckets*@field_size # start drawing the damn thing - loadImg = Magick::Image.new(x, y) { self.background_color = "white" } + @data = Magick::Image.new(x, y) { self.background_color = "white" } i = 0 storage.data.each do |point| @@ -34,13 +55,13 @@ module Rubella # 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 + core.draw @data j = j + 1 end i = i + 1 end - @data = loadImg + self end