#!/usr/bin/env bash
# set -vx
# Copyright (C) 2022 Brian Hayes
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
printhelp() {
cat <&2
${2:+exit $2}
}
dependencycheck() {
local dep missingdependencies=0
for dep in "$@"; do
if ! command -v "$dep" &>/dev/null; then
error "dependency not met: $dep"
fi
done
if [[ $missingdependencies -gt 0 ]]; then
exit 1
fi
}
main() {
if [[ "$#" -lt 1 ]]; then
printhelp
fi
while getopts ":x:r:?" arg; do
case $arg in
x)
rgbval=$(hex_to_rgb "${2:-''}")
printf "%s\n" "$rgbval"
read -e -r -p "save code to clipboard?(y/n): " clipit
if [[ "$clipit" == "yes" || "$clipit" == "y" ]]; then
dependencycheck xclip
printf "%s" "$rgbval" | xclip -sel clip
fi
;;
r)
hexval=$(rgb_to_hex "${2:-''}")
printf "%s\n" "$hexval"
read -e -r -p "save code to clipboard?(y/n): " clipit
if [[ "$clipit" == "yes" || "$clipit" == "y" ]]; then
dependencycheck xclip
printf "%s" "$hexval" | xclip -sel clip
fi
;;
?)
printhelp
;;
esac
done
}
main "$@"
# Credits:
# hex_to_rgb() and rgb_to_hex() taken from Akash Mittal:
# https://akashmittal.com/code-example-convert-hex-color-to-rgb-rgb-to-hex-using-bash-script/
# https://www.linkedin.com/in/akashrishimittal/
#
# error() and dependencycheck() copied from tomocafe:
# https://github.com/tomocafe