summaryrefslogtreecommitdiffstats
path: root/share/extensions/export_gimp_palette.py
blob: 25856f4b4a82e8063ce4a0ba1e7442cf2696844f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python 
'''
Author: Jos Hirth, kaioa.com
License: GNU General Public License - http://www.gnu.org/licenses/gpl.html
Warranty: see above
'''

DOCNAME='sodipodi:docname'

# standard library
import sys
# third party
try:
    from xml.dom.minidom import parse
except:
    inkex.errormsg(_('The export_gpl.py module requires PyXML.  Please download the latest version from http://pyxml.sourceforge.net/.'))
    sys.exit()
# local library
import inkex
import simplestyle

inkex.localize()

colortags=(u'fill',u'stroke',u'stop-color',u'flood-color',u'lighting-color')
colors={}

def walk(node):
    checkStyle(node)
    if node.hasChildNodes():
        childs=node.childNodes
        for child in childs:
            walk(child)

def checkStyle(node):
    if hasattr(node,"hasAttributes") and node.hasAttributes():
        sa=node.getAttribute('style')
        if sa!='':
            styles=simplestyle.parseStyle(sa)
            for c in range(len(colortags)):
                if colortags[c] in styles.keys():
                    addColor(styles[colortags[c]])

def addColor(col):
    if simplestyle.isColor(col):
        c=simplestyle.parseColor(col)
        colors['%3i %3i %3i ' % (c[0],c[1],c[2])]=simplestyle.formatColoria(c).upper()

stream = open(sys.argv[-1:][0],'r')
dom = parse(stream)
stream.close()
walk(dom)
print 'GIMP Palette\nName: %s\n#' % (dom.getElementsByTagName('svg')[0].getAttribute(DOCNAME).split('.')[0])

for k,v in sorted(colors.items()):
    print k+v


# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99