-
Notifications
You must be signed in to change notification settings - Fork 2
/
tensor_patch
executable file
·57 lines (55 loc) · 1.59 KB
/
tensor_patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python3
#+
# Demonstration of effect of control points in a tensor-product patch.
#
# Copyright 2015 by Lawrence D'Oliveiro <ldo@geek-central.gen.nz>. This
# script is licensed CC0
# <https://creativecommons.org/publicdomain/zero/1.0/>; do with it
# what you will.
#-
import sys
import os
import qahirah as qah
from qahirah import \
CAIRO, \
Colour, \
Matrix, \
Rect, \
Vector
colour1 = Colour.from_hsva((0.55, 0.7, 0.7))
colour2 = Colour.from_hsva((0.05, 0.7, 0.7))
offsets = (None, -1.0, -0.5, 0, 0.5, 1.0, 2.0)
# offsets of patch control points from centre towards corners, None for default (Coons patch)
swatch_size = Vector(300, 300)
pix = qah.ImageSurface.create \
(
format = CAIRO.FORMAT_RGB24,
dimensions = swatch_size * Vector(len(offsets), 1)
)
g = qah.Context.create(pix)
for i, offset in enumerate(offsets) :
pat = qah.MeshPattern.create()
pat.begin_patch()
for j, corner in \
enumerate((
Vector(0, 0),
Vector(1, 0),
Vector(1, 1),
Vector(0, 1),
)) \
:
pat.line_to(corner)
pat.set_corner_colour(j, (colour1, colour2)[j % 2 == 0])
if offset != None :
pat.set_control_point(j, qah.interp(offset, Vector(0.5, 0.5), corner))
#end if
#end for
pat.end_patch()
pat.matrix = Matrix.scale(swatch_size).inv()
g.matrix = Matrix.translate(Vector(swatch_size.x, 0) * i)
g.source = pat
g.new_path()
g.rectangle(Rect.from_dimensions(swatch_size))
g.fill()
#end for
pix.flush().write_to_png("%s.png" % os.path.basename(sys.argv[0]))