-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_sprites.sh
executable file
·157 lines (119 loc) · 5.57 KB
/
create_sprites.sh
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
#!/usr/bin/env bash
#
# (C) Copyright 2017, Anthony Gaudino
# (C) Copyright 2022, Matt Topper <[email protected]> and UberEther contributors
#
# The following script is derivative work of Anthony's sprite generator script found at:
# https://github.com/Piotr1215/sprites/blob/master/sprites/create_sprites.sh
# which is licensed via the GPL. This code therefore is also licensed under the terms
# of the GNU General Public License.
#
#
# Batch creates sprite files for PlantUml
#
# Given a directory can convert all PNG files to PlantUml sprite files.
#
# The generated sprites files formats are based on the ones introduced in
# PlantUml 1.2017.19.
#
# This script assumes plantuml is in PATH.
#
# Help usage message
usage="Batch creates sprite files for PlantUml.
$(basename "$0") [options] prefix
options:
-p directory path to process Default: ./
-g sprite graylevel Default: 16
prefix: a prefix that is added to the sprite name"
# Default arguments values
dir="./" # Directory path to process Default: ./
graylevel=16 # Number of grayscale colors Default: 16
prefix="" # Prefix for sprites names, avoids having
prefixupper="" # two sprites with same name on STDLIB
########################################
#
# Main function
#
########################################
main () {
# Get arguments
while getopts p:w:h:g:s option
do
case "$option" in
p) dir="$OPTARG";;
w) width="$OPTARG";;
h) height="$OPTARG";;
g) graylevel="$OPTARG";;
:) echo "$usage"
exit 1
;;
\?) echo "$usage"
exit 1
;;
esac
done
# Get mandatory argument
shift $(($OPTIND-1))
prefix=$( echo $1 | tr '[:upper:]' '[:lower:]')
prefixupper=$(echo $1 | tr '[:lower:]' '[:upper:]')
# Check mandatory argument
if [ -z "$prefix" ]
then
echo "Please specify a prefix!"
echo "$usage"
exit 1
fi
# Change dir to where images are
if [ ! -d "${dir}" ]
then
echo "Please specify a valid directory"
echo "$usage"
exit 1
fi
cd "$dir"
# Setup Index Generation
echo "# $dir" > index.md
echo "### Overview" >> index.md
echo "| Name | Macro | Image | Url |" >>index.md;
echo "|-------|--------|-------|-----|" >>index.md;
process_png
}
########################################
#
# Generate PlantUml sprite
#
########################################
process_png () {
for i in *.png
do
[ -f "$i" ] || continue
convert "$i" -resize 48x48 -channel RGBA -matte -colorspace gray -transparent none -average "$i"
mv "$i" "${i//-/_}"
done
for i in *.png
do
[ -f "$i" ] || continue
filename=$(echo $i | sed -e 's/.png$//') # Filename without extension
filenameupper=$(echo $filename | tr '[:lower:]' '[:upper:]') # Filename without extension in uppercase
spritename="${prefix}_$filename" # Sprite name is composed by prefix_filename
spritenameupper="${prefixupper}_$filenameupper" # Sprite name in uppercase
spritestereo="$prefixupper $filenameupper" # Sprite stereotype is uppercase prefix followed by uppercase filename
stereowhites=$(echo $spritestereo | sed -e 's/./ /g') # This is just whitespace to make output nicer
echo "@startuml" > $filename.puml
echo -e "$(plantuml -encodesprite $graylevel $i | sed -e '1!b' -e 's/\$/$'${prefix}_'/')\n" >> $filename.puml
echo "!define $spritenameupper(_color) SPRITE_PUT( $stereowhites $spritename, _color)" >> $filename.puml
echo "!define $spritenameupper(_color, _scale) SPRITE_PUT( $stereowhites $spritename, _color, _scale)" >> $filename.puml
echo "!define $spritenameupper(_color, _scale, _alias) SPRITE_ENT( _alias, $spritestereo, $spritename, _color, _scale)" >> $filename.puml
echo "!define $spritenameupper(_color, _scale, _alias, _shape) SPRITE_ENT( _alias, $spritestereo, $spritename, _color, _scale, _shape)" >> $filename.puml
echo "!define $spritenameupper(_color, _scale, _alias, _shape, _label) SPRITE_ENT_L(_alias, $spritestereo, _label, $spritename, _color, _scale, _shape)" >> $filename.puml
echo "!define $spritenameupper(_alias) ENTITY(rectangle,black,$spritename,_alias,$spritenameupper)" >> $filename.puml
echo "!define $spritenameupper(_alias, _label) ENTITY(rectangle,black,$spritename,_label, _alias,$spritenameupper)" >> $filename.puml
echo "!define $spritenameupper(_alias, _label, _shape) ENTITY(_shape,black,$spritename,_label, _alias,$spritenameupper)" >> $filename.puml
echo "!define $spritenameupper(_alias, _label, _shape, _color) ENTITY(_shape,_color,$spritename,_label, _alias,$spritenameupper" >> $filename.puml
echo "skinparam folderBackgroundColor<<$prefixupper $filenameupper>> White" >> $filename.puml
echo "@enduml" >> $filename.puml
# Populate the index.md
echo "| $filename | ${prefixupper}_$filenameupper | ![image-$filename]($filename.png) |$filename.puml |" >> index.md
done
}
main "$@"