hexagram/py/hexagram/pattern.py
XANTRONIX Development 25664644bf Don't need that
2024-01-14 22:26:28 -05:00

83 lines
2.2 KiB
Python

import cairo
from typing import Iterable
from hexagram.path import Path
class Pattern(Path):
pass
class HexagonPattern(Pattern):
WIDTH = 100
HEIGHT = 87
COLOR = (0.3, 0.1, 0.3)
MATRIX = (0.21213203, 0.21213203, 0.21213203, -0.21213203, 1, 1)
MATRIX_SCALE = (3, 3)
COMMANDS = [
['M', [23.246093, 0,
0, 13.382812]],
['V', [44.689453]],
['L', [0.0839844, 44.640625,
24.033203, 58.505859,
24.001953, 86.332031,
22.841796, 87]],
['h', [4.478516]],
['L', [26.068359, 86.275391,
26.099609, 58.449219,
50.083984, 44.640625,
74.033203, 58.505859,
74.001953, 86.332031,
72.841796, 87]],
['h', [4.478516]],
['L', [ 76.068359, 86.275391,
76.099609, 58.449219,
100, 44.689453]],
['V', [13.365234]],
['L', [76.919921, 0]],
['H', [73.246093]],
['L', [50.015625, 13.373047,
26.919921, 0]],
['Z', []],
['M', [25.083984, 1.25,
49.033203, 15.115234,
49.001953, 42.941406,
25.017578, 56.75,
1.0019531, 42.845703]],
['l', [ 0.033203, -27.75]],
['z', []],
['m', [50, 0,
24.017576, 13.904297,
-0.0352, 27.75]],
['L', [75.017578, 56.75,
51.068359, 42.884766,
51.099609, 15.058594]],
['Z', []]
]
def __init__(self):
super().__init__(self.COMMANDS)
def to_surface(self) -> cairo.ImageSurface:
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.WIDTH, self.HEIGHT)
cr = cairo.Context(surface)
cr.set_source_rgb(*self.COLOR)
self.draw(cr)
cr.stroke()
return surface
def fill(self, cr: cairo.Context):
matrix = cairo.Matrix(*self.MATRIX)
matrix.scale(*self.MATRIX_SCALE)
surface = self.to_surface()
cr.set_source_surface(surface, 0, 0)
source = cr.get_source()
source.set_matrix(matrix)
source.set_extend(cairo.EXTEND_REPEAT)
cr.fill()