Patterns - special fills

Martin McBride, 2020-08-01
Tags pattern
Categories generativepy generative art

In generativepy, shapes can be filled or stroked (outline) using solid colours or patterns.

Currently, the only pattern supported is linear gradient. More patterns will be supported in future versions.

Patterns make use of the following classes, in the geometry module:

  • LinearGradient - defines a linear gradient.

Patterns all work in s similar way:

  • The pattern is first constructed using the constructor and any required builder methods.
  • The build method is then called to create the pattern.

The object returned by get_pattern can be used in place of a Color object when setting a stroke or fill.

See the patterns tutorial for examples.


The LinearGradient class defines a linear gradient.



Creates a LinearGradient object.


Sets the start and end points.

of_points(start, end)
Parameter Type Description
start (number, number) A tuple of two numbers, giving the (x, y) position of start point.
end (number, number) A tuple of two numbers, giving the (x, y) position of end point.

Specifies the start and end points of the gradient.


Sets the start and end colours.

with_start_end(start, end)
Parameter Type Description
start Color The first colour in the gradient.
end Color The second colour in the gradient.

Specifies the start and end colours of a simple two colour gradient.

This is a shortcut for

of_points([(0, start), (1, stop)])

A LinearGradient should always have either with_start_end or of_points, but not both.


Sets a list of colour stops.

Parameter Type Description
stops Sequence of stops A list or tuple of stops (see below)

Specifies individual stops in the gradient.

Each stop is a tuple of (position, color), where:

  • position is the position within the gradient (0 for start, 1 for end, 0.5 for halfway etc).
  • color is the colour at that stop.

For example [(0, Color('black')), (0.5, Color('red')), (1, Color('blue'))] goes from black to red to blue.



Finalises the creation of the pattern. You must call this after all the construction steps, but before using the pattern.



This method is used internally to get the Pycairo pattern object when the pattern is passed in to s Shape fill or stroke method.

You should not normally need to call this in your code.

If you found this article useful, you might be interested in the book Computer Graphics in Python or other books by the same author.


Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart polygon positional parameter print pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip