Skip to content

Instantly share code, notes, and snippets.

@nogajun
Last active August 29, 2015 14:10
Show Gist options
  • Save nogajun/97951bfd2dd835998551 to your computer and use it in GitHub Desktop.
Save nogajun/97951bfd2dd835998551 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Convert GIMP Palette to LibreOffice Color Palette.
# Usage: hue3602soc.sh [ GIMP_palette.gpl ]
#
# Copyright: Jun NOGATA <nogajun@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
GPL_FILE="$1"
usage(){
echo "Usage: gpl2soc.sh [ GIMP_palette.gpl ]"
echo "Convert GIMP Palette(.gpl) to LibreOffice Color Palette(.soc)."
echo
}
# file Check
if [ "$#" = "0" ]; then
usage
exit 1
fi
if [ ! -e "$1" ]; then
echo "Can't file reading: No such file or directory"
exit 1
fi
# GIMP Palette Check
if [ ! "$(head -1 $1 | grep GIMP)" ]; then
echo "This is NOT Gimp Color Palette file."
exit 1
fi
FILENAME="${GPL_FILE%.gpl}.soc"
# ------------------------------------------------------------------------
put_header(){
# put header
cat << EOL > ${FILENAME}
<?xml version="1.0" encoding="UTF-8"?>
<ooo:color-table xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ooo="http://openoffice.org/2004/office">
<!-- White to Black -->
<draw:color draw:name="White" draw:color="#FFFFFF"/>
<draw:color draw:name="Gray1" draw:color="#E3E3E3"/>
<draw:color draw:name="Gray2" draw:color="#C6C6C6"/>
<draw:color draw:name="Gray3" draw:color="#AAAAAA"/>
<draw:color draw:name="Gray4" draw:color="#8E8E8E"/>
<draw:color draw:name="Gray5" draw:color="#717171"/>
<draw:color draw:name="Gray6" draw:color="#555555"/>
<draw:color draw:name="Gray7" draw:color="#393939"/>
<draw:color draw:name="Gray8" draw:color="#1C1C1C"/>
<draw:color draw:name="Black" draw:color="#000000"/>
<!-- Convert Palette -->
EOL
}
put_footer(){
# put footer
echo "</ooo:color-table>" >> ${FILENAME}
}
put_header
tail -n +5 $GPL_FILE | while read; do
C=(${REPLY})
if [ "$(echo ${C[0]} | grep [0-9])" ]; then
COLORNAME=$(echo ${REPLY} | cut -f4- -d" ")
COLORCODE=$(printf "#%02x%02x%02x" ${C[0]} ${C[1]} ${C[2]})
echo " <draw:color draw:name=\"${COLORNAME}\" draw:color=\"${COLORCODE}\"/>" >> ${FILENAME}
fi
done
put_footer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment