Code

Removing seemingly dead/unused extension files that are causing a menu problem -...
[inkscape.git] / share / extensions / coloreffect.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2006 Jos Hirth, kaioa.com
4 Copyright (C) 2007 Aaron C. Spike
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 '''
20 import sys, copy, optparse, simplestyle, inkex, copy
22 import random
24 color_props_fill=('fill:','stop-color:','flood-color:','lighting-color:')
25 color_props_stroke=('stroke:',)
26 color_props = color_props_fill + color_props_stroke
29 class ColorEffect(inkex.Effect):
30   def __init__(self):
31     inkex.Effect.__init__(self)
32     self.visited = []
34   def effect(self):
35     if len(self.selected)==0:
36       self.getAttribs(self.document.getroot())
37     else:
38       for id,node in self.selected.iteritems():
39         self.getAttribs(node)
41   def getAttribs(self,node):
42     self.changeStyle(node)
43     for child in node:
44       self.getAttribs(child)
45   
46   def changeStyle(self,node):
47     if node.attrib.has_key('style'):
48       style=node.get('style') # fixme: this will break for presentation attributes!
49       if style!='':
50         #inkex.debug('old style:'+style)
51         styles=style.split(';')
52         for i in range(len(styles)):
53           for c in range(len(color_props)):
54             if styles[i].startswith(color_props[c]):
55               styles[i]=color_props[c]+self.process_prop(styles[i][len(color_props[c]):])
56         #inkex.debug('new style:'+';'.join(styles))
57         node.set('style',';'.join(styles))
59   def process_prop(self,col):
60     #debug('got:'+col)
61     if simplestyle.isColor(col):
62       c=simplestyle.parseColor(col)
63       col='#'+self.colmod(c[0],c[1],c[2])
64       #debug('made:'+col)
65     if col.startswith('url(#'):
66       id = col[len('url(#'):col.find(')')]
67       newid = '%s-%d' % (id, int(random.random() * 1000))
68       #inkex.debug('ID:' + id )
69       path = '//*[@id="%s"]' % id
70       for node in self.document.xpath(path, namespaces=inkex.NSS):
71         self.process_gradient(node, newid)
72       col = 'url(#%s)' % newid
73     return col
75   def process_gradient(self, node, newid):
76     #if node.hasAttributes():                           
77        #this_id=node.getAttribute('id')
78        #if this_id in self.visited:
79          ## prevent multiple processing of the same gradient if it is used by more than one selected object
80          ##inkex.debug("already had: " + this_id)
81          #return
82        #self.visited.append(this_id)
83        #inkex.debug("visited: " + str(self.visited))
84     newnode = copy.deepcopy(node)
85     newnode.set('id', newid)
86     node.getparent().append(newnode)
87     self.changeStyle(newnode)
88     for child in newnode:
89       self.changeStyle(child)
90     xlink = inkex.addNS('href','xlink')
91     if newnode.attrib.has_key(xlink):
92       href=newnode.get(xlink)
93       if href.startswith('#'):
94         id = href[len('#'):len(href)]
95         #inkex.debug('ID:' + id )
96         newhref = '%s-%d' % (id, int(random.random() * 1000))
97         newnode.set(xlink, '#%s' % newhref)
98         path = '//*[@id="%s"]' % id
99         for node in self.document.xpath(path, namespaces=inkex.NSS):
100           self.process_gradient(node, newhref)
101  
102   def colmod(self,r,g,b):
103     pass
105   def rgb_to_hsl(self,r, g, b):
106     rgb_max = max (max (r, g), b)
107     rgb_min = min (min (r, g), b)
108     delta = rgb_max - rgb_min
109     hsl = [0.0, 0.0, 0.0]
110     hsl[2] = (rgb_max + rgb_min)/2.0
111     if delta == 0:
112         hsl[0] = 0.0
113         hsl[1] = 0.0
114     else:
115         if hsl[2] <= 0.5:
116             hsl[1] = delta / (rgb_max + rgb_min)
117         else:
118             hsl[1] = delta / (2 - rgb_max - rgb_min)
119         if r == rgb_max:
120             hsl[0] = (g - b) / delta
121         else:
122             if g == rgb_max:
123                 hsl[0] = 2.0 + (b - r) / delta
124             else:
125                 if b == rgb_max:
126                     hsl[0] = 4.0 + (r - g) / delta
127         hsl[0] = hsl[0] / 6.0
128         if hsl[0] < 0:
129             hsl[0] = hsl[0] + 1
130         if hsl[0] > 1:
131             hsl[0] = hsl[0] - 1
132     return hsl
134   def hue_2_rgb (self, v1, v2, h):
135     if h < 0:
136         h += 6.0
137     if h > 6:
138         h -= 6.0
139     if h < 1:
140         return v1 + (v2 - v1) * h
141     if h < 3:
142         return v2
143     if h < 4:
144         return v1 + (v2 - v1) * (4 - h)
145     return v1
147   def hsl_to_rgb (self,h, s, l):
148     rgb = [0, 0, 0]
149     if s == 0:
150         rgb[0] = l
151         rgb[1] = l
152         rgb[2] = l
153     else:
154         if l < 0.5:
155             v2 = l * (1 + s)
156         else:
157             v2 = l + s - l*s
158         v1 = 2*l - v2
159         rgb[0] = self.hue_2_rgb (v1, v2, h*6 + 2.0)
160         rgb[1] = self.hue_2_rgb (v1, v2, h*6)
161         rgb[2] = self.hue_2_rgb (v1, v2, h*6 - 2.0)
162     return rgb