Code

2144955023a1c13bd727ba1cbf89df0cfff8ab29
[inkscape.git] / share / extensions / coloreffect.py
1 #!/usr/bin/env python \r
2 '''\r
3 Copyright (C) 2006 Jos Hirth, kaioa.com\r
4 \r
5 This program is free software; you can redistribute it and/or modify\r
6 it under the terms of the GNU General Public License as published by\r
7 the Free Software Foundation; either version 2 of the License, or\r
8 (at your option) any later version.\r
9 \r
10 This program is distributed in the hope that it will be useful,\r
11 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 GNU General Public License for more details.\r
14 \r
15 You should have received a copy of the GNU General Public License\r
16 along with this program; if not, write to the Free Software\r
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
18 '''\r
19 import sys, copy, optparse, simplestyle, inkex\r
20 \r
21 import xml.xpath\r
22 \r
23 color_props_fill=('fill:','stop-color:','flood-color:','lighting-color:')\r
24 color_props_stroke=('stroke:',)\r
25 color_props = color_props_fill + color_props_stroke\r
26 \r
27 class ColorEffect(inkex.Effect):\r
28   def __init__(self):\r
29     inkex.Effect.__init__(self,use_minidom=True)\r
30 \r
31   def effect(self):\r
32     if len(self.selected)==0:\r
33       self.getAttribs(self.document)\r
34     else:\r
35       for id,node in self.selected.iteritems():\r
36         self.getAttribs(node)\r
37 \r
38   def getAttribs(self,node):\r
39     self.changeStyle(node)\r
40     if node.hasChildNodes():\r
41       childs=node.childNodes\r
42       for child in childs:\r
43         self.getAttribs(child)\r
44   \r
45   def changeStyle(self,node):\r
46     if node.hasAttributes():\r
47       style=node.getAttribute('style') # fixme: this will break for presentation attributes!\r
48       if style!='':\r
49         #inkex.debug('old style:'+style)\r
50         styles=style.split(';')\r
51         for i in range(len(styles)):\r
52           for c in range(len(color_props)):\r
53             if styles[i].startswith(color_props[c]):\r
54               styles[i]=color_props[c]+self.process_prop(styles[i][len(color_props[c]):])\r
55         #inkex.debug('new style:'+';'.join(styles))\r
56         node.setAttribute('style',';'.join(styles))\r
57   '''\r
58   def changeStyle(self,node):\r
59     if node.hasAttributes():\r
60       sa=node.getAttribute('style')\r
61       if sa!='':\r
62         debug(sa)\r
63         styles=simplestyle.parseStyle(sa)\r
64         for c in range(len(colortags)):\r
65           if colortags[c] in styles.keys():\r
66             styles[colortags[c]]=self.process_prop(styles[colortags[c]])\r
67         node.setAttribute('style',simplestyle.formatStyle(styles))\r
68   '''        \r
69   def process_prop(self,col):\r
70     #debug('got:'+col)\r
71     if simplestyle.isColor(col):\r
72       c=simplestyle.parseColor(col)\r
73       col='#'+self.colmod(c[0],c[1],c[2])\r
74       #debug('made:'+col)\r
75     if col.startswith('url(#'):\r
76         id = col[len('url(#'):col.find(')')]\r
77         #inkex.debug('ID:' + id )\r
78         path = '//*[@id="%s"]' % id\r
79         for node in xml.xpath.Evaluate(path,self.document):\r
80           self.process_gradient(node)\r
81     return col\r
82 \r
83   def process_gradient(self, node):\r
84     self.changeStyle(node)\r
85     if node.hasChildNodes():\r
86       for child in node.childNodes:\r
87         self.process_gradient(child)\r
88     if node.hasAttributes():                            \r
89       href=node.getAttribute('xlink:href')\r
90       if href.startswith('#'):\r
91         id = href[len('#'):len(href)]\r
92         #inkex.debug('ID:' + id )\r
93         path = '//*[@id="%s"]' % id\r
94         for node in xml.xpath.Evaluate(path,self.document):\r
95           self.process_gradient(node)\r
96  \r
97   def colmod(self,r,g,b):\r
98     pass\r