Class: PixelChart
Overview
PixelChart class
Constant Summary
Constants included from Version
Class Method Summary collapse
-
.dimensions(area) ⇒ Array<Array<Integer>>
Calculate the possible dimensions (width, height) for a given area.
-
.load_file(filename) ⇒ Array<Integer>
Read the input file to extract the values.
Instance Method Summary collapse
-
#dimensions ⇒ Array<Array<Integer>>
Calculate the possible dimensions (width, height) for the data of the object.
-
#draw(filename, opts = {}) ⇒ Object
Generate and save the image.
-
#initialize(data, width, height, opts = {}) ⇒ PixelChart
constructor
A new instance of PixelChart.
Constructor Details
#initialize(data, width, height, opts = {}) ⇒ PixelChart
A new instance of PixelChart
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pixelchart.rb', line 27 def initialize(data, width, height, opts = {}) @width = check_int(width) @height = check_int(height) @data = check_data(data) opts[:colors] ||= [[255, 255, 255], [0, 0, 0]] # white, black check_colors(opts[:colors]) opts[:colors].each_with_index do |color, i| opts[:colors][i] = (0..2).map { |_x| rand(256) } if color == :random end @colors = opts[:colors] opts[:scale] ||= nil @scale = opts[:scale] end |
Class Method Details
.dimensions(area) ⇒ Array<Array<Integer>>
Calculate the possible dimensions (width, height) for a given area
60 61 62 63 64 65 66 67 |
# File 'lib/pixelchart.rb', line 60 def self.dimensions(area) dim = [] (1..Math.sqrt(area).to_i).each do |x| y, rem = area.divmod(x) dim.push([x, y]) if rem.zero? end dim end |
.load_file(filename) ⇒ Array<Integer>
Read the input file to extract the values
79 80 81 82 83 84 85 86 87 |
# File 'lib/pixelchart.rb', line 79 def self.load_file(filename) content = File.read(filename).gsub(/\s+/, '') data = content.split(',') data.each_with_index do |x, i| data[i] = 0 if x == '0' || x.downcase == 'false' data[i] = 1 if x == '1' || x.downcase == 'true' end data end |
Instance Method Details
#dimensions ⇒ Array<Array<Integer>>
Calculate the possible dimensions (width, height) for the data of the object
71 72 73 |
# File 'lib/pixelchart.rb', line 71 def dimensions dimensions(@data.size) end |
#draw(filename, opts = {}) ⇒ Object
Generate and save the image
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pixelchart.rb', line 46 def draw(filename, opts = {}) opts[:backend] ||= :rmagick backend = check_backend(opts[:backend]) case backend when :rmagick draw_rmagick(filename) when :rubyvips draw_rubyvips(filename) end end |