Use a new tinted book set on every page
authorAlex Chan <a.chan@wellcome.ac.uk>
Mon, 12 Oct 2020 00:37:18 +0000 (01:37 +0100)
committerAlex Chan <a.chan@wellcome.ac.uk>
Mon, 12 Oct 2020 00:37:18 +0000 (01:37 +0100)
.gitignore
scripts/deploy.sh
scripts/generate_bookshelf.py
scripts/render_html.py
static/style.css
templates/base.html
templates/review.html

index 6ac44e0..3844795 100644 (file)
@@ -1,3 +1,4 @@
 _html
+_shelves
 
 *.pyc
index 93ef952..96ea720 100755 (executable)
@@ -4,6 +4,8 @@ set -o errexit
 set -o nounset
 set -o verbose
 
+git pull origin development
+
 python3 scripts/render_html.py
 
 rsync --archive --verbose --compress --delete \
index a15119c..5e260fb 100755 (executable)
@@ -1,28 +1,91 @@
 #!/usr/bin/env python3
 
+import base64
+import colorsys
+import functools
+import io
+import os
 import random
 
 from PIL import Image, ImageDraw
 
 
-if __name__ == "__main__":
+R = random.Random(0)
+
+
+def get_bins(total_width, min_height, max_height):
     x_coord = 0
-    max_width = 2000
-    min_height = 30
-    max_height = 45
 
-    im = Image.new("RGBA", size=(max_width, max_height))
+    while x_coord <= total_width:
+        width = R.randint(5, 25)
+        height = R.randint(min_height, max_height)
+
+        yield [(x_coord, 0), (x_coord + width, height)]
+        x_coord += width
+
+
+@functools.lru_cache()
+def get_repeatable_bins(**kwargs):
+    """
+    Get a set of bins which is always the same.
+    """
+    return list(get_bins(**kwargs))
+
+
+def get_tint_colors(tint_color):
+    r, g, b = tint_color
+    h, s, v = colorsys.rgb_to_hsv(r, g, b)
+
+    v = min(v, 0.45)
+
+    while True:
+        new_brightness = R.uniform(max(v * 3 / 4, 0), min(v * 4 / 3, 1))
+        yield colorsys.hsv_to_rgb(h, s, new_brightness)
+
+
+def create_shelf(tint_color):
+    bins = get_repeatable_bins(total_width=2000, min_height=30, max_height=45)
+    colors = get_tint_colors(tint_color=tint_color)
+
+    im = Image.new("RGBA", size=(2000, 45))
 
     draw = ImageDraw.Draw(im)
 
-    while x_coord <= max_width:
-        width = random.randint(5, 25)
-        height = random.randint(min_height, max_height)
-        grey = random.randint(10, 110)
+    for bin_xy, bin_color in zip(bins, colors):
+        r, g, b = bin_color
+        draw.rectangle(bin_xy, (int(r * 255), int(g * 255), int(b * 255)))
 
-        draw.rectangle(
-            [(x_coord, 0), (x_coord + width, height)], fill=(grey, grey, grey)
-        )
-        x_coord += width
+    im.save("static/bookshelf_blue.png")
+
+    return im
+
+
+def create_shelf_data_uri(tint_color):
+    r, g, b = tint_color
+
+    if [r, g, b] == [0, 0, 0] or r <= 0.02 and g <= 0.02 and b <= 0.02:
+        tint_color = [0.2, 0.2, 0.2]
+        r, g, b = tint_color
+
+    try:
+        f = open(f"_shelves/{r}_{g}_{b}.png", "rb")
+    except FileNotFoundError:
+        im = create_shelf(tint_color)
+
+        os.makedirs("_shelves", exist_ok=True)
+
+        im.save(f"_shelves/{r}_{g}_{b}.png")
+        f = open(f"_shelves/{r}_{g}_{b}.png", "rb")
+
+    b64_string = base64.b64encode(f.read()).decode("utf8")
+    return f"data:image/png;base64,{b64_string}"
+
+
+if __name__ == "__main__":
+    im = get_shelf(tint_color=[
+        0.23529411764705882,
+        0.2627450980392157,
+        0.5294117647058824
+    ])
 
-    im.save("static/bookshelf.png")
+    im.save("static/bookshelf_blue.png")
index e4b9de0..f30b13b 100755 (executable)
@@ -17,6 +17,7 @@ from markdown.extensions.smarty import SmartyExtension
 from PIL import Image
 import smartypants
 
+from generate_bookshelf import create_shelf_data_uri
 from tint_colors import get_tint_colors, store_tint_color
 
 
@@ -251,6 +252,7 @@ def main():
     env.filters["smartypants"] = smartypants.smartypants
     env.filters["thumbnail_1x"] = thumbnail_1x
     env.filters["css_hash"] = css_hash
+    env.filters["create_shelf_data_uri"] = create_shelf_data_uri
 
     create_thumbnails()
 
index 9b53241..b3adda7 100644 (file)
@@ -53,7 +53,6 @@ aside #fallback_background {
 }
 
 aside #shelf_background {
-  background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB9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vICkiQpeQFJkpS8jA529k9ieliJe1Pk7EYq+GqCmB1IS9k3Rt1Oiv43TdwumsaniDhEJ+sdJyZDVRk5s1eIeJ01+maMiGOswc1RavqYF25S9STp+ceH1L3HVjP+B3E9dJxnBllON6qZuUjTCXQXVTx/lrrAag2cQct6433m9VKRsXYTP7NogPYYRdLft/s7CvawvFe/RFr04yMW7WcrecB6+eEH4vayCk+co24HGylD21z376zdqUlyTiBtUV0fU3aEvIFhBC+NoWRlrNSzI6yPy5O0xZ90niP/oWgfStPlGaL2s5wbVyk7Sl7PEFvTmds0HGUjjV1GW8b5WeZ1o/WWsf1M0vQUbXD0U1Zo+AZK3+GPaZ/PiHqRpQyh9fIhS5tB2jCzg6xGhlbnwh3mvMs2NckKHfgIeJk5xz8gro+OM4OW9HCIosOU7Rhl0V5au30LrcXNCQoG2AAP2cK6vqcTZLTJGCt0nnk9bL5rVO1Cm6Lna4peYc7rJ6k4yAZ7iLRJpljGIC30ou0lQyVTrMSnSOvnKu32Fdps2bdI7TJMRYbWwSfUPEf7XWBp/XSQk7TbtRGkbWMCpeD451QMshYZm2YSSan6iJKnaWmIJYwiqS2yK2id3GDLuXefjfUBS/qaDXD9V6R1NErFEXKeGEab5NnTlD1GztW/WJ3RcRa8TdPOU7Qww0bKWNYIktRxhtlM0yTmOkUH2OZOseVkqOMdO0OH+YK4PajTjJOiYbSMM7TX7kH0f76SyBM6SrN0AAAAAElFTkSuQmCC);
   background-size: auto 100%;
   height: 45px;
   z-index: 2;
index 48f34c6..87e8c77 100644 (file)
@@ -8,6 +8,12 @@
 
     {% block head_content %}{% endblock %}
 
+    <style>
+      aside #shelf_background {
+        background: url({{ [0, 0, 0] | create_shelf_data_uri }});
+      }
+    </style>
+
     <title>{% if title %}{{ title | smartypants | safe }} {% endif %}📚 lexie&rsquo;s book tracker</title>
   </head>
   <body>
index 2926a5a..73cc74c 100644 (file)
       .book-cover img {
         box-shadow: 0px 5px 5px rgba({{ r }}, {{ g }}, {{ b }}, 0.25);
       }
+
+      aside #shelf_background {
+        background: url({{ tint_colors[review_entry.book.cover_image] | create_shelf_data_uri }})
+      }
     </style>
   {% endif %}