rubysvgxml-parsingnokogirihpricot

Hpricot / nokogiri - Parse SVG / XML file to get colors used


I need help in finding all colors used in an SVG (XML) file.

For example, i need the list of colors used in the image http://upload.wikimedia.org/wikipedia/commons/e/e9/Pepsi_logo_2008.svg

I was trying with hpricot / nokogiri gems to do something like,

--> doc.search("['fill:']") .. etc

But i am unable to get the proper result.

If there is any command line tool for linux like inkscape to get the colors it will be helpful.


Solution

  • Something like this should work:

    require 'nokogiri'
    require 'open-uri'
    
    url = 'http://upload.wikimedia.org/wikipedia/commons/e/e9/Pepsi_logo_2008.svg'
    doc = Nokogiri::HTML open(url)
    puts doc.xpath('//*[contains(@style,"fill")]').map{|e| e[:style][/fill:([^;]*)/, 1]}.uniq