Style scopes

Saving and restoring fill, stroke and the other drawing style.

style

style(s=None, /, **settings)

Sets the drawing style, for good or for a with block.

Called on its own, it applies s (a Style, typically from current_style), then the keyword settings, and they stay applied. Used with with, the style from before the call comes back when the block ends, even through an exception or a return::

kiln.style(stroke_width=2)            # from now on
with kiln.style(fill=None, stroke="red"):
    kiln.circle(50, 50, 20)           # outlined in red
# filled again, in the colour from before

with kiln.style(): with no settings only saves and restores. The saved style lives in the returned object, not on a stack, so a call without with leaves nothing behind.

The settings are the fields of Style, each named as its setter function and checked as it checks it: fill and stroke take any colour fill() takes as one argument, or None; tint a colour; stroke_width is clamped to 0 or more and curve_resolution to 1 or more; rect_mode, blend and winding become their enum member, and blend=None is Blend.REPLACE, as blend(None) is.

Parameters

Name Type Description Default
s Style | None A Style to apply first, or None to start from the current one. It is copied. None
**settings Style fields to set: fill, stroke, stroke_width, tint, blend, rect_mode, winding, curve_resolution. {}

Returns

Name Type Description
_StyleScope A context manager that restores the style from before the call.

Raises

Name Type Description
TypeError A setting is not a Style field, or a colour is not a colour.
ValueError A mode is not a member of its enum, or a colour string is unknown.

current_style

current_style()

Returns a copy of the current drawing style.

Returns

Name Type Description
Style A Style; editing it does not change the current style until you
Style pass it to style.