plugins/network/resource-pack/central/generator/dunelib.py

149 lines
5.5 KiB
Python

"""Tiny supersampled vector-ish drawing kit for the DuneRealms pack (smooth futuristic style)."""
import math
import os
from pathlib import Path
import numpy as np
from PIL import Image, ImageDraw, ImageFilter, ImageChops, ImageFont
SS = 4 # supersampling factor
PAL = dict(
void="#06140f", deep="#081e17", panel="#102a23", panel2="#17372c", moss="#235242",
gold="#e5b95e", gold2="#c4a255", gold3="#f2d593", sand="#eae3cc", sand2="#b7c8b7",
spice="#e8913a", spice2="#ffb347", ember="#ff6a3d", fremen="#2f7fe0", fremen2="#58a8ff",
ixian="#6ee7d8", guild="#8a6fd6", guild2="#b78bed", ok="#58be87", ink="#030a07",
)
QUALITY = ["common", "uncommon", "rare", "very_rare", "legendary", "mythic"]
QCOL = ["#e3e3dc", "#58be87", "#65a9ff", "#b78bed", "#edc363", "#ff7a45"]
FONT_BOLD = os.environ.get("DUNE_PACK_FONT", str(Path(__file__).resolve().parents[1] / "inputs/Poppins-Bold.ttf"))
def rgb(h, a=255):
if isinstance(h, tuple):
return h if len(h) == 4 else (*h, a)
h = PAL.get(h, h).lstrip("#")
return (int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16), a)
def mix(c1, c2, t):
a, b = rgb(c1), rgb(c2)
return tuple(int(a[i] + (b[i] - a[i]) * t) for i in range(4))
class Cv:
def __init__(self, w, h=None):
self.w, self.h = w, h or w
self.im = Image.new("RGBA", (self.w * SS, self.h * SS), (0, 0, 0, 0))
# ---- masks -------------------------------------------------
def _new(self):
return Image.new("L", self.im.size, 0)
def poly(self, pts):
m = self._new()
ImageDraw.Draw(m).polygon([(x * SS, y * SS) for x, y in pts], fill=255)
return m
def circle(self, cx, cy, r):
m = self._new()
ImageDraw.Draw(m).ellipse([(cx - r) * SS, (cy - r) * SS, (cx + r) * SS, (cy + r) * SS], fill=255)
return m
def rrect(self, x0, y0, x1, y1, r):
m = self._new()
ImageDraw.Draw(m).rounded_rectangle([x0 * SS, y0 * SS, x1 * SS, y1 * SS], radius=r * SS, fill=255)
return m
def line(self, pts, width):
m = self._new()
d = ImageDraw.Draw(m)
P = [(x * SS, y * SS) for x, y in pts]
d.line(P, fill=255, width=max(1, int(width * SS)), joint="curve")
r = width * SS / 2
for x, y in (P[0], P[-1]):
d.ellipse([x - r, y - r, x + r, y + r], fill=255)
return m
def text(self, xy, s, size, anchor="mm", font=FONT_BOLD):
m = self._new()
f = ImageFont.truetype(font, int(size * SS))
ImageDraw.Draw(m).text((xy[0] * SS, xy[1] * SS), s, font=f, fill=255, anchor=anchor)
return m
# ---- painting ----------------------------------------------
def fill(self, mask, c1, c2=None, alpha=1.0, horizontal=False):
c1 = rgb(c1)
c2 = rgb(c2) if c2 else c1
bb = mask.getbbox()
if not bb:
return
w, h = self.im.size
t = np.linspace(0, 1, w if horizontal else h)
arr = np.zeros((h, w, 4), dtype=np.float32)
for i in range(4):
ch = c1[i] + (c2[i] - c1[i]) * t
arr[:, :, i] = ch[None, :] if horizontal else ch[:, None]
layer = Image.fromarray(arr.astype(np.uint8), "RGBA")
a = mask.point(lambda v: int(v * alpha))
layer.putalpha(ImageChops.multiply(layer.getchannel("A"), a))
self.im = Image.alpha_composite(self.im, layer)
def solid(self, mask, c, alpha=1.0):
self.fill(mask, c, None, alpha)
def glow(self, mask, c, radius, strength=1.0):
g = mask.filter(ImageFilter.GaussianBlur(radius * SS))
g = g.point(lambda v: min(255, int(v * strength)))
layer = Image.new("RGBA", self.im.size, rgb(c, 255))
layer.putalpha(g)
self.im = Image.alpha_composite(self.im, layer)
def edge(self, mask, c, width=1.0, alpha=1.0):
k = int(width * SS) * 2 + 1
inner = mask.filter(ImageFilter.MinFilter(k))
ring = ImageChops.subtract(mask, inner)
self.solid(ring, c, alpha)
def bevel(self, mask, light=0.55, dark=0.35, d=1.2):
off = int(d * SS)
tl = ImageChops.subtract(mask, ImageChops.offset(mask, off, off))
br = ImageChops.subtract(mask, ImageChops.offset(mask, -off, -off))
self.solid(tl, "#ffffff", light)
self.solid(br, "#000000", dark)
def clip(self, mask, other):
return ImageChops.multiply(mask, other)
def out(self, size=None):
s = size or (self.w, self.h)
return self.im.resize(s, Image.LANCZOS)
def hexagon(cx, cy, r, rot=0):
return [(cx + r * math.cos(math.radians(60 * i + rot)), cy + r * math.sin(math.radians(60 * i + rot))) for i in range(6)]
def sparkle(cv, cx, cy, r, color):
m = cv.poly([(cx, cy - r), (cx + r * .22, cy - r * .22), (cx + r, cy), (cx + r * .22, cy + r * .22),
(cx, cy + r), (cx - r * .22, cy + r * .22), (cx - r, cy), (cx - r * .22, cy - r * .22)])
cv.glow(m, color, r * .6, 1.0)
cv.solid(m, "#ffffff", .95)
def hex_lattice(cv, mask, color, alpha=.12, size=6.0, width=.5):
"""faint hex grid clipped to mask"""
layer = cv._new()
d = ImageDraw.Draw(layer)
h = size * math.sqrt(3)
y = -h
row = 0
while y < cv.h + h:
x = -size * 3 + (size * 1.5 if row % 2 else 0)
while x < cv.w + size * 3:
pts = [((x + size * math.cos(math.radians(60 * i))) * SS, (y + size * math.sin(math.radians(60 * i))) * SS) for i in range(6)]
d.line(pts + [pts[0]], fill=255, width=max(1, int(width * SS)))
x += size * 3
y += h / 2
row += 1
cv.solid(ImageChops.multiply(layer, mask), color, alpha)