def test_color_property(self): """Color special handling""" stl = Style("fill-opacity:0.7;fill:red;") self.assertEqual(stl.get_color('fill').alpha, 0.7) self.assertEqual(str(stl.get_color('fill')), 'rgba(255, 0, 0, 0.7)') stl.set_color('rgba(0, 127, 0, 0.5)', 'stroke') self.assertEqual( str(stl), 'fill-opacity:0.7;fill:red;stroke-opacity:0.5;stroke:#007f00')
def _create_styles(self, n): 'Return a style to use for the generated objects.' # Use either the first or the last element's stroke for line caps, # stroke widths, etc. fstyle = self.svg.selection.first().style lstyle = self.svg.selection[-1].style if self.options.stroke_type == 'last_sel': style = Style(lstyle) else: style = Style(fstyle) # Apply the specified fill color. if self.options.fill_type == 'first_sel': fcolor = fstyle.get_color('fill') style.set_color(fcolor, 'fill') elif self.options.fill_type == 'last_sel': fcolor = lstyle.get_color('fill') style.set_color(fcolor, 'fill') elif self.options.fill_type == 'specified': style.set_color(self.options.fill_color, 'fill') elif self.options.fill_type == 'random': pass # Handled below else: sys.exit( inkex.utils.errormsg( _('Internal error: Unrecognized fill type "%s".')) % self.options.fill_type) # Apply the specified stroke color. if self.options.stroke_type == 'first_sel': scolor = fstyle.get_color('stroke') style.set_color(scolor, 'stroke') elif self.options.stroke_type == 'last_sel': scolor = lstyle.get_color('stroke') style.set_color(scolor, 'stroke') elif self.options.stroke_type == 'specified': style.set_color(self.options.stroke_color, 'stroke') elif self.options.stroke_type == 'random': pass # Handled below else: sys.exit( inkex.utils.errormsg( _('Internal error: Unrecognized stroke type "%s".')) % self.options.stroke_type) # Produce n copies of the style. styles = [Style(style) for i in range(n)] if self.options.fill_type == 'random': for s in styles: r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) s.set_color('#%02x%02x%02x' % (r, g, b), 'fill') s['fill-opacity'] = 255 if self.options.stroke_type == 'random': for s in styles: r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) s.set_color('#%02x%02x%02x' % (r, g, b), 'stroke') s['stroke-opacity'] = 255 # Return the list of styles. return [str(s) for s in styles]