diff --git a/boxes/Color.py b/boxes/Color.py new file mode 100644 index 0000000..1655e81 --- /dev/null +++ b/boxes/Color.py @@ -0,0 +1,6 @@ +class Color: + BLACK = [ 0.0, 0.0, 0.0 ] + BLUE = [ 0.0, 0.0, 1.0 ] + GREEN = [ 0.0, 1.0, 0.0 ] + RED = [ 1.0, 0.0, 0.0 ] + WHITE = [ 1.0, 1.0, 1.0 ] diff --git a/boxes/__init__.py b/boxes/__init__.py index d461642..f2dc20f 100755 --- a/boxes/__init__.py +++ b/boxes/__init__.py @@ -36,7 +36,7 @@ from boxes import svgutil from boxes import gears from boxes import pulley from boxes import parts - +from boxes.Color import * ### Helpers @@ -49,7 +49,6 @@ def dist(dx, dy): """ return (dx * dx + dy * dy) ** 0.5 - def restore(func): """ Wrapper: Restore coordiantes after function @@ -79,10 +78,10 @@ def holeCol(func): @wraps(func) def f(self, *args, **kw): self.ctx.stroke() - self.ctx.set_source_rgb(0.0, 0.0, 1.0) - func(self, *args, **kw) - self.ctx.stroke() - self.ctx.set_source_rgb(0.0, 0.0, 0.0) + with self.saved_context(): + self.set_source_color(Color.BLUE) + func(self, *args, **kw) + self.ctx.stroke() return f @@ -266,18 +265,25 @@ class Boxes: finally: cr.restore() + def set_source_color(self, color): + """ + Sets the color of the pen. + """ + self.ctx.set_source_rgb(*color) + def open(self): """ Prepare for rendering Call this function from your .render() method """ - self.spacing = 2 * self.burn + 0.5 * self.thickness self.bedBoltSettings = (3, 5.5, 2, 20, 15) # d, d_nut, h_nut, l, l1 self.hexHolesSettings = (5, 3, 'circle') # r, dist, style self.surface, self.ctx = self.formats.getSurface(self.format, self.output) self.ctx.set_line_width(max(2 * self.burn, 0.05)) + self.set_source_color(Color.BLACK) + self.spacing = 2 * self.burn + 0.5 * self.thickness self.ctx.select_font_face("sans-serif") self._buildObjects() if self.reference: @@ -1148,15 +1154,15 @@ class Boxes: raise ValueError("Unknown alignment: %s" % align) self.ctx.stroke() - self.ctx.set_source_rgb(1.0, 1.0, 1.0) + self.set_source_color(Color.WHITE) self.ctx.rectangle(0, 0, width, height) self.ctx.stroke() - self.ctx.set_source_rgb(*color) + self.set_source_color(color) self.ctx.scale(1, -1) for line in reversed(text): self.ctx.show_text(line) self.moveTo(0, 1.4 * -lheight) - self.ctx.set_source_rgb(0.0, 0.0, 0.0) + self.set_source_color(Color.BLACK) self.ctx.set_font_size(10) tx_sizes = { diff --git a/boxes/formats.py b/boxes/formats.py index d2e2218..455621c 100644 --- a/boxes/formats.py +++ b/boxes/formats.py @@ -91,8 +91,6 @@ class Formats: ctx.translate(0, height) ctx.scale(mm2pt, -mm2pt) - ctx.set_source_rgb(0.0, 0.0, 0.0) - return surface, ctx def convert(self, filename, fmt): diff --git a/boxes/generators/universalbox.py b/boxes/generators/universalbox.py index 9d5f04d..c51511e 100644 --- a/boxes/generators/universalbox.py +++ b/boxes/generators/universalbox.py @@ -79,10 +79,10 @@ class UniversalBox(_TopEdge, _ChestLid): self.bottom_edge != "e"): self.rectangularWall(x, y, "ffff", move="left only") if self.top_edge in "fF": - self.ctx.set_source_rgb(1., 0, 0) + self.set_source_color(Color.RED) self.rectangularWall(x+4*t, y+4*t, callback=[ lambda:self.top_hole(x, y, self.top_edge)], move="right") - self.ctx.set_source_rgb(0, 0, 0) + self.set_source_color(Color.BLACK) self.drawAddOnLid(x, y, self.lid) self.ctx.restore()