From addaa750ec1dbd4495b63660c3d76e9cdca82c71 Mon Sep 17 00:00:00 2001 From: z3rOR0ne Date: Mon, 19 Dec 2022 17:27:44 -0800 Subject: [PATCH] :sparkles: Finished dye, updated dependency check --- scripts/dependencycheck | 26 +++++------ scripts/dye | 95 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 13 deletions(-) create mode 100755 scripts/dye diff --git a/scripts/dependencycheck b/scripts/dependencycheck index 132d1e92..eb36c552 100755 --- a/scripts/dependencycheck +++ b/scripts/dependencycheck @@ -36,22 +36,22 @@ function dependencycheck() # invoke function comment out if sourced dependencycheck "$@" -# Barebones version without tput (for being sourced): -# function dependencycheck() -# { - # numdependencies="$#" - # dependencies=("$@") - # missingdependencies=0 +# Better simple version from tomocafe (see upnup script) +# For use within scripts, not on its own - # for ((i = 0; i < numdependencies; i++)) ; do - # if ! command -v "${dependencies[$i]}" &> /dev/null ; then - # echo "dependency not met: ${dependencies[$i]}" - # missingdependencies=$((missingdependencies+1)) +#error() { + # printf "error: %s\n" "$1" 1>&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 - # echo "Please install needed dependencies" + # if [[ $missingdependencies -gt 0 ]] ; then # exit 1 # fi # } diff --git a/scripts/dye b/scripts/dye new file mode 100755 index 00000000..6d808753 --- /dev/null +++ b/scripts/dye @@ -0,0 +1,95 @@ +#!/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 << EOM +usage: dye [-h|-r] [color_code] +options: + -x hex code to rgb + -r rgb to hex +examples: + dye -x "#FFFFFF" + dye -r 255 255 255 +EOM + exit 1 +} + +hex_to_rgb() { + : "${1/\#}" + ((r=16#${_:0:2},g=16#${_:2:2},b=16#${_:4:2})) + printf '%s\n' "rgba($r, $g, $b)" +} + +rgb_to_hex() { + printf '#%02x%02x%02x\n' "$1" "$2" "$3" +} + +error() { + printf "error: %s\n" "$1" 1>&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() { + while getopts ":x:r:?" arg; do + case $arg in + x) + rgbaval=$(hex_to_rgb "${2:-''}") + printf "%s\n" "$rgbaval" + read -e -r -p "save code to clipboard?(y/n): " clipit + if [[ "$clipit" == "yes" || "$clipit" == "y" ]]; then + dependencycheck xclip + printf "%s" "$rgbaval" | 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