diff options
| author | Nicolas Dufour <nicoduf@yahoo.fr> | 2011-09-01 18:18:14 +0000 |
|---|---|---|
| committer | JazzyNico <nicoduf@yahoo.fr> | 2011-09-01 18:18:14 +0000 |
| commit | eb82c605445c9a3b6d45541bc89904b5af22f519 (patch) | |
| tree | 365d631eaced5b80c79194e5251d68c79999f015 | |
| parent | Extensions. Fix for bug #837962 (Untranslatable and not localized strings in ... (diff) | |
| download | inkscape-eb82c605445c9a3b6d45541bc89904b5af22f519.tar.gz inkscape-eb82c605445c9a3b6d45541bc89904b5af22f519.zip | |
Extensions. Fix for bug #837603 (Simplestyle extension helper breaks on spaces in styles) thanks to nikitakit.
Extensions. Fix for bug #298528 (parseColor() in simplestyle.py can't parse colors of type rgb(xx.x%,xx.x%,xx.xx%)) thanks to Kjell Magne Fauske.
Extensions. New test file for simplestyle.py.
(bzr r10610)
| -rwxr-xr-x | share/extensions/simplestyle.py | 22 | ||||
| -rwxr-xr-x | share/extensions/test/simplestyle.test.py | 63 |
2 files changed, 79 insertions, 6 deletions
diff --git a/share/extensions/simplestyle.py b/share/extensions/simplestyle.py index feb0b0bcd..c97d57e75 100755 --- a/share/extensions/simplestyle.py +++ b/share/extensions/simplestyle.py @@ -176,10 +176,12 @@ def parseStyle(s): if s is None: return {} else: - return dict([i.split(":") for i in s.split(";") if len(i)]) + return dict([[x.strip() for x in i.split(":")] for i in s.split(";") if len(i)]) + def formatStyle(a): """Format an inline style attribute from a dictionary""" return ";".join([att+":"+str(val) for att,val in a.iteritems()]) + def isColor(c): """Determine if its a color we can use. If not, leave it unchanged.""" if c.startswith('#') and (len(c)==4 or len(c)==7): @@ -205,27 +207,35 @@ def parseColor(c): if len(numbers) == 3: for num in numbers: if num.endswith(r'%'): - converted_numbers.append( int(int(num[0:-1])*255/100)) + converted_numbers.append(int(float(num[0:-1])*255/100)) else: converted_numbers.append(int(num)) return tuple(converted_numbers) else: return (0,0,0) - - r=int(c[1:3],16) - g=int(c[3:5],16) - b=int(c[5:],16) + try: + r=int(c[1:3],16) + g=int(c[3:5],16) + b=int(c[5:],16) + except: + # unknown color ... + # Return a default color. Maybe not the best thing to do but probably + # better than raising an exception. + return(0,0,0) return (r,g,b) def formatColoria(a): """int array to #rrggbb""" return '#%02x%02x%02x' % (a[0],a[1],a[2]) + def formatColorfa(a): """float array to #rrggbb""" return '#%02x%02x%02x' % (int(round(a[0]*255)),int(round(a[1]*255)),int(round(a[2]*255))) + def formatColor3i(r,g,b): """3 ints to #rrggbb""" return '#%02x%02x%02x' % (r,g,b) + def formatColor3f(r,g,b): """3 floats to #rrggbb""" return '#%02x%02x%02x' % (int(round(r*255)),int(round(g*255)),int(round(b*255))) diff --git a/share/extensions/test/simplestyle.test.py b/share/extensions/test/simplestyle.test.py new file mode 100755 index 000000000..3c75ac43a --- /dev/null +++ b/share/extensions/test/simplestyle.test.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +import unittest, sys
+sys.path.append('..') # this line allows to import the extension code
+
+from simplestyle import parseColor, parseStyle
+
+
+class ParseColorTest(unittest.TestCase):
+ """Test for single transformations"""
+ def test_namedcolor(self):
+ "Parse 'red'"
+ col = parseColor('red')
+ self.failUnlessEqual((255,0,0),col)
+
+ def test_hexcolor4digit(self):
+ "Parse '#ff0102'"
+ col = parseColor('#ff0102')
+ self.failUnlessEqual((255,1,2),col)
+
+ def test_hexcolor3digit(self):
+ "Parse '#fff'"
+ col = parseColor('#fff')
+ self.failUnlessEqual((255,255,255),col)
+
+ def test_rgbcolorint(self):
+ "Parse 'rgb(255,255,255)'"
+ col = parseColor('rgb(255,255,255)')
+ self.failUnlessEqual((255,255,255),col)
+
+ def test_rgbcolorpercent(self):
+ "Parse 'rgb(100%,100%,100%)'"
+ col = parseColor('rgb(100%,100%,100%)')
+ self.failUnlessEqual((255,255,255),col)
+
+ def test_rgbcolorpercent2(self):
+ "Parse 'rgb(100%,100%,100%)'"
+ col = parseColor('rgb(50%,0%,1%)')
+ self.failUnlessEqual((127,0,2),col)
+
+ def test_rgbcolorpercentdecimal(self):
+ "Parse 'rgb(66.667%,0%,6.667%)'"
+ col = parseColor('rgb(66.667%,0%,6.667%)')
+ self.failUnlessEqual((170, 0, 17),col)
+
+ def test_currentColor(self):
+ "Parse 'currentColor'"
+ col = parseColor('currentColor')
+ self.failUnlessEqual(('currentColor'),col)
+
+ def test_spaceinstyle(self):
+ "Parse 'stop-color: rgb(0,0,0)'"
+ col = parseStyle('stop-color: rgb(0,0,0)')
+ self.failUnlessEqual({'stop-color': 'rgb(0,0,0)'},col)
+
+ #def test_unknowncolor(self):
+ # "Parse 'unknown'"
+ # col = parseColor('unknown')
+ # self.failUnlessEqual((0,0,0),col)
+ #
+
+if __name__ == '__main__':
+ unittest.main() |
