djcev.com

//

Git Repos / dotfiles / fixcolors.sh

Last update to this file was on 2024-01-02 at 05:12.

Show fixcolors.sh

#!/bin/sh
#
# Copyright (c) 2015-2021, Cameron Vanderzanden. Available under the
# terms of the 2-clause BSD license. Please see the file "LICENSE" in
# the root dir of this repository for more information.
#
#- fixcolors.sh 2015/02/13 cev, last updated 2021/12/07
## Usage: fixcolors.sh [-dhVxX] [-c colordeffile] infile outfile
##
## Build a regex list and process a file. 'colordeffile' defines
## the variable "colors" which is then used to generate a command
## file (regex list). Sed then processes infile, using the regex list,
## and writes to outfile. Simple.
##
## -c Path to color definition file.
## -d RGB color output (255 255 255 form).
## -h Show help.
## -V Print version info.
## -x Hexadecimal RGB color output (#xxxxxx form). The default.
## -X Hexadecimal output without the prefixed #. (xxxxxx form).
##

[ -x $HOME/.local/bin/cev_print.sh ] && . $HOME/.local/bin/cev_print.sh

self="${0##*'/'}"
colordef="./colordef_gruvbox.sh"
mode="hex"

cleanup() {
if [ -n "$exprfile" -a -e $exprfile ]; then rm "$exprfile"; fi
}

die_colordefnotfound() {
print_self error
printf "%bcolor definition %b%s%b not found.%b\n"\
"$_norm" "$_error" "$*" "$_norm" "$_clear" >&2
exit 1
}

die_inputfiledoesnotexist() {
print_self error
printf "%binput file %b%s%b not found.%b\n" \
"$_norm" "$_error" "$1" "$_norm" "$_clear" >&2
exit 1
}

die_inputoutputsame() {
print_self error
printf "%binput file cannot be identical to output file.%b\n" \
"$_norm" "$_clear" >&2
exit 1
}

die_noinputfile() {
print_self error
printf "%bno input file specified.%b\n" "$_norm" "$_clear" >&2
exit 1
}

die_nooutputfile() {
print_self error
printf "%bno output file specified.%b\n" "$_norm" "$_clear" >&2
exit 1
}

while getopts "c:dhVxX?" option; do
case $option in
c) colordef="$OPTARG" ;;
d) mode="rgb" ;;
h) print_help && exit ;;
V) print_version && exit ;;
x) mode="hex" ;;
X) mode="hex" && suppress_pound="True" ;;
"?") print_help && exit ;;
esac
shift $((OPTIND - 1))
done

# make sure $1 was specified and exists
[ -z "$1" ] && die_noinputfile
[ ! -e "$1" ] && die_inputfiledoesnotexist "$1"
# make sure $2 was specified.
[ -z "$2" ] && die_nooutputfile
# make sure the two aren't the same
[ "$1" = "$2" ] && die_inputoutputsame

# source our color definitions.
if [ -e "$colordef" ]; then
. "$colordef"
else
die_colordefnotfound "$colordef"
fi

# build a big list of (simple) regexes in /tmp. Real ugly.
exprfile=$(mktemp ${TMPDIR:-/tmp}/${self}.1.XXXXXXXX)
trap cleanup EXIT
for h in $colors; do
# eval is scary.
if [ $mode = "hex" ]; then
eval color=\"\$${h}\"
if [ -n "$suppress_pound" ]; then
color=${color#'#'}
fi
elif [ $mode = "rgb" ]; then
# convert a hexadecimal color like '#rrggbb' into a decimal
# color triple of the form 'r,g,b'
eval hexcolor=\"\$${h}\"
for c in $(printf "%s\n" $hexcolor | sed 's/\(.\)/\1 /g'); do
if [ "$c" = "#" ]; then continue; fi
x="${x}${c}"
if [ ${#x} -eq 2 ]; then
d=$((0x${x}))
color="$color,$d"
unset d x
fi
done
color=${color#','}
fi
printf "s/%s/%s/g;\n" "${h}" "${color}" >> $exprfile
unset color
done

# chuck the mess at sed. This could be handled better...
sed -f $exprfile $1 > $2

Return to the top of this page or return to the overview of this repo.

Log fixcolors.sh

Return to the top of this page or return to the overview of this repo.