inkex.tween module#

Module for interpolating attributes and styles

Changed in version 1.2: Rewritten in inkex 1.2 in an object-oriented structure to support more attributes.

inkex.tween.interpcoord(coord_a: inkex.tween.Number, coord_b: inkex.tween.Number, time: float)[source]#

Interpolate single coordinate by the amount of time

inkex.tween.interppoints(point1: Tuple[float, float], point2: Tuple[float, float], time: float) Tuple[float, float][source]#

Interpolate coordinate points by amount of time

class inkex.tween.AttributeInterpolator(start_value, end_value)[source]#

Bases: abc.ABC

Interpolate between attributes

static best_style(node)[source]#

Gets the best possible approximation to a node’s style. For nodes inside the element tree of an SVG file, stylesheets defined in the defs of that file can be taken into account. This should be the case for input elements, but is not required - in that case, only the local inline style is used.

During the interpolation process, some nodes are created temporarily, such as plain gradients of a single color to allow solid<->gradient interpolation. These are not attached to the document tree and therefore have no root. Since the only style relevant for them is the inline style, it is acceptable to fallback to it.

Parameters

node (BaseElement) – The node to get the best approximated style of

Returns

If the node is rooted, the CSS specified style. Else, the inline style.

Return type

Style

static create_from_attribute(snode, enode, attribute, method=None)[source]#

Creates an interpolator for an attribute. Currently, only path, transform and style attributes are supported

Parameters
  • snode (BaseElement) – start element

  • enode (BaseElement) – end element

  • attribute (str) – attribute name (for styles, starting with “style/”)

  • method (AttributeInterpolator, optional) – (currently only used for paths). Specifies a method used to interpolate the attribute. Defaults to None.

Raises

ValueError – if an attribute is passed that is not a style, path or transform attribute

Returns

an interpolator whose type depends on attribute.

Return type

AttributeInterpolator

abstract interpolate(time=0)[source]#

Interpolation method, needs to be implemented by subclasses

_abc_impl = <_abc._abc_data object>#
class inkex.tween.StyleInterpolator(start_value, end_value)[source]#

Bases: inkex.tween.AttributeInterpolator

Class to interpolate styles

static create(snode, enode, attribute)[source]#

Creates an Interpolator for a given style attribute, depending on its type:

Parameters
  • snode (BaseElement) – start element

  • enode (BaseElement) – end element

  • attribute (str) – attribute to interpolate

Raises

ValueError – if the attribute is not in any of the lists

Returns

an interpolator object whose type depends on the attribute.

Return type

AttributeInterpolator

static create_from_fill_stroke(snode, enode, attribute)[source]#

Creates an Interpolator for a given color-like attribute

Parameters
  • snode (BaseElement) – start element

  • enode (BaseElement) – end element

  • attribute (str) – attribute to interpolate

Raises
  • ValueError – if the attribute is not color-like

  • ValueError – if the attribute is unset on both start and end style

Returns

an interpolator object whose type depends on the attribute.

Return type

AttributeInterpolator

interpolate(time=0)[source]#

Interpolates a style using the interpolators set in self.interpolators

Parameters

time (int, optional) – Interpolation position. If 0, start_value is returned, if 1, end_value is returned. Defaults to 0.

Returns

interpolated style

Return type

inkex.Style

_abc_impl = <_abc._abc_data object>#
class inkex.tween.TrivialInterpolator(value)[source]#

Bases: inkex.tween.AttributeInterpolator

Trivial interpolator, returns value for every time

interpolate(time=0)[source]#

Interpolation method, needs to be implemented by subclasses

_abc_impl = <_abc._abc_data object>#
class inkex.tween.ValueInterpolator(start_value=0, end_value=0)[source]#

Bases: inkex.tween.AttributeInterpolator

Class for interpolation of a single value

interpolate(time=0)[source]#

(Linearly) interpolates a value

Parameters

time (int, optional) – Interpolation position. If 0, start_value is returned, if 1, end_value is returned. Defaults to 0.

Returns

interpolated value

Return type

int

_abc_impl = <_abc._abc_data object>#
class inkex.tween.UnitValueInterpolator(start_value=0, end_value=0)[source]#

Bases: inkex.tween.ValueInterpolator

Class for interpolation of a value with unit

interpolate(time=0)[source]#

(Linearly) interpolates a value

Parameters

time (int, optional) – Interpolation position. If 0, start_value is returned, if 1, end_value is returned. Defaults to 0.

Returns

interpolated value

Return type

int

_abc_impl = <_abc._abc_data object>#
class inkex.tween.ArrayInterpolator(start_value, end_value)[source]#

Bases: inkex.tween.AttributeInterpolator

Interpolates array-like objects element-wise, e.g. color, transform, coordinate

interpolate(time=0)[source]#

Interpolates an array element-wise

Parameters

time (int, optional) – [description]. Defaults to 0.

Returns

interpolated array

Return type

List

_abc_impl = <_abc._abc_data object>#
class inkex.tween.TransformInterpolator(start_value=Transform(), end_value=Transform())[source]#

Bases: inkex.tween.ArrayInterpolator

Class for interpolation of transforms

interpolate(time=0)[source]#

Interpolates a transform by interpolating each item in the transform hexad separately.

Parameters

time (int, optional) – Interpolation position. If 0, start_value is returned, if 1, end_value is returned. Defaults to 0.

Returns

interpolated transform

Return type

Transform

