Skip to content

Instantly share code, notes, and snippets.

@pkeir
Created January 2, 2019 00:13
Show Gist options
  • Save pkeir/4131fd20dfe363ab5a2cf502d0536586 to your computer and use it in GitHub Desktop.
Save pkeir/4131fd20dfe363ab5a2cf502d0536586 to your computer and use it in GitHub Desktop.
A script to download and scale svg icons from simple-icons.org, before converting them to png, and outputing a SassScript (CSS) fragment with background colour information
#!/bin/bash
# sudo apt-get install jq librsvg2-bin imagemagick
# Move each vector image to the origin, scale, and move it back again
# Outputs a SIZExSIZE transparent png
# Also outputs scss (Sassy CSS) fragments, unless "-q" is an input argument
SIZE=48
SCALE_FACTOR=0.65
ARGS=("$@")
NARGS=$#
SI=simple-icons
SI_URL=https://raw.githubusercontent.com/$SI/$SI/develop/_data/$SI.json
ICON_URL=https://simpleicons.org/icons
if [[ $NARGS -eq 0 ]]; then
RED='\033[0;31m'
NC='\033[0m' # No colour
echo -en "${RED}error:${NC} "
echo " please provide space-delimited SVG icon names without extension. e.g:"
echo " bash simple-icons-png.sh strava bitbucket"
echo " If you don't want the scss colour information use -q (quiet):"
echo " bash simple-icons-png.sh strava bitbucket -q"
echo " ...or if an icon's simple-icons.json entry contains spaces, use quotes:"
echo " bash simple-icons-png.sh strava bitbucket \"Adobe Acrobat Reader\""
exit -1
fi
function wget_scale_svg_png()
{
for N in "${ARGS[@]}"
do
if [ "$N" == "-q" ]; then continue; fi
S_="${N// /}" # Remove spaces
L_="${S_,,}" # Lower case
wget -q $ICON_URL/$L_.svg
X=$(identify "$L_.svg" | awk -F ' ' '{print $3}' | awk -F 'x' '{print $1}')
X=$((X / 2))
SCALE_STR="translate(${X},${X}) scale(${SCALE_FACTOR}) translate(-${X},-${X})"
# Double quotes are needed due to SCALE_STR's use of ${X}
# The odd sed formation ensures only the first occurrence of > is replaced
sed "0,/>/s//><g transform=\"${SCALE_STR}\">/" "$L_.svg" |
sed 's/<\/svg>/<\/g><\/svg>/g' > ${L_}-${SCALE_FACTOR}.svg
OUT=${L_}-${SIZE}.png #OUT=${L_}-${SIZE}-${SCALE_FACTOR}.png
rsvg-convert -h ${SIZE} ${L_}-${SCALE_FACTOR}.svg > $OUT
convert $OUT -negate $OUT
cp $OUT $OLDPWD
done
}
function scss_colour_out()
{
wget -q $SI_URL
for N in "${ARGS[@]}"
do
C=$(jq '.icons[] | select(.title | match('"\"$N\""';"i")) .hex' $SI.json)
S_="${N// /}" # Remove spaces
L_="${S_,,}" # Lower case
C_="${C//\"/}" # Remove quotes
echo -e ".ico-$L_ { @include get-ico-image($L_); background-color: #$C_; }"
done
}
cd `mktemp -d`
for N in "${ARGS[@]}"
do
if [ "$N" == "-q" ]; then QUIET=1; fi
done
if [ -z "$QUIET" ]; then scss_colour_out; fi
wget_scale_svg_png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment