Sha256: 1186d00afa13efba64c659ad531fb9ef16a645030075497abd3bac15e54740f8

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module GridGenerator
  class FaceParser
    COLOURS = { 
      'w' => '#ffffff',
      'y' => '#ffff00',
      'b' => '#0000ff',
      'g' => '#00ff00',
      'r' => '#ff0000',
      'o' => '#ffb000',
      'gr' => '#808080',
      'br' => '#804000',
      's' => '#c2b280', 
      'c' => '#00ffff',
      'l' => '#80ff80',
      'p' => '#800080', 
      'pi' => '#ff8080',
      'bl' => '#404040',
      'fu' => "#f0f0f0", # face up
      'ff' => "#d0d0d0", # face front
      'fr' => "#b0b0b0"  # face right
    } 
  
    OPACITY = {
      full: 1,
      faded: 0.4
    }
  
    def initialize(string)
      @string = string
    end

    def single?
      !@string.include?("\\n")
    end
  
    attr_reader :string

    def parse
      if single?
        parse_char(string)
      else
        parse_array(string)
      end
    end
  
    def parse_char(char)
      if char == '-'
        nil
      else
        colour = COLOURS[char.downcase]
        opacity = OPACITY[(/[[:upper:]]/.match(char) ? :full : :faded)]
        { colour: colour, opacity: opacity }
      end
    end

    def parse_array(str)
      str.split(/\\n/).map do |line|
        line.split(',').map(&:strip).map do |col|
          parse_char(col)
        end
      end 
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grid_generator-0.4.3 lib/grid_generator/face_parser.rb