Class: Colorant::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/colorant/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(*args) ⇒ Object



5
6
7
# File 'lib/colorant/parser.rb', line 5

def parse(*args)
  new.parse(*args)
end

Instance Method Details

#parse(raw_data) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/colorant/parser.rb', line 10

def parse(raw_data)
  collection = []
  raw_data.split("\n").map(&:strip).each do |line|
    line.match /^(\d+):.*rgb\((.*)%,(.*)%,(.*)%\)$/
    freq = $1.to_i
    red, green, blue = [$2,$3,$4].map do |color|
      color.to_f  / 100.to_f
    end
    collection << { :freq => freq,
                    :red => percent_to_number(red),
                    :green => percent_to_number(green),
                    :blue => percent_to_number(blue) }
  end
  total = collection.map{|element| element[:freq]}.inject(:+)
  collection.each{|element| element[:freq] = number_to_percent(element[:freq], total)}
  collection
end