| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
''' |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
import simplestyle |
|---|
| 23 |
import pdb |
|---|
| 24 |
|
|---|
| 25 |
color_props_fill=('fill:','stop-color:','flood-color:','lighting-color:') |
|---|
| 26 |
color_props_stroke=('stroke:',) |
|---|
| 27 |
color_props = color_props_fill + color_props_stroke |
|---|
| 28 |
opacity_props = 'opacity:' |
|---|
| 29 |
|
|---|
| 30 |
def apply_colmod(self, nodeList, hsbcoeffs, opacoeff, hsbo_cf): |
|---|
| 31 |
for n in nodeList: |
|---|
| 32 |
changeStyle(self, n, hsbcoeffs, opacoeff, hsbo_cf) |
|---|
| 33 |
apply_colmod(self, n, hsbcoeffs, opacoeff, hsbo_cf) |
|---|
| 34 |
|
|---|
| 35 |
def changeStyle(self, node, hsbcoeffs, opacoeff, hsbo_cf): |
|---|
| 36 |
if node.attrib.has_key('style'): |
|---|
| 37 |
style=node.get('style') |
|---|
| 38 |
if style!='': |
|---|
| 39 |
|
|---|
| 40 |
styles=style.split(';') |
|---|
| 41 |
for i in range(len(styles)): |
|---|
| 42 |
if styles[i].startswith(opacity_props): |
|---|
| 43 |
styles[i]=opacity_props+str(opamod(float(styles[i][len(opacity_props):]), opacoeff, hsbo_cf)) |
|---|
| 44 |
else: |
|---|
| 45 |
for c in range(len(color_props)): |
|---|
| 46 |
if styles[i].startswith(color_props[c]): |
|---|
| 47 |
styles[i]=color_props[c]+process_prop(self, styles[i][len(color_props[c]):], hsbcoeffs, hsbo_cf) |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
node.set('style',';'.join(styles)) |
|---|
| 51 |
|
|---|
| 52 |
def process_prop(self, col, hsbcoeffs, hsbo_cf): |
|---|
| 53 |
|
|---|
| 54 |
if simplestyle.isColor(col): |
|---|
| 55 |
c=simplestyle.parseColor(col) |
|---|
| 56 |
col='#'+colmod(c[0],c[1],c[2], hsbcoeffs, hsbo_cf) |
|---|
| 57 |
|
|---|
| 58 |
if col.startswith('url(#'): |
|---|
| 59 |
id = col[len('url(#'):col.find(')')] |
|---|
| 60 |
newid = self.newid(self.svg_prefixfromtag(id)) |
|---|
| 61 |
|
|---|
| 62 |
path = '//*[@id="%s"]' % id |
|---|
| 63 |
for node in self.document.xpath(path, namespaces=inkex.NSS): |
|---|
| 64 |
process_gradient(self, node, newid, hsbcoeffs) |
|---|
| 65 |
col = 'url(#%s)' % newid |
|---|
| 66 |
return col |
|---|
| 67 |
|
|---|
| 68 |
def process_gradient(self, node, newid, hsbcoeffs, hsbo_cf): |
|---|
| 69 |
newnode = copy.deepcopy(node) |
|---|
| 70 |
newnode.set('id', newid) |
|---|
| 71 |
node.getparent().append(newnode) |
|---|
| 72 |
changeStyle(self, newnode, hsbcoeffs, opacoeff, hsbo_cf) |
|---|
| 73 |
for child in newnode: |
|---|
| 74 |
changeStyle(self, child, hsbcoeffs, opacoeff, hsbo_cf) |
|---|
| 75 |
xlink = inkex.addNS('href','xlink') |
|---|
| 76 |
if newnode.attrib.has_key(xlink): |
|---|
| 77 |
href=newnode.get(xlink) |
|---|
| 78 |
if href.startswith('#'): |
|---|
| 79 |
id = href[len('#'):len(href)] |
|---|
| 80 |
|
|---|
| 81 |
newhref = self.newid(self.svg_prefixfromtag(id)) |
|---|
| 82 |
newnode.set(xlink, '#%s' % newhref) |
|---|
| 83 |
path = '//*[@id="%s"]' % id |
|---|
| 84 |
for node in self.document.xpath(path, namespaces=inkex.NSS): |
|---|
| 85 |
process_gradient(node, newhref) |
|---|
| 86 |
|
|---|
| 87 |
hsb_chars = {0: 'h', 1: 's', 2: 'b'} |
|---|
| 88 |
def colmod(r,g,b, (hue_coeff, sat_coeff, bri_coeff), hsbo_cf): |
|---|
| 89 |
hsl = rgb_to_hsl(r/255.0, g/255.0, b/255.0) |
|---|
| 90 |
hsl[0] = hsl[0]+hue_coeff |
|---|
| 91 |
hsl[1] = hsl[1]+sat_coeff |
|---|
| 92 |
hsl[2] = hsl[2]+bri_coeff |
|---|
| 93 |
for i in range(len(hsl)): |
|---|
| 94 |
fidx = hsb_chars[i]+'f' |
|---|
| 95 |
cidx = hsb_chars[i]+'c' |
|---|
| 96 |
if hsbo_cf[fidx] >= 0 and hsl[i] <= hsbo_cf[fidx]: |
|---|
| 97 |
hsl[i] = hsbo_cf[fidx] |
|---|
| 98 |
if hsbo_cf[cidx] >= 0 and hsl[i] >= hsbo_cf[cidx]: |
|---|
| 99 |
hsl[i] = hsbo_cf[cidx] |
|---|
| 100 |
if abs(hsl[i]) > 1.0: |
|---|
| 101 |
hsl[i] -= int(hsl[i]) |
|---|
| 102 |
if hsl[i] < 0: |
|---|
| 103 |
hsl[i]=1.0+hsl[i] |
|---|
| 104 |
rgb = hsl_to_rgb(hsl[0], hsl[1], hsl[2]) |
|---|
| 105 |
return '%02x%02x%02x' % (rgb[0]*255, rgb[1]*255, rgb[2]*255) |
|---|
| 106 |
|
|---|
| 107 |
def opamod(opa, opacoeff, hsbo_cf): |
|---|
| 108 |
opa+=opacoeff |
|---|
| 109 |
if hsbo_cf['of'] >=0 and opa <= hsbo_cf['of']: |
|---|
| 110 |
opa = hsbo_cf['of'] |
|---|
| 111 |
if hsbo_cf['oc'] >=0 and opa >= hsbo_cf['oc']: |
|---|
| 112 |
opa = hsbo_cf['oc'] |
|---|
| 113 |
if abs(opa) > 1.0: |
|---|
| 114 |
opa -= int(opa) |
|---|
| 115 |
if opa < 0: |
|---|
| 116 |
opa=1.0+opa |
|---|
| 117 |
return opa |
|---|
| 118 |
|
|---|
| 119 |
def rgb_to_hsl(r, g, b): |
|---|
| 120 |
rgb_max = max (max (r, g), b) |
|---|
| 121 |
rgb_min = min (min (r, g), b) |
|---|
| 122 |
delta = rgb_max - rgb_min |
|---|
| 123 |
hsl = [0.0, 0.0, 0.0] |
|---|
| 124 |
hsl[2] = (rgb_max + rgb_min)/2.0 |
|---|
| 125 |
if delta == 0: |
|---|
| 126 |
hsl[0] = 0.0 |
|---|
| 127 |
hsl[1] = 0.0 |
|---|
| 128 |
else: |
|---|
| 129 |
if hsl[2] <= 0.5: |
|---|
| 130 |
hsl[1] = delta / (rgb_max + rgb_min) |
|---|
| 131 |
else: |
|---|
| 132 |
hsl[1] = delta / (2 - rgb_max - rgb_min) |
|---|
| 133 |
if r == rgb_max: |
|---|
| 134 |
hsl[0] = (g - b) / delta |
|---|
| 135 |
else: |
|---|
| 136 |
if g == rgb_max: |
|---|
| 137 |
hsl[0] = 2.0 + (b - r) / delta |
|---|
| 138 |
else: |
|---|
| 139 |
if b == rgb_max: |
|---|
| 140 |
hsl[0] = 4.0 + (r - g) / delta |
|---|
| 141 |
hsl[0] = hsl[0] / 6.0 |
|---|
| 142 |
if hsl[0] < 0: |
|---|
| 143 |
hsl[0] = hsl[0] + 1 |
|---|
| 144 |
if hsl[0] > 1: |
|---|
| 145 |
hsl[0] = hsl[0] - 1 |
|---|
| 146 |
return hsl |
|---|
| 147 |
|
|---|
| 148 |
def hue_2_rgb (v1, v2, h): |
|---|
| 149 |
if h < 0: |
|---|
| 150 |
h += 6.0 |
|---|
| 151 |
if h > 6: |
|---|
| 152 |
h -= 6.0 |
|---|
| 153 |
if h < 1: |
|---|
| 154 |
return v1 + (v2 - v1) * h |
|---|
| 155 |
if h < 3: |
|---|
| 156 |
return v2 |
|---|
| 157 |
if h < 4: |
|---|
| 158 |
return v1 + (v2 - v1) * (4 - h) |
|---|
| 159 |
return v1 |
|---|
| 160 |
|
|---|
| 161 |
def hsl_to_rgb (h, s, l): |
|---|
| 162 |
rgb = [0, 0, 0] |
|---|
| 163 |
if s == 0: |
|---|
| 164 |
rgb[0] = l |
|---|
| 165 |
rgb[1] = l |
|---|
| 166 |
rgb[2] = l |
|---|
| 167 |
else: |
|---|
| 168 |
if l < 0.5: |
|---|
| 169 |
v2 = l * (1 + s) |
|---|
| 170 |
else: |
|---|
| 171 |
v2 = l + s - l*s |
|---|
| 172 |
v1 = 2*l - v2 |
|---|
| 173 |
rgb[0] = hue_2_rgb (v1, v2, h*6 + 2.0) |
|---|
| 174 |
rgb[1] = hue_2_rgb (v1, v2, h*6) |
|---|
| 175 |
rgb[2] = hue_2_rgb (v1, v2, h*6 - 2.0) |
|---|
| 176 |
return rgb |
|---|