2 from librarian.cover import Cover, Metric
 
   3 from .utils.color import algo_contrast_or_hue, luminance, is_very_bright
 
   4 from .utils.textbox import DoesNotFit
 
   5 from .widgets.author import AuthorBox
 
   6 from .widgets.background import Background
 
   7 from .widgets.image import WLLogo, Label
 
   8 from .widgets.marquise import Marquise
 
   9 from .widgets.title import TitleBox
 
  12 class MarquiseCover(Cover):
 
  14     square_variant = False
 
  26             'rgb': (0xc3, 0x27, 0x21),
 
  30             'rgb': (0xa0, 0xbf, 0x38),
 
  34             'rgb': (0xed, 0xc0, 0x16),
 
  38             'rgb': (0x47, 0x66, 0x75),
 
  42     for c in color_schemes:
 
  43         c['luminance'] = luminance(c['rgb'])
 
  44         cim = PIL.Image.new('RGB', (1, 1))
 
  45         cim.putpixel((0, 0), c['rgb'])
 
  47         c['hsv'] = cim.getpixel((0, 0))
 
  50     def set_size(self, final_width, final_height):
 
  51         if final_width is None:
 
  52             self.m = Metric(self, 1)
 
  54             if final_width > self.width:
 
  55                 self.m = Metric(self, final_width / self.width)
 
  57                 self.m = Metric(self, 1)
 
  58                 self.scale_after = final_width / self.width
 
  60         if final_height is not None:
 
  61             self.height = round(final_height / self.scale_after / self.m._scale)
 
  63         self.square_variant = self.height / self.width < 250 / 210
 
  65         marquise_square_small = int(self.width / 2) - 300
 
  66         marquise_square_big = int(self.width / 2) - 100
 
  67         marquise_a4_small = 2970 - self.width
 
  68         marquise_a4_big = marquise_a4_small + 100
 
  70         self.marquise_small = int(round(marquise_square_small + (marquise_a4_small - marquise_square_small) * (self.height - self.width) / (2970 - 2100)))
 
  71         self.marquise_big = int(round(marquise_square_big + (marquise_a4_big - marquise_square_big) * (self.height - self.width) / (2970 - 2100)))
 
  72         self.marquise_xl = self.marquise_big + 200
 
  74         if self.marquise_small > self.marquise_big:
 
  75             self.marquise_small = self.marquise_big
 
  77     def set_color_scheme_from(self, img):
 
  78         self.color_scheme = algo_contrast_or_hue(img, self.color_schemes)
 
  79         self.is_very_bright = is_very_bright(img)
 
  82         img = PIL.Image.new('RGB', (self.m.width, self.m.height), self.background_color)
 
  86         if self.square_variant:
 
  88                 (self.m.marquise_small, 1),
 
  89                 (self.m.marquise_big, 2),
 
  90                 (self.m.marquise_big, 3),
 
  91                 (self.m.marquise_big, None),
 
  95                 (self.m.marquise_small, 2),
 
  96                 (self.m.marquise_small, 1),
 
  97                 (self.m.marquise_big, 3),
 
  98                 (self.m.marquise_xl, 4),
 
  99                 (self.m.marquise_xl, None),
 
 102         # Trying all the layout options with decreasing scale.
 
 105         while title_box is None:
 
 106             for marquise_h, lines in layout_options:
 
 107                 title_box_height = marquise_h - self.m.title_box_top - self.m.margin
 
 109                     title_box = TitleBox(
 
 111                         self.m.width - 2 * self.m.margin,
 
 122         self.marquise_height = marquise_h
 
 123         marquise = Marquise(self, marquise_h)
 
 127             0, marquise.edge_top,
 
 128             self.m.width, self.m.height - marquise.edge_top
 
 130         self.set_color_scheme_from(
 
 132                 0, marquise.edge_top,
 
 133                 self.m.width, marquise.edge_top + (
 
 134                     self.m.height - marquise.edge_top
 
 140             img, 0, 0, self.m.width
 
 144             marquise.title_box_position[0],
 
 145             marquise.title_box_position[1],
 
 148         AuthorBox(self, self.m.author_width).apply(
 
 149             img, self.m.width - self.m.margin - self.m.author_width, self.m.margin
 
 151         WLLogo(self).apply(img, self.m.margin, self.m.margin, None, self.m.logo_h)
 
 154         for logo in self.additional_logos:
 
 155             LogoSticker(self, logo).apply(img, 0, 0)
 
 162 class LabelMarquiseCover(MarquiseCover):
 
 169         img = super().image()
 
 174             self.marquise_height - self.m.label_top,