Patterns¶
keyed_extras.patterns
¶
Generate patterns that can be applied to shapes.
Tip
The size of the pattern should equal or exceed the size of the object receiving the fill pattern to prevent a weird, unavoidable tiling edge effect in cairo.
However, it is best to keep patterns as small as possible, as they are relatively expensive to render compared to solid colors.
linear_gradient_pattern
¶
Create a linear gradient pattern that can be used as a fill or stroke source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x0
|
float
|
X-coordinate of the starting point of the gradient |
required |
y0
|
float
|
Y-coordinate of the starting point of the gradient |
required |
x1
|
float
|
X-coordinate of the ending point of the gradient |
required |
y1
|
float
|
Y-coordinate of the ending point of the gradient |
required |
stops
|
list[tuple[float, tuple[float, float, float]]]
|
List of color stops, where each stop is a tuple of (offset, color) offset is a float from 0.0 to 1.0 representing position along the gradient color is a tuple of (r, g, b) values from 0.0 to 1.0 |
required |
Returns:
Type | Description |
---|---|
LinearGradient
|
A linear gradient pattern. |
radial_gradient_pattern
¶
Create a radial gradient pattern that can be used as a fill or stroke source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cx0
|
float
|
X-coordinate of the center of the starting circle |
required |
cy0
|
float
|
Y-coordinate of the center of the starting circle |
required |
radius0
|
float
|
Radius of the starting circle |
required |
cx1
|
float
|
X-coordinate of the center of the ending circle |
required |
cy1
|
float
|
Y-coordinate of the center of the ending circle |
required |
radius1
|
float
|
Radius of the ending circle |
required |
stops
|
list[tuple[float, tuple[float, float, float]]]
|
List of color stops, where each stop is a tuple of (offset, color) offset is a float from 0.0 to 1.0 representing position along the gradient color is a tuple of (r, g, b) values from 0.0 to 1.0 |
required |
Returns:
Type | Description |
---|---|
RadialGradient
|
A radial gradient pattern. |
cross_hatch_pattern
¶
cross_hatch_pattern(size=100, line_width=2, spacing=10, color=(0, 0, 0), direction='forward', freehand=False)
Create a cross-hatching pattern that can be used as a fill or stroke source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
Size of the pattern tile in pixels |
100
|
line_width
|
float
|
Width of the hatching lines |
2
|
spacing
|
float
|
Space between hatching lines |
10
|
color
|
tuple
|
RGB color tuple for the lines, each value from 0.0 to 1.0 |
(0, 0, 0)
|
freehand
|
bool
|
If True, uses FreeHandContext for a hand-drawn appearance |
False
|
direction
|
Literal['forward', 'backward', 'both']
|
Direction of the hatching lines:
|
'forward'
|
Returns:
Type | Description |
---|---|
SurfacePattern
|
A repeating pattern containing the cross-hatch design. |
checker_pattern
¶
Create a checkerboard pattern that can be used as a fill or stroke source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
Size of the pattern tile in pixels |
100
|
square_size
|
int
|
Size of each square in the checkerboard |
10
|
color1
|
tuple
|
RGB color tuple for the first set of squares |
(1, 1, 1)
|
color2
|
tuple
|
RGB color tuple for the second set of squares |
(0, 0, 0)
|
freehand
|
bool
|
If True, uses FreeHandContext for a hand-drawn appearance |
False
|
Returns:
Type | Description |
---|---|
SurfacePattern
|
A repeating pattern containing the checkerboard design. |
polka_dot_pattern
¶
polka_dot_pattern(size=100, dot_radius=5, spacing=20, dot_color=(0, 0, 0), bg_color=(1, 1, 1), freehand=False)
Create a polka dot pattern that can be used as a fill or stroke source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
Size of the pattern tile in pixels |
100
|
dot_radius
|
int
|
Radius of each dot in pixels |
5
|
spacing
|
int
|
Distance between dot centers in pixels |
20
|
dot_color
|
tuple
|
RGB color tuple for the dots |
(0, 0, 0)
|
bg_color
|
tuple
|
RGB color tuple for the background |
(1, 1, 1)
|
freehand
|
bool
|
If True, uses FreeHandContext for a hand-drawn appearance |
False
|
Returns:
Type | Description |
---|---|
SurfacePattern
|
A repeating pattern containing the polka dot design. |
spiral_pattern
¶
Create a spiral pattern that can be used as a fill or stroke source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
Size of the pattern tile in pixels |
100
|
revolutions
|
int
|
Number of complete revolutions in the spiral |
5
|
line_width
|
float
|
Width of the spiral line |
2
|
color
|
tuple
|
RGB color tuple for the spiral |
(0, 0, 0)
|
freehand
|
bool
|
If True, uses FreeHandContext for a hand-drawn appearance |
False
|
Returns:
Type | Description |
---|---|
SurfacePattern
|
A repeating pattern containing the spiral design. |
concentric_circle_pattern
¶
concentric_circle_pattern(size=100, num_circles=5, color_stops=[(0, (1, 0, 0)), (1, (0, 0, 1))], freehand=False)
Create a pattern of concentric circles that can be used as a fill or stroke source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
Size of the pattern tile in pixels |
100
|
num_circles
|
int
|
Number of concentric circles to draw |
5
|
color_stops
|
list[tuple[float, tuple[float, float, float]]]
|
List of color stops to use for the circles, where each stop is a tuple of (offset, color). The offset is a value from 0.0 to 1.0 representing the position from innermost to outermost circle, and color is an RGB tuple. |
[(0, (1, 0, 0)), (1, (0, 0, 1))]
|
freehand
|
bool
|
If True, uses FreeHandContext for a hand-drawn appearance |
False
|
Returns:
Type | Description |
---|---|
SurfacePattern
|
A repeating pattern containing the concentric circles design. |
stipple_pattern
¶
stipple_pattern(size=100, dot_radius=0.5, density=0.1, dot_color=(0, 0, 0), bg_color=(1, 1, 1), freehand=False)
Create a stippling pattern with randomly distributed dots that can be used as a fill or stroke source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
Size of the pattern tile in pixels |
100
|
dot_radius
|
float
|
Radius of each stipple dot in pixels |
0.5
|
density
|
float
|
Density of dots as a fraction from 0.0 to 1.0 (higher means more dots) |
0.1
|
dot_color
|
tuple[float, float, float]
|
RGB color tuple for the stipple dots |
(0, 0, 0)
|
bg_color
|
tuple[float, float, float]
|
RGB color tuple for the background |
(1, 1, 1)
|
freehand
|
bool
|
If True, uses FreeHandContext for a hand-drawn appearance |
False
|
Returns:
Type | Description |
---|---|
SurfacePattern
|
A repeating pattern containing the stippling design. |