Add snippets.
[librarian.git] / src / librarian / covers / widgets / marquise.py
1 import PIL.Image
2 from .base import Widget
3
4
5 class Marquise(Widget):
6     segments = 4
7
8     def __init__(self, cover, edge_top):
9         self.edge_top = edge_top
10         super().__init__(cover)
11
12     def setup(self):
13         self.slope_w = self.cover.m.width / self.segments / 2
14         self.segment_h = self.cover.m.margin
15         self.title_box_position = (
16             self.cover.m.margin,
17             self.cover.m.title_box_top
18         )
19
20     def get_points(self, w):
21         tip_y = self.edge_top + self.segment_h
22         points = [
23             (0, 0),
24             (w, 0),
25             (w, tip_y),
26         ]
27         for i in range(self.segments - 1, 0, -1):
28             points.extend([
29                 ((2 * i + 1) * self.slope_w, self.edge_top),
30                 (2 * i * self.slope_w, tip_y)
31             ])
32         points.extend([
33             (self.slope_w, self.edge_top),
34             (0, tip_y)
35         ])
36         return points
37
38     def build(self, w, h):
39         img = PIL.Image.new('RGBA', (
40             round(w), round(self.edge_top + self.segment_h)
41         ))
42         draw = PIL.ImageDraw.ImageDraw(img)
43         draw.polygon(
44             self.get_points(w), fill=self.cover.color_scheme['rgb'])
45         return img