add color overriding to qsave()
Does not work correct for color BYLAYER see issue
# Copyright (c) 2020, Matthew Broadway
# License: MIT License
import math
-from typing import Iterable, TYPE_CHECKING, Sequence
+from typing import Iterable, TYPE_CHECKING, Sequence, Optional
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
@@ -186,6 +186,8 @@ def _get_path_patch_data(path):
def qsave(layout: 'Layout', filename: str, *,
+ bg: Optional[Color] = None,
+ fg: Optional[Color] = None,
rect: Sequence[float] = (0, 0, 1, 1),
dpi: int = 300) -> None:
""" Quick and simplified render export by matplotlib.
@@ -194,6 +196,9 @@ def qsave(layout: 'Layout', filename: str, *,
layout: modelspace or paperspace layout to export
filename: export filename, file extension determines the format e.g.
"image.png" to save in PNG format.
+ bg: override default background color in hex format #RRGGBB
+ fg: override default foreground color in hex format #RRGGBB,
+ requires also `bg` argument
rect: the dimensions [left, bottom, width, height] of the axes.
All quantities are in fractions of figure width and height.
dpi: image resolution
@@ -206,6 +211,9 @@ def qsave(layout: 'Layout', filename: str, *,
fig: plt.Figure = plt.figure()
ax: plt.Axes = fig.add_axes(rect)
ctx = RenderContext(layout.doc)
+ ctx.set_current_layout(layout)
+ if bg is not None:
+ ctx.current_layout.set_colors(bg, fg)
out = MatplotlibBackend(ax)
Frontend(ctx, out).draw_layout(layout, finalize=True)
fig.savefig(filename, dpi=dpi)
