spacebox/src/android/generate_icon.sh

62 lines
3.6 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. Set the adaptive background by writing the color value to XML.
ANDROID_BUILD_DIR=$1
FOREGROUND=../../icon/foreground.png
BACKGROUND=#000000
MANIFEST='<?xml version="1.0" encoding="utf-8"?>\n<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/iconBackground" />
<foreground android:drawable="@mipmap/ic_launcher_foreground" />
<monochrome android:drawable="@mipmap/ic_launcher_foreground_mono" />
</adaptive-icon>'
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
# Create all the XXXHDPI versions
convert xc:$BACKGROUND -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 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 [ -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