_abc_impl = <_abc._abc_data object>#
class inkex.tween.ColorInterpolator(start_value=Color('#000000'), end_value=Color('#000000'))[source]#

Bases: inkex.tween.ArrayInterpolator

Class for color interpolation

static create(sst, est, attribute)[source]#

Creates a ColorInterpolator for either Fill or stroke, depending on the attribute.

Parameters
  • sst (Style) – Start style

  • est (Style) – End style

  • attribute (string) – either fill or stroke

Raises

ValueError – if none of the start or end style is a color.

Returns

A ColorInterpolator object

Return type

ColorInterpolator

interpolate(time=0)[source]#

Interpolates a color by interpolating its r, g, b, a channels separately.

Parameters

time (int, optional) – Interpolation position. If 0, start_value is returned, if 1, end_value is returned. Defaults to 0.

Returns

interpolated color

Return type

Color

_abc_impl = <_abc._abc_data object>#
class inkex.tween.GradientInterpolator(start_value, end_value, svg=None)[source]#

Bases: inkex.tween.AttributeInterpolator

Base class for Gradient Interpolation

static create(snode, enode, attribute)[source]#

Creates a GradientInterpolator for either fill or stroke, depending on attribute.

Cases: (A, B) -> Interpolator

  • Linear Gradient, Linear Gradient -> LinearGradientInterpolator

  • Color or None, Linear Gradient -> LinearGradientInterpolator

  • Radial Gradient, Radial Gradient -> RadialGradientInterpolator

  • Color or None, Radial Gradient -> RadialGradientInterpolator

  • Radial Gradient, Linear Gradient -> ValueError

  • Color or None, Color or None -> ValueError

Parameters
  • snode (BaseElement) – start element

  • enode (BaseElement) – end element

  • attribute (string) – either fill or stroke

Raises

ValueError – if none of the styles are a gradient or if they are gradients of different types

Returns

an Interpolator object

Return type

GradientInterpolator

static interpolate_linear_list(positions, values, newpositions, func)[source]#

Interpolates a list of values given at n positions to the best approximation at m newpositions.

>>>
    |
    |         x
    |  x
    _________________
       pq  q  p   q
    (x denotes function values, p: positions, q: newpositions)
    A function may be given to interpolate between given values.
Parameters
  • positions (list[number-like]) – position of current function values

  • values (list[Type]) – list of arbitrary type, len(values) == len(positions)

  • newpositions (list[number-like]) – position of interpolated values

  • func (Callable[[Type, Type, float], Type]) – Function to interpolate between values

Returns

interpolated function values at positions

Return type

list[Type]

static append_to_doc(element, gradient)[source]#

Splits a gradient into stops and orientation, appends it to the document’s defs and returns the href to the orientation gradient.

Parameters
  • element (BaseElement) – an element inside the SVG that the gradient should be added to

  • gradient (Gradient) – the gradient to append to the document

Returns

the orientation gradient, or the gradient object if element has no root or is None

Return type

Gradient

interpolate(time=0)[source]#

Interpolate with another gradient.

_abc_impl = <_abc._abc_data object>#
class inkex.tween.LinearGradientInterpolator(start_value=LinearGradient(), end_value=LinearGradient(), svg=None)[source]#

Bases: inkex.tween.GradientInterpolator

Class for interpolation of linear gradients

static initialize_position(grad, bbox)[source]#

Initializes a linear gradient’s position

_abc_impl = <_abc._abc_data object>#
class inkex.tween.RadialGradientInterpolator(start_value=RadialGradient(), end_value=RadialGradient(), svg=None)[source]#

Bases: inkex.tween.GradientInterpolator

Class to interpolate radial gradients

static initialize_position(grad, bbox)[source]#

Initializes a radial gradient’s position

_abc_impl = <_abc._abc_data object>#
class inkex.tween.StopInterpolator(start_value, end_value)[source]#

Bases: inkex.tween.AttributeInterpolator

Class to interpolate gradient stops

interpolate(time=0)[source]#

Interpolates a gradient stop by interpolating style and offset separately

Parameters

time (int, optional) – Interpolation position. If 0, start_value is returned, if 1, end_value is returned. Defaults to 0.

Returns

interpolated gradient stop

Return type

Stop

_abc_impl = <_abc._abc_data object>#
class inkex.tween.PathInterpolator(start_value=Path(), end_value=Path())[source]#

Bases: inkex.tween.AttributeInterpolator

Base class for Path interpolation

truncate_subpaths()[source]#

Truncates the longer path so that all subpaths in both paths have an equal number of bezier commands

interpolate(time=0)[source]#

Interpolation method, needs to be implemented by subclasses

_abc_impl = <_abc._abc_data object>#
class inkex.tween.EqualSubsegmentsInterpolator(start_path=Path(), end_path=Path())[source]#

Bases: inkex.tween.PathInterpolator

Interpolates the path by rediscretizing the subpaths first.

static get_subpath_lenghts(path)[source]#

prepare lengths for interpolation

static process_path(path, other)[source]#

Rediscretize path so that all subpaths have an equal number of segments, so that there is a node at the path “times” where path or other have a node

Parameters
  • path (Path) – the first path

  • other (Path) – the second path

Returns

the prepared path description for the intermediate path

Return type

Array

_abc_impl = <_abc._abc_data object>#
class inkex.tween.FirstNodesInterpolator(start_path=Path(), end_path=Path())[source]#

Bases: inkex.tween.PathInterpolator

Interpolates a path by discarding the trailing nodes of the longer subpath

_abc_impl = <_abc._abc_data object>#