Skip to content

Commit 7d2bf22

Browse files
committed
add manually selected layer colors
1 parent b2535c2 commit 7d2bf22

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

elkplot/renderer.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,15 @@ def render(
3333
height: float,
3434
dpi: float = 64,
3535
bg_color: tuple[int, int, int] = (0, 0, 0),
36+
layer_colors: Optional[list[tuple[int, int, int]]] = None,
3637
) -> None:
37-
"""
38-
NOTE: You will probably not want to call this directly and instead use elkplot.draw
39-
Displays a preview of what the plotter will draw. Each layer is rendered in a different color. The first 8 layers'
40-
colors have been chosen with maximum distinguishability in mind. If (for some reason) you need more than 8 layers,
41-
subsequent layers' colors are chosen randomly and no guarantees are made about legibility.
42-
:param drawings: A list of MultiLineStrings, one per layer to be drawn
43-
:param width: The width of the page (in inches)
44-
:param height: The height of the page (in inches)
45-
:param dpi: How large would you like the preview shown in screen pixels per plotter-inch
46-
:return:
47-
"""
4838
pygame.init()
4939
win = pygame.display.set_mode((int(dpi * width), int(dpi * height)))
5040
rng = np.random.default_rng()
51-
my_colors = COLORS + [_random_color(rng) for _ in range(len(layers) - len(COLORS))]
41+
if layer_colors is not None:
42+
my_colors = layer_colors + [_random_color(rng) for _ in range(len(layers) - len(layer_colors))]
43+
else:
44+
my_colors = COLORS + [_random_color(rng) for _ in range(len(layers) - len(COLORS))]
5245
run = True
5346
first_run = True
5447
while run:

elkplot/util.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def draw(
2323
plot: bool = True,
2424
retrace: int = 1,
2525
device: Optional[Device] = None,
26-
bg_color: tuple[float, ...] = (0, 0, 0),
26+
bg_color: tuple[int, int, int] = (0, 0, 0),
27+
layer_colors: Optional[list[tuple[int, int, int]]] = None,
2728
) -> None:
2829
"""
2930
Visualize and/or plot a given drawing. Automatically pauses the plotter between layers to allow for changing pens.
@@ -47,11 +48,11 @@ def draw(
4748
draw that layer a second time, then either finish or prompt you to change pens.
4849
device: The AxiDraw config to which the plot should be sent. If excluded, a `Device` with all default settings
4950
will be used.
51+
bg_color: The color of the background on which the preview is rendered (r, g, b)
52+
layer_colors: A list of colors for each layer - good for previewing pen colors. Each color given as (r,g,b)
5053
"""
5154
if isinstance(drawing, shapely.GeometryCollection):
52-
layers = [
53-
flatten_geometry(layer) for layer in shapely.get_parts(drawing)
54-
]
55+
layers = [flatten_geometry(layer) for layer in shapely.get_parts(drawing)]
5556
elif isinstance(drawing, list):
5657
layers = [flatten_geometry(layer) for layer in drawing]
5758
else:
@@ -70,7 +71,14 @@ def draw(
7071
warnings.warn("THIS DRAWING GOES OUT OF BOUNDS!")
7172

7273
if preview:
73-
render(layers, width, height, preview_dpi, bg_color=bg_color)
74+
render(
75+
layers,
76+
width,
77+
height,
78+
preview_dpi,
79+
bg_color=bg_color,
80+
layer_colors=layer_colors,
81+
)
7482
if not plot:
7583
return
7684
min_x = min([layer.bounds[0] for layer in layers])

0 commit comments

Comments
 (0)