spacebox/src/android/generate_icon.sh

90 lines
4.7 KiB
Bash
Executable File

#!/bin/sh
# /\ +------------------------------------------------------------+
# ____/ \____ /| zlib/MIT/Unlicenced game framework licensed to freely use, |
# \ / / | copy, modify and sell without restriction |
# +--\ ^__^ /--+ | |
# | ~/ \~ | | created for <https://foam.shampoo.ooo> |
# | ~~~~~~~~~~~~ | +------------------------------------------------------------+
# | SPACE ~~~~~ | /
# | ~~~~~~~ BOX |/
# +--------------+
#
# Generate a folder structure of adaptive icons to be used in an Android build. Requires imagemagick.
#
# Create legacy, adaptive, and monochrome adaptive icons as mipmaps, one per device DPI. Draw the foreground onto the background to create
# the legacy icons. Draw the foreground onto a transparent background to create the adaptive icons and save them in mipmap-anydpi-v26 to
# to target Android API 26 and up.
#
# The background can be either a hex color or a path to an existing file. If the background is an existing file, mipmaps will be created.
# Otherwise, it is assumed to be a color, and color values will be added to the XML.
ANDROID_BUILD_DIR=$1
FOREGROUND=$2
BACKGROUND=$3
COLORS=$ANDROID_BUILD_DIR/app/src/main/res/values/colors.xml
XXXHDPI=$ANDROID_BUILD_DIR/app/src/main/res/mipmap-xxxhdpi
XXHDPI=$ANDROID_BUILD_DIR/app/src/main/res/mipmap-xxhdpi
XHDPI=$ANDROID_BUILD_DIR/app/src/main/res/mipmap-xhdpi
HDPI=$ANDROID_BUILD_DIR/app/src/main/res/mipmap-hdpi
MDPI=$ANDROID_BUILD_DIR/app/src/main/res/mipmap-mdpi
# If the background is a file, use a mipmap. Otherwise, assume it is a color.
if [ -f "$BACKGROUND" ]
then
DRAWABLE="@mipmap/ic_launcher_background"
BASE="$BACKGROUND"
else
DRAWABLE="@color/iconBackground"
BASE="xc:$BACKGROUND"
fi
# Android adaptive icon XML
MANIFEST="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<adaptive-icon xmlns:android=\"http://schemas.android.com/apk/res/android\">
<background android:drawable=\"$DRAWABLE\" />
<foreground android:drawable=\"@mipmap/ic_launcher_foreground\" />
<monochrome android:drawable=\"@mipmap/ic_launcher_foreground_mono\" />
</adaptive-icon>"
# Create the XXXHDPI versions
convert "$BASE" -resize 432x432 "$FOREGROUND" -gravity center -composite "$XXXHDPI/ic_launcher.png"
convert "xc:none" -resize 432x432 "$FOREGROUND" -gravity center -composite "$XXXHDPI/ic_launcher_foreground.png"
convert "xc:none" -resize 432x432 "$FOREGROUND" -gravity center -composite -threshold 0 -negate "$XXXHDPI/ic_launcher_foreground_mono.png"
# Use the XXXHDPI versions to create the lower resolutions
convert "$XXXHDPI/ic_launcher.png" -resize 324x324 "$XXHDPI/ic_launcher.png"
convert "$XXXHDPI/ic_launcher_foreground.png" -resize 324x324 "$XXHDPI/ic_launcher_foreground.png"
convert "$XXXHDPI/ic_launcher_foreground_mono.png" -resize 324x324 "$XXHDPI/ic_launcher_foreground_mono.png"
convert "$XXXHDPI/ic_launcher.png" -resize 216x216 "$XHDPI/ic_launcher.png"
convert "$XXXHDPI/ic_launcher_foreground.png" -resize 216x216 "$XHDPI/ic_launcher_foreground.png"
convert "$XXXHDPI/ic_launcher_foreground_mono.png" -resize 216x216 "$XHDPI/ic_launcher_foreground_mono.png"
convert "$XXXHDPI/ic_launcher.png" -resize 162x162 "$HDPI/ic_launcher.png"
convert "$XXXHDPI/ic_launcher_foreground.png" -resize 162x162 "$HDPI/ic_launcher_foreground.png"
convert "$XXXHDPI/ic_launcher_foreground_mono.png" -resize 162x162 "$HDPI/ic_launcher_foreground_mono.png"
convert "$XXXHDPI/ic_launcher.png" -resize 108x108 "$MDPI/ic_launcher.png"
convert "$XXXHDPI/ic_launcher_foreground.png" -resize 108x108 "$MDPI/ic_launcher_foreground.png"
convert "$XXXHDPI/ic_launcher_foreground_mono.png" -resize 108x108 "$MDPI/ic_launcher_foreground_mono.png"
# Create background mipmaps if necessary
if [ -f "$BACKGROUND" ]
then
convert "$BASE" -resize 432x432 "$XXXHDPI/ic_launcher_background.png"
convert "$XXXHDPI/ic_launcher_background.png" -resize 324x324 "$XXHDPI/ic_launcher_background.png"
convert "$XXXHDPI/ic_launcher_background.png" -resize 216x216 "$XHDPI/ic_launcher_background.png"
convert "$XXXHDPI/ic_launcher_background.png" -resize 162x162 "$HDPI/ic_launcher_background.png"
convert "$XXXHDPI/ic_launcher_background.png" -resize 108x108 "$MDPI/ic_launcher_background.png"
fi
# Create the adaptive icons XML
mkdir -p "$ANDROID_BUILD_DIR/app/src/main/res/mipmap-anydpi-v26/"
echo "$MANIFEST" > "$ANDROID_BUILD_DIR/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml"
if [ ! -f "$BACKGROUND" ]
then
if [ -z "$(grep iconBackground $COLORS)" ]
then
sed -i "6i\ <color name=\"iconBackground\">$BACKGROUND</color>" $COLORS
else
sed -i "s|\(iconBackground\">\).*\(</color>\)|\1$BACKGROUND\2|" $COLORS
fi
fi