-
Notifications
You must be signed in to change notification settings - Fork 2
/
glitcher
executable file
·213 lines (186 loc) · 5.43 KB
/
glitcher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
OUT="${1}"
TXT="${*:2}"
GLITCHER_DEBUG="${GLITCHER_DEBUG:-0}"
[ ! -t 0 ] && { read -r TXT; }
function print_help {
cat <<EOF
$ glitcher FILE TEXT...
eg.
glitcher out.svg hello world
EOF
}
! [[ "$BASH_VERSION" =~ 5.* ]] && {
echo "[ERROR] Bash v5 or higher is required, found $BASH_VERSION"
exit 1
}
[ "$OUT" = "" ] || [ "$TXT" = "" ] && {
echo " [ERROR] bad argument"
print_help
exit 1
}
! [ "$OSTYPE" = "linux-gnu" ] && {
echo "[WARNING] only tested on linux-gnu"
sleep 3
}
function rnd_int {
local MAX="${1:-24}"
echo $(((RANDOM % MAX) + 8))
}
function rnd_float {
local MAX="${1:-99}"
local REV="${2:-0}"
((RANDOM % 2 && REV)) && NEG=-1 || NEG=1
printf '%.2f\n' "$(bc -l <<<"scale=2; ($(((RANDOM % MAX) + 1)) / 100) * $NEG")"
}
function rnd_seq {
local keys=()
local vals=''
local count=0
local steps="$(((RANDOM % 16) + 8))"
while ((count < steps)); do
((count++))
keys+=("$(rnd_float)")
done
mapfile -t keys < <(
printf '0;\n'
printf '%s;\n' "${keys[@]}" | sort -g | uniq
)
for _ in "${keys[@]}"; do
((UPPER = RANDOM % 6 ? 2 : 4))
vals+="$(rnd_float ${UPPER} 1); "
done
local ks="${keys[*]}"
printf '%s\n%s' "${ks::(-1)}" "${vals::(-2)}"
}
mapfile -t B_SHIFT < <(rnd_seq)
mapfile -t R_SHIFT < <(rnd_seq)
read -r B_DUR < <(rnd_int 60)
read -r R_DUR < <(rnd_int 60)
read -r WIDTH < <(bc <<<"${#TXT} * 18")
((GLITCHER_DEBUG)) && RECT='<rect fill="#ee33aa" width="100%" height="100%" x="0" y="0" />'
cat <<EOF >"$OUT"
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 ${WIDTH} 24" >
<!-- made with glitcher-cli v0.3
█▀▀ █░░ █ ▀█▀ █▀▀ █░█ █▀▀ █▀█
█▄█ █▄▄ █ ░█░ █▄▄ █▀█ ██▄ █▀▄
https://github.com/metaory/glitcher-cli -->
<style>
text { fill: #220033; }
@media (prefers-color-scheme: dark) { text { fill: #DDCCFF; } }
</style>
${RECT}
<text filter="url(#glitch)"
font-family="monospace, serif"
font-weight="bolder"
font-size="x-large"
text-anchor="middle"
dominant-baseline="mathematical"
x="50%" y="42%"
>${TXT}</text>
<defs>
<filter id="glitch"
primitiveUnits="objectBoundingBox"
x="-10%"
y="0%"
width="120%"
height="100%">
<feColorMatrix in="SourceGraphic"
result="red"
type="matrix"
values="1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0" />
<feColorMatrix in="SourceGraphic"
result="green"
type="matrix"
values="0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0" />
<feColorMatrix in="SourceGraphic"
result="blue"
type="matrix"
values="0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0" />
<feOffset in="red"
result="red-shifted"
dx="-0.05"
dy="0">
<animate attributeName="dx"
keyTimes="${R_SHIFT[0]}"
values="${R_SHIFT[1]}"
begin="0"
dur="${R_DUR}s"
calcMode="discrete"
repeatCount="indefinite"
fill="freeze" />
</feOffset>
<feOffset in="blue"
result="blue-shifted"
dx="0.05"
dy="0">
<animate attributeName="dx"
keyTimes="${B_SHIFT[0]}"
values="${B_SHIFT[1]}"
begin="0"
dur="${B_DUR}s"
calcMode="discrete"
repeatCount="indefinite"
fill="freeze" />
</feOffset>
<feBlend mode="screen"
in="red-shifted"
in2="green"
result="red-green" />
<feBlend mode="screen"
in="red-green"
in2="blue-shifted"
result="blended" />
EOF
I=0
NW=0
SLICES=()
function put_slice {
((I++))
read -r S1 < <(rnd_int 30)
read -r S2 < <(rnd_int 10)
read -r DR < <(rnd_int 60)
mapfile -t SEQ < <(rnd_seq)
NX=$((NW + S1))
SA="slice_${I}a"
SB="slice_${I}b"
SLICES+=("$SA" "$SB")
cat <<EOF >>"$OUT"
<feOffset in="blended"
dx="0"
dy="0"
y="${NW}%"
height="${S1}%"
result="${SA}" />
<feOffset in="blended"
dx="0"
dy="0"
y="${NX}%"
height="${S2}%"
result="${SB}">
<animate attributeName="dx"
keyTimes="${SEQ[0]}"
values="${SEQ[1]}"
begin="0s"
dur="${DR}s"
calcMode="discrete"
repeatCount="indefinite"
fill="freeze" />
</feOffset>
EOF
NW=$((NX + S2))
}
while ((NW <= 100)); do put_slice; done
cat <<<' <feMerge>' >>"$OUT"
for S in "${SLICES[@]}"; do
cat <<<" <feMergeNode in=""\"${S}\""" />" >>"$OUT"
done
cat <<EOF >>"$OUT"
</feMerge>
</filter>
</defs>
</svg>
EOF
echo "created $OUT successfully."
# vim: ft=bash