Method: Pixelart::Image.parse_pixels

Defined in:
lib/pixelart/image.rb

.parse_pixels(obj) ⇒ Object

helpers


351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/pixelart/image.rb', line 351

def self.parse_pixels( obj )
  pixels = []
  if obj.is_a?( Array )  ## assume array of string (lines)
      lines = obj
      lines.each do |line|
        ##  convert (string) line into indidual chars
        pixels << line.each_char.reduce( [] ) { |mem, c| mem << c; mem }
      end
  else  ## assume it's a (multi-line) string (with newlines)
        ##  assert and throw ArgumentError if not? - why? why not?
      txt = obj
      txt.each_line do |line|
        line = line.strip
        next if line.start_with?( '#' ) || line.empty?   ## note: allow comments and empty lines

        ## note: allow multiple spaces or tabs
        ##   to separate pixel codes
        ##  e.g.   o o o o o o o o o o o o dg lg w w lg w lg lg dg dg w w  lg dg o o o o o o o o o o o
        ##    or
        pixels << line.split( /[ \t]+/ )
     end
  end
  pixels
end