3

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.

max
  • 347
  • 3
  • 14

1 Answers1

6

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
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • This did what i need... Thanks Guardiario.. :) But there are few SVG's which has colors which doesn't comes under style tag (@style,"fill")... Is it possible to handled them dynamically..? – max Mar 20 '12 at 09:55
  • You're welcome. Don't forget to vote it up and mark it correct. – pguardiario Mar 20 '12 at 09:57