Module: RubyMatrixToSvg

Defined in:
lib/ruby_matrix_to_svg.rb,
lib/ruby_matrix_to_svg/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.matrix_to_svg(title, matrix) ⇒ Object

generate the svg string

Example:

>> matrix_to_svg("svg_image_title" , matrix)
=> (String)

Parameters:

  • title (String)

    the title of the svg image

  • matrix (Array[Array[(String)]])

    the colors matrix



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_matrix_to_svg.rb', line 14

def self.matrix_to_svg title, matrix

  # test if params are correct
  raise "title is invalid" if title == nil || title == ''
  raise "matrix is invalid, must be a matrix (array of arrays) of colors" if matrix.class != Array || matrix[0].class != Array
  
  # open svg tag
  num_rows = matrix.length
  num_cols = matrix[0].length

  svg = "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='#{num_cols}' height='#{num_rows}'>"

  # set title
  svg << "<title>#{title}</title>" 

  # background with first color
  svg << "<rect width='100%' height='100%' x='0' y='0' fill='#{matrix[0][0]}' stroke='none' shape-rendering='crispEdges'/>"

  num_rows.times do |row|
    num_cols.times do |col|
      # draw rect if its color its different than background color
      if matrix[row][col] != matrix[0][0]
        svg << "<rect fill='#{matrix[row][col]}' stroke='none' fill-rule='nonzero' x='#{col}' y='#{row}' width='1' height='1' shape-rendering='crispEdges' />"
      end
    end
  end 

  # close svg tag
  svg << "</svg>"

end