Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_graphics/tests/gradient/gen.sh

3.0 KiB, 1 run, executable

created by r1870400018:20026, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#!/bin/bash
2# Render each `.grad` fixture to the `.png` beside it, using a browser's SVG implementation.
3#
4# The PNGs are the oracle for `tests/gradient_oracle.rs` and are committed alongside the data.
5# Nothing here runs during a test: regenerate only when a fixture changes, and look at the result
6# before keeping it.
7#
8# A `.grad` file is a handful of lines both sides read, so that no part of the description is
9# written twice. Blank lines and lines beginning with `#` are comments.
10#
11# linear x0 y0 x1 y1 the gradient's axis, in the 256 by 256 plane
12# radial cx cy r the gradient's centre and radius, in the same plane
13# stop t rrggbbaa a stop at position t, colour as eight hexadecimal digits
14# rect x y w h the rectangle to fill
15#
16# The page is drawn on a transparent background, so the alpha channel of the PNG is the fixture's
17# own alpha and not a composite against anything.
18set -euo pipefail
19
20DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
21SIZE=256
22
23CHROME=""
24for c in \
25 "$HOME/.cache/ms-playwright/chromium-1229/chrome-linux64/chrome" \
26 "$HOME/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome" \
27 "$(command -v google-chrome || true)" \
28 "$(command -v chromium || true)"
29do
30 if [ -n "$c" ] && [ -x "$c" ]; then
31 CHROME="$c"
32 break
33 fi
34done
35if [ -z "$CHROME" ]; then
36 echo "No Chrome or Chromium found to render with." >&2
37 exit 1
38fi
39echo "Rendering with: $CHROME"
40
41TMP="$(mktemp -d)"
42trap 'rm -rf "$TMP"' EXIT
43
44for g in "$DIR"/*.grad; do
45 name="$(basename "$g" .grad)"
46 kind=""
47 attrs=""
48 stops=""
49 rect=""
50 while read -r verb rest; do
51 case "$verb" in
52 \#*|"") continue ;;
53 linear)
54 set -- $rest
55 kind="linearGradient"
56 attrs="x1=\"$1\" y1=\"$2\" x2=\"$3\" y2=\"$4\""
57 ;;
58 radial)
59 set -- $rest
60 kind="radialGradient"
61 attrs="cx=\"$1\" cy=\"$2\" r=\"$3\" fx=\"$1\" fy=\"$2\""
62 ;;
63 stop)
64 set -- $rest
65 # Eight hexadecimal digits: six of colour and two of alpha, which SVG carries
66 # as a separate attribute rather than in the colour.
67 rgb="${2:0:6}"
68 a8="${2:6:2}"
69 alpha="$(printf '%d' "0x$a8")"
70 alpha="$(awk -v a="$alpha" 'BEGIN { printf "%.6f", a / 255 }')"
71 stops="$stops<stop offset=\"$1\" stop-color=\"#$rgb\" stop-opacity=\"$alpha\"/>"
72 ;;
73 rect)
74 set -- $rest
75 rect="<rect x=\"$1\" y=\"$2\" width=\"$3\" height=\"$4\" fill=\"url(#g)\"/>"
76 ;;
77 esac
78 done < "$g"
79
80 cat > "$TMP/$name.svg" <<EOF
81<svg xmlns="http://www.w3.org/2000/svg" width="$SIZE" height="$SIZE" viewBox="0 0 $SIZE $SIZE">
82<defs><$kind id="g" gradientUnits="userSpaceOnUse" $attrs>$stops</$kind></defs>
83$rect
84</svg>
85EOF
86 "$CHROME" --headless=new --no-sandbox --disable-gpu --hide-scrollbars \
87 --force-device-scale-factor=1 --default-background-color=00000000 \
88 --window-size="$SIZE,$SIZE" --virtual-time-budget=3000 \
89 --screenshot="$TMP/$name.png" "file://$TMP/$name.svg" >/dev/null 2>&1 || true
90 if [ ! -f "$TMP/$name.png" ]; then
91 echo " FAILED to render $name" >&2
92 exit 1
93 fi
94 mv "$TMP/$name.png" "$DIR/$name.png"
95 echo " $name.png"
96done
97echo "Done. Look at the PNGs before keeping them."