Class: PixelChart

Inherits:
Object
  • Object
show all
Includes:
Version
Defined in:
lib/pixelchart.rb

Overview

PixelChart class

Constant Summary

Constants included from Version

Version::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, width, height, opts = {}) ⇒ PixelChart

A new instance of PixelChart

Parameters:

  • data (Array<Boolean>)

    An array containing values (0, 1) used to generate the image.

  • width (Integer)

    Desired width of the image.

  • height (Integer)

    Desired height of the image.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :colors (Array<Color>)

    Must be an array containing 2 values. Each value is either the :random symbol to get a random color or a color given as RGB, so 3 integers in an array.

  • :scale (Scale)

    Scale the image to dimensions or with a ratio. Ratio must be a positive (non-nul) float or integer, and dimensions must be a set of width and height separated by a comma.


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

Parameters:

  • area (Integer)

    number of values, total of pixels

Returns:

  • (Array<Array<Integer>>)

    Array of possible dimensions


59
60
61
62
63
64
65
66
# File 'lib/pixelchart.rb', line 59

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

Parameters:

  • filename (String)

    Name of the input file containing data. Values must be 0, 1, true, false and separated by commas.

Returns:

  • (Array<Integer>)

    Array of 0, 1. Can be directly passed to initialize


78
79
80
81
82
83
84
85
86
# File 'lib/pixelchart.rb', line 78

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

#dimensionsArray<Array<Integer>>

Calculate the possible dimensions (width, height) for the data of the object

Returns:

  • (Array<Array<Integer>>)

    Array of possible dimensions


70
71
72
# File 'lib/pixelchart.rb', line 70

def dimensions
  dimensions(@data.size)
end

#draw(filename, opts = {}) ⇒ Object

Generate and save the image

Parameters:

  • filename (String)

    Name of the output image.

  • opts (Hash) (defaults to: {})

    options for image processing

Options Hash (opts):

  • :backend (Symbol)

    Image processing backend, :rmagick for Rmagick/Imagemagick or :rubyvips for ruby-vips/libvips.


46
47
48
49
50
51
52
53
54
# File 'lib/pixelchart.rb', line 46

def draw(filename, opts = {})
  opts[:backend] ||= :rmagick
  backend = check_backend(opts[:backend])
  if backend == :rmagick
    draw_rmagick(filename)
  elsif backend == :rubyvips
    draw_rubyvips(filename)
  end
end