thirdculture-docker/scripts/button.sh
2026-11-07 19:26:19 -05:00

58 lines
3.2 KiB
Shell
Executable file

#!/usr/bin/env bash
# ./scripts/button.sh <handle> [source image] → 88x31 member button in ppl's but/
set -euo pipefail
[ $# -ge 1 ] || { sed -n 2p "$0"; exit 2; }
cd "$(dirname "$0")/.."
handle=$1
imgs=werc/sites/thirdculture.diy/_werc/pub/imgs
out=werc/sites/ppl.thirdculture.diy/_werc/pub/imgs/but/$handle.gif
sigil=$imgs/tcl2.svg
seed=$(printf %s "$handle" | cksum | cut -d' ' -f1)
if [ $# -ge 2 ]; then src=$2; else
pool=($imgs/{dither_ruins,p2,garden,news,wires,cables,church,clutter,drive,patch,saya,chillin}.png)
src=${pool[$((seed % ${#pool[@]}))]}
fi
t=$(mktemp -d); trap 'rm -rf "$t"' EXIT
magick -size 1x1 xc:'#000300' xc:'#1C202F' xc:'#AEA3F0' xc:'#E4F0FE' +append "$t/pal.png"
magick "$t/pal.png" -size 1x1 xc:'#A4D1AD' +append "$t/pal5.png"
# art: a seeded crop, stretched, 4-tone ordered dither at button size
read -r w h < <(magick identify -format '%w %h\n' "$src[0]")
magick "$src[0]" -resize "x$((h < 186 ? 186 : h))" "$t/src.png"
read -r w h < <(magick identify -format '%w %h\n' "$t/src.png")
cw=$((w * 3 / 5)); ch=$((cw * 31 / 88)); [ $ch -le $h ] || { ch=$h; cw=$((ch * 88 / 31)); }
best=-1
for i in 0 1 2 3 4 5; do
cx=$(( (seed / (7 + i * 11)) % (w - cw + 1) )); cy=$(( (seed / (13 + i * 5)) % (h - ch + 1) ))
sd=$(magick "$t/src.png" -crop "${cw}x${ch}+$cx+$cy" -colorspace gray -format '%[fx:int(standard_deviation*1000)]' info:)
[ "$sd" -le $best ] || { best=$sd; x=$cx; y=$cy; }
done
magick "$t/src.png" -crop "${cw}x${ch}+$x+$y" +repage -resize '88x31!' -colorspace gray -auto-level -level 0,100%,0.9 \
\( -size 88x31 gradient:white-'gray(30%)' \) -compose multiply -composite \
-ordered-dither o4x4,4 "$t/pal.png" -clut "$t/art.png"
# handle: CyberpunkWaifus 11, tightened, then wrapped at a capital/digit if still too wide
txt() { magick -background none -fill '#E4F0FE' +antialias -font fonts/CyberpunkWaifus.ttf -pointsize 11 \
-interline-spacing -2 -kerning "$2" -gravity center label:"$1" -trim +repage "$t/txt.png"; }
fits() { [ "$(magick identify -format %w "$t/txt.png")" -le 82 ]; }
txt "$handle" 0; fits || txt "$handle" -1
if ! fits; then
wrapped=$(perl -e '$s=shift; $m=length($s)/2; my($b,$d)=(int $m,99); while($s=~/(?<=[a-z])(?=[A-Z0-9])|(?<=[^_.-])(?=[_.-])/g){my $p=pos($s); ($b,$d)=($p,abs($p-$m)) if abs($p-$m)<$d} print substr($s,0,$b),"\n",substr($s,$b)' "$handle")
txt "$wrapped" 0; fits || txt "$wrapped" -1
fi
magick "$t/txt.png" -resize '82x>' -alpha extract -threshold 50% -background black -alpha shape "$t/fill.png"
magick "$t/fill.png" -fill '#E4F0FE' -colorize 100% "$t/fill.png"
magick "$t/txt.png" -alpha extract -threshold 50% -morphology Dilate Square:1 -background black -alpha shape -fill black -colorize 100% "$t/edge.png"
g=$(( $(magick identify -format %h "$t/fill.png") > 12 ? 3 : 4 ))
magick -background none -density 72 "$sigil" -resize 11x11 "$t/sig.png"
magick "$t/art.png" \
"$t/sig.png" -gravity northwest -geometry +3+3 -composite \
"$t/edge.png" -gravity south -geometry +0+$g -composite \
"$t/fill.png" -gravity south -geometry +0+$g -composite \
-fill none -strokewidth 1 -stroke '#000300' -draw 'rectangle 1,1 86,29' -stroke '#AEA3F0' -draw 'rectangle 0,0 87,30' \
+dither -remap "$t/pal5.png" +repage "$out"
echo "$out ($(basename "$src"))"