#!/usr/bin/env bash set -euo pipefail # Set default modes json_mode=0 alpha_mode=0 name_mode=0 # Function to display help message show_help() { cat <&2 return 1 fi local r=$((16#${clean_hex:0:2})) local g=$((16#${clean_hex:2:2})) local b=$((16#${clean_hex:4:2})) local a=$((16#${clean_hex:6:2})) local name="" if [[ $name_mode -eq 1 ]]; then local api="https://www.thecolorapi.com/id?hex=${clean_hex:0:6}" name=$(curl -s "$api" | jq -r '.name.value // empty') fi if [[ $json_mode -eq 1 ]]; then if [[ $alpha_mode -eq 1 ]]; then printf '{"hex":"#%s","rgba":[%d,%d,%d,%d]' "$clean_hex" "$r" "$g" "$b" "$a" else printf '{"hex":"#%s","rgb":[%d,%d,%d]' "${clean_hex:0:6}" "$r" "$g" "$b" fi [[ -n "$name" ]] && printf ',"name":"%s"' "$name" echo "}" else if [[ $alpha_mode -eq 1 ]]; then printf "\033[2mHEX:\033[0m #%s \033[2m→ RGBA(\033[0m%3d, %3d, %3d, %3d\033[2m)\033[0m → \033[48;2;%d;%d;%dm \033[0m" \ "$clean_hex" "$r" "$g" "$b" "$a" "$r" "$g" "$b" else printf "\033[2mHEX:\033[0m #%s \033[2m→ RGB(\033[0m%3d, %3d, %3d\033[2m)\033[0m → \033[48;2;%d;%d;%dm \033[0m" \ "${clean_hex:0:6}" "$r" "$g" "$b" "$r" "$g" "$b" fi [[ -n "$name" ]] && printf " \033[2m→ Name:\033[0m %s" "$name" echo fi } # Main if [[ $# -eq 0 ]] && [[ -t 0 ]]; then show_help exit 0 fi args=() while [[ $# -gt 0 ]]; do case "$1" in --help | -h) show_help exit 0 ;; --pick) if ! command -v wl-colorpicker-plasma &>/dev/null; then echo "Error: wl-colorpicker-plasma not found in PATH" >&2 exit 1 fi picked="$(wl-colorpicker-plasma)" [[ -n "$picked" ]] && args+=("$picked") ;; --json) json_mode=1 ;; --alpha) alpha_mode=1 ;; --name) name_mode=1 ;; -*) echo "Unknown option: $1" >&2 exit 1 ;; *) args+=("$1") ;; esac shift done # If input is piped in, add to args if [[ ! -t 0 ]]; then while read -r line; do [[ -n "$line" ]] && args+=("$line") done fi # Run conversion for hex in "${args[@]}"; do hex_to_rgba "$hex" done