Created
February 29, 2024 16:36
-
-
Save alifeee/586b92e21f912dde7f74cd498b7cbb56 to your computer and use it in GitHub Desktop.
Convert GIMP's `.gpl` file to CSS variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# convert a Gimp colour palette (.gpl) to a list of CSS variables | |
# example: | |
# ./gpl-to-css.sh my-palette.gpl > my-palette.css | |
# converts | |
# ----- my-palette.gpl ----- | |
# GIMP Palette | |
# Name: My Palette | |
# Columns: 3 | |
# 255 0 0 Red | |
# 0 255 0 Green | |
# 0 0 255 Blue | |
# --------------------------- | |
# to | |
# ----- my-palette.css ----- | |
# :root { | |
# --colour-Red: #ff0000; | |
# --colour-Green: #00ff00; | |
# --colour-Blue: #0000ff; | |
# } | |
# --------------------------- | |
# only supports 3-column colours | |
# you could edit the lines below to enable 4 colours by: | |
# - add another match to the regex | |
# - shift the indexes of the column items to add a fourth alpha column | |
# - add an A colour to the print statement | |
regex='^Columns: ([0-9]+)$' | |
if [[ $(grep -E "$regex" $1) =~ $regex ]]; | |
then | |
columns=${BASH_REMATCH[1]} | |
else | |
echo "Error: could not find columns in $1" >&2 | |
exit 1 | |
fi | |
if [[ $columns -ne 3 ]]; | |
then | |
echo "Error: only 3-column palettes are supported" >&2 | |
exit 1 | |
fi | |
cat $1 | awk -F ' ' ' | |
BEGIN { | |
printf ":root {\n" | |
} /^([0-9]{1,3})\s+([0-9]{1,3})\s+([0-9]{1,3})\s+(.*)$/ { | |
colourname = "" | |
for (i=4;i<=NF;i++) { | |
colourname = colourname "-" $i | |
} | |
printf " --colour%s: #%02x%02x%02x;\n", colourname, $1, $2, $3 | |
} END { | |
printf "}\n" | |
}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment