virtual_knitting_machine.visualizer.visualizer_elements.visualizer_element module

Module containing the Visualizer Element class and common primitive subclasses.

class Visualizer_Element(x, y, name, **element_kwargs)[source]

Bases: object

Super class for all visualizer element wrappers that support coordinate checking and transformation.

parent

The optional parent element of this element used to find global coordinate position.

Type:

Visualizer_Element | None

__init__(x, y, name, **element_kwargs)[source]
property name: str

Returns: str: The unique name of this element. This will match the id of the svg element being wrapped.

property x: float

Returns: float: The x coordinate of this element relative to its parent (or globally).

property y: float

Returns: float: The y coordinate of this element relative to its parent (or globally).

property global_x: float

Returns: float: The global x coordinate of this element based on its parent’s coordinates.

property global_y: float

Returns: float: The global y coordinate of this element based on its parent’s coordinates.

global_x_position(x)[source]
Parameters:

x (float) – An X-coordinate position relative to this element’s coordinate system.

Returns:

The x-coordinate adjusted to the global coordinate system.

Return type:

float

global_y_position(y)[source]
Parameters:

y (float) – An Y-coordinate position relative to this element’s coordinate system.

Returns:

The y-coordinate adjusted to the global coordinate system.

Return type:

float

add_to_drawing(drawing)[source]

Adds this element to the given svg drawing. :param drawing: The drawing to add. :type drawing: Drawing

static fill_from_stroke(stroke, lighten_factor=0.3)[source]
Parameters:
  • stroke (str) – The color string of the stroke to be lightened for infill.

  • lighten_factor (float, optional) – The factor to lighten the stroke color by. Defaults to 0.3.

Returns:

The fill color string created by lightening the stroke color.

Return type:

str

static stroke_from_fill(fill, darken_factor=0.7)[source]
Parameters:
  • fill (str) – The color string of the fill to be darkened for an outline.

  • darken_factor (float, optional) – The factor to darken the fill color by. Defaults to 0.7.

Returns:

The stroke color string created by darkening the fill color.

Return type:

str

static darken_color(color='none', factor=0.7)[source]

Darken a hex or named color.

Parameters:
  • color (str) – The color to darken. Either a named color or a hex representation of the color.

  • factor (float, optional) –

    A factor to darken. Defaults to 0.7.

    • 0.0 = No change.

    • 1.0 = black.

Returns:

The color-string of the darkened color. If “none” color is given, this will return “black”.

Return type:

str

static lighten_color(color='none', factor=0.3)[source]

Lighten a hex or named color.

Parameters:
  • color (str, optional) – The color to lighten. Either a named color or a hex representation of the color. Defaults to transparent (“none”).

  • factor (float, optional) –

    A factor to lighten (0.0 to 1.0). Defaults to 0.3.

    • 0.0 = no change.

    • 1.0 = white.

Returns:

The color-string of the lightened color. If “none” color is given, this will return “black”.

Return type:

str

class Text_Anchor(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Enumeration of Text Anchoring options for text elements

start = 'start'
middle = 'middle'
end = 'end'
classmethod __contains__(member)

Return True if member is a member of this enum raises TypeError if member is not an enum member

note: in 3.12 TypeError will no longer be raised, and True will also be returned if member is the value of a member in this enum

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class Text_Baseline(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Enumeration of Text Anchoring options for text elements

baseline = 'baseline'
middle = 'middle'
hanging = 'hanging'
classmethod __contains__(member)

Return True if member is a member of this enum raises TypeError if member is not an enum member

note: in 3.12 TypeError will no longer be raised, and True will also be returned if member is the value of a member in this enum

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class Text_Element(x, y, label, name=None, font_family='Arial', font_size=14, is_bold=False, is_italic=False, text_anchor=Text_Anchor.middle, alignment_baseline=Text_Baseline.middle, font_color='black', **element_kwargs)[source]

Bases: Visualizer_Element

Wrapper class for Text SVG elements.

__init__(x, y, label, name=None, font_family='Arial', font_size=14, is_bold=False, is_italic=False, text_anchor=Text_Anchor.middle, alignment_baseline=Text_Baseline.middle, font_color='black', **element_kwargs)[source]

Initialize the text element. :param x: The x coordinate of this element relative to its parent (or globally). :type x: float :param y: The y coordinate of this element relative to its parent (or globally). :type y: float :param label: The value of the text label. :type label: str :param name: The id name of this element defaults to “label_<label>”. :type name: str, optional :param font_family: The font family to render the text in. Defaults to “Arial”. :type font_family: str, optional :param font_size: The font size to render the text in. Defaults to 14px height. :type font_size: int, optional :param is_bold: Whether the text is bold. Defaults to False. :type is_bold: bool, optional :param is_italic: Whether the text is italic. Defaults to False. :type is_italic: bool, optional :param text_anchor: The anchor to render the text in. Defaults to “middle”. :type text_anchor: str, optional :param **element_kwargs: Keyword arguments used to configure the svg text element. :type **element_kwargs: Any

property approximate_label_width: float
Returns:

The approximate width of the text label.

Return type:

float

Notes

Very rough estimate: ~0.6 * font_size per character for typical fonts.

static approximate_text_width(char_count, font_size=14)[source]
Parameters:
  • char_count (int) – The total number of characters.

  • font_size (int, optional) – The font size in pixels. Defaults to 14.

Returns:

The approximate width of the text based on the number of characters.

Return type:

float

Notes

Very rough estimate: ~0.6 * font_size per character for typical fonts.

__len__()[source]
Returns:

The number of characters in the label.

Return type:

int

add_to_drawing(drawing)

Adds this element to the given svg drawing. :param drawing: The drawing to add. :type drawing: Drawing

static darken_color(color='none', factor=0.7)

Darken a hex or named color.

Parameters:
  • color (str) – The color to darken. Either a named color or a hex representation of the color.

  • factor (float, optional) –

    A factor to darken. Defaults to 0.7.

    • 0.0 = No change.

    • 1.0 = black.

Returns:

The color-string of the darkened color. If “none” color is given, this will return “black”.

Return type:

str

static fill_from_stroke(stroke, lighten_factor=0.3)
Parameters:
  • stroke (str) – The color string of the stroke to be lightened for infill.

  • lighten_factor (float, optional) – The factor to lighten the stroke color by. Defaults to 0.3.

Returns:

The fill color string created by lightening the stroke color.

Return type:

str

property global_x: float

Returns: float: The global x coordinate of this element based on its parent’s coordinates.

global_x_position(x)
Parameters:

x (float) – An X-coordinate position relative to this element’s coordinate system.

Returns:

The x-coordinate adjusted to the global coordinate system.

Return type:

float

property global_y: float

Returns: float: The global y coordinate of this element based on its parent’s coordinates.

global_y_position(y)
Parameters:

y (float) – An Y-coordinate position relative to this element’s coordinate system.

Returns:

The y-coordinate adjusted to the global coordinate system.

Return type:

float

static lighten_color(color='none', factor=0.3)

Lighten a hex or named color.

Parameters:
  • color (str, optional) – The color to lighten. Either a named color or a hex representation of the color. Defaults to transparent (“none”).

  • factor (float, optional) –

    A factor to lighten (0.0 to 1.0). Defaults to 0.3.

    • 0.0 = no change.

    • 1.0 = white.

Returns:

The color-string of the lightened color. If “none” color is given, this will return “black”.

Return type:

str

property name: str

Returns: str: The unique name of this element. This will match the id of the svg element being wrapped.

static stroke_from_fill(fill, darken_factor=0.7)
Parameters:
  • fill (str) – The color string of the fill to be darkened for an outline.

  • darken_factor (float, optional) – The factor to darken the fill color by. Defaults to 0.7.

Returns:

The stroke color string created by darkening the fill color.

Return type:

str

property x: float

Returns: float: The x coordinate of this element relative to its parent (or globally).

property y: float

Returns: float: The y coordinate of this element relative to its parent (or globally).