Implement proper color handling

Avoid accessing Boxes.ctx (the cairo context) all over the code.
Have named colors
This commit is contained in:
Wayne Wylupski 2019-02-02 21:07:32 +01:00 committed by Florian Festi
parent c573914bcd
commit abacdd486e
4 changed files with 24 additions and 14 deletions

6
boxes/Color.py Normal file
View File

@ -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 ]

View File

@ -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 = {

View File

@ -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):

View File

@ -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()