Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 344fc140dc | |||
|
5f659b90e3
|
|||
|
d170bd15cf
|
@@ -84,20 +84,22 @@ You can define your preferred defaults in `~/.config/color-tool/config.toml`. Th
|
|||||||
|
|
||||||
```toml
|
```toml
|
||||||
[defaults]
|
[defaults]
|
||||||
output = "hex" # default output format(s)
|
# Set any option to true to enable it by default when using the terminal
|
||||||
|
output = "hex" # default output format(s): hex, rgb, hsl, rgba, hsla, hexa, all
|
||||||
json = false # output in JSON format
|
json = false # output in JSON format
|
||||||
swatch = false # show color swatch in terminal
|
swatch = false # show color swatch in terminal
|
||||||
name = false # fetch color name
|
name = false # fetch color name from thecolorapi.com
|
||||||
copy = false # copy result to clipboard
|
copy = false # copy result to clipboard
|
||||||
pick = false # auto-launch picker
|
pick = false # auto-launch color picker when invoked with no arguments
|
||||||
notify = false # show desktop notification
|
notify = false # show desktop notification
|
||||||
|
|
||||||
[desktop]
|
[desktop]
|
||||||
output = "hex"
|
# Defaults for --desktop mode (launched from the app menu; copy is always enabled by default)
|
||||||
json = false
|
output = "hex" # format to copy
|
||||||
name = false
|
json = false # copy JSON format instead of plain text
|
||||||
copy = true
|
name = false # fetch color name (requires network)
|
||||||
notify = true
|
copy = true # copy result to clipboard
|
||||||
|
notify = true # show desktop notification with the copied value
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🤝 Credits
|
## 🤝 Credits
|
||||||
|
|||||||
+66
-24
@@ -76,7 +76,8 @@ load_config() {
|
|||||||
if [[ "$line" =~ ^[[:space:]]*([A-Za-z_]+)[[:space:]]*=[[:space:]]*([^[:space:]]+) ]]; then
|
if [[ "$line" =~ ^[[:space:]]*([A-Za-z_]+)[[:space:]]*=[[:space:]]*([^[:space:]]+) ]]; then
|
||||||
key="${BASH_REMATCH[1]}"
|
key="${BASH_REMATCH[1]}"
|
||||||
val="${BASH_REMATCH[2],,}"
|
val="${BASH_REMATCH[2],,}"
|
||||||
val="${val#\"}" ; val="${val%\"}"
|
val="${val#\"}"
|
||||||
|
val="${val%\"}"
|
||||||
case "$section:$key" in
|
case "$section:$key" in
|
||||||
defaults:json) [[ "$val" == "true" ]] && json_mode=1 || json_mode=0 ;;
|
defaults:json) [[ "$val" == "true" ]] && json_mode=1 || json_mode=0 ;;
|
||||||
defaults:alpha) [[ "$val" == "true" ]] && alpha_mode=1 || alpha_mode=0 ;;
|
defaults:alpha) [[ "$val" == "true" ]] && alpha_mode=1 || alpha_mode=0 ;;
|
||||||
@@ -94,7 +95,7 @@ load_config() {
|
|||||||
desktop:output) desktop_output="$val" ;;
|
desktop:output) desktop_output="$val" ;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
done < "$CONFIG_FILE"
|
done <"$CONFIG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── Help ──────────────────────────────────────────────────────────────────────
|
# ── Help ──────────────────────────────────────────────────────────────────────
|
||||||
@@ -159,7 +160,7 @@ copy_to_clipboard() {
|
|||||||
elif command -v xclip &>/dev/null; then
|
elif command -v xclip &>/dev/null; then
|
||||||
printf '%s' "$text" | xclip -selection clipboard
|
printf '%s' "$text" | xclip -selection clipboard
|
||||||
else
|
else
|
||||||
echo "Warning: no clipboard utility found (install wl-copy or xclip)" >&2
|
echo "Warning: Missing clipboard utility. Please install wl-clipboard (preferred) or xclip." >&2
|
||||||
echo " Value: $text" >&2
|
echo " Value: $text" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
@@ -171,12 +172,18 @@ notify_result() {
|
|||||||
notify-send -i "color-picker" "color-tool" "$value" || true
|
notify-send -i "color-picker" "color-tool" "$value" || true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
notify_error() {
|
||||||
|
local value="$1"
|
||||||
|
command -v notify-send &>/dev/null || return 0
|
||||||
|
notify-send -u normal -i "dialog-warning" "color-tool" "$value" || true
|
||||||
|
}
|
||||||
|
|
||||||
# ── Color picker ──────────────────────────────────────────────────────────────
|
# ── Color picker ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
# Generate the internal Python helper for KDE Plasma color picking
|
# Generate the internal Python helper for KDE Plasma color picking
|
||||||
generate_picker_script() {
|
generate_picker_script() {
|
||||||
local target="$1"
|
local target="$1"
|
||||||
cat > "$target" <<'EOF'
|
cat >"$target" <<'EOF'
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
#
|
#
|
||||||
# Original work Copyright (C) 2024 SASUPERNOVA
|
# Original work Copyright (C) 2024 SASUPERNOVA
|
||||||
@@ -273,7 +280,7 @@ do_install() {
|
|||||||
local config_file="$config_dir/config.toml"
|
local config_file="$config_dir/config.toml"
|
||||||
mkdir -p "$config_dir"
|
mkdir -p "$config_dir"
|
||||||
if [[ ! -f "$config_file" ]]; then
|
if [[ ! -f "$config_file" ]]; then
|
||||||
cat > "$config_file" <<'EOF'
|
cat >"$config_file" <<'EOF'
|
||||||
# color-tool configuration
|
# color-tool configuration
|
||||||
# https://github.com/rootiest/color-tool
|
# https://github.com/rootiest/color-tool
|
||||||
|
|
||||||
@@ -281,7 +288,6 @@ do_install() {
|
|||||||
# Set any option to true to enable it by default when using the terminal
|
# Set any option to true to enable it by default when using the terminal
|
||||||
output = "hex" # default output format(s): hex, rgb, hsl, rgba, hsla, hexa, all
|
output = "hex" # default output format(s): hex, rgb, hsl, rgba, hsla, hexa, all
|
||||||
json = false # output in JSON format
|
json = false # output in JSON format
|
||||||
alpha = false # include alpha channel (8-digit hex)
|
|
||||||
swatch = false # show color swatch in terminal
|
swatch = false # show color swatch in terminal
|
||||||
name = false # fetch color name from thecolorapi.com
|
name = false # fetch color name from thecolorapi.com
|
||||||
copy = false # copy result to clipboard
|
copy = false # copy result to clipboard
|
||||||
@@ -292,7 +298,6 @@ notify = false # show desktop notification
|
|||||||
# Defaults for --desktop mode (launched from the app menu; copy is always enabled by default)
|
# Defaults for --desktop mode (launched from the app menu; copy is always enabled by default)
|
||||||
output = "hex" # format to copy
|
output = "hex" # format to copy
|
||||||
json = false # copy JSON format instead of plain text
|
json = false # copy JSON format instead of plain text
|
||||||
alpha = false # include alpha channel
|
|
||||||
name = false # fetch color name (requires network)
|
name = false # fetch color name (requires network)
|
||||||
copy = true # copy result to clipboard
|
copy = true # copy result to clipboard
|
||||||
notify = true # show desktop notification with the copied value
|
notify = true # show desktop notification with the copied value
|
||||||
@@ -306,7 +311,7 @@ EOF
|
|||||||
local desktop_file="$app_dir/color-tool.desktop"
|
local desktop_file="$app_dir/color-tool.desktop"
|
||||||
local bin_path="$HOME/.local/bin/color-tool"
|
local bin_path="$HOME/.local/bin/color-tool"
|
||||||
mkdir -p "$app_dir"
|
mkdir -p "$app_dir"
|
||||||
cat > "$desktop_file" <<EOF
|
cat >"$desktop_file" <<EOF
|
||||||
[Desktop Entry]
|
[Desktop Entry]
|
||||||
Version=1.1
|
Version=1.1
|
||||||
Type=Application
|
Type=Application
|
||||||
@@ -381,7 +386,7 @@ print(json.dumps(formats))
|
|||||||
validate_output_formats() {
|
validate_output_formats() {
|
||||||
local formats="$1"
|
local formats="$1"
|
||||||
local valid_fmts=("hex" "hexa" "rgb" "rgba" "hsl" "hsla")
|
local valid_fmts=("hex" "hexa" "rgb" "rgba" "hsl" "hsla")
|
||||||
IFS=',' read -ra ADDR <<< "$formats"
|
IFS=',' read -ra ADDR <<<"$formats"
|
||||||
for fmt in "${ADDR[@]}"; do
|
for fmt in "${ADDR[@]}"; do
|
||||||
[[ "$fmt" == "all" ]] && continue
|
[[ "$fmt" == "all" ]] && continue
|
||||||
local is_valid=0
|
local is_valid=0
|
||||||
@@ -398,7 +403,10 @@ validate_output_formats() {
|
|||||||
process_color() {
|
process_color() {
|
||||||
local input="$1"
|
local input="$1"
|
||||||
local formats_json
|
local formats_json
|
||||||
formats_json=$(get_all_formats "$input") || { echo "Error: Invalid color: $input" >&2; return 1; }
|
formats_json=$(get_all_formats "$input") || {
|
||||||
|
echo "Error: Invalid color: $input" >&2
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
local name=""
|
local name=""
|
||||||
if [[ $name_mode -eq 1 ]]; then
|
if [[ $name_mode -eq 1 ]]; then
|
||||||
@@ -409,7 +417,7 @@ process_color() {
|
|||||||
|
|
||||||
local selected_fmts=()
|
local selected_fmts=()
|
||||||
local valid_fmts=("hex" "hexa" "rgb" "rgba" "hsl" "hsla")
|
local valid_fmts=("hex" "hexa" "rgb" "rgba" "hsl" "hsla")
|
||||||
IFS=',' read -ra ADDR <<< "$output_formats"
|
IFS=',' read -ra ADDR <<<"$output_formats"
|
||||||
for fmt in "${ADDR[@]}"; do
|
for fmt in "${ADDR[@]}"; do
|
||||||
if [[ "$fmt" == "all" ]]; then
|
if [[ "$fmt" == "all" ]]; then
|
||||||
selected_fmts=("${valid_fmts[@]}")
|
selected_fmts=("${valid_fmts[@]}")
|
||||||
@@ -436,7 +444,10 @@ process_color() {
|
|||||||
if [[ $json_mode -eq 1 ]]; then
|
if [[ $json_mode -eq 1 ]]; then
|
||||||
output_text="$json_obj"
|
output_text="$json_obj"
|
||||||
else
|
else
|
||||||
output_text=$(IFS=' ' ; echo "${display_parts[*]}")
|
output_text=$(
|
||||||
|
IFS=' '
|
||||||
|
echo "${display_parts[*]}"
|
||||||
|
)
|
||||||
[[ -n "$name" ]] && output_text="$output_text ($name)"
|
[[ -n "$name" ]] && output_text="$output_text ($name)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -446,14 +457,29 @@ process_color() {
|
|||||||
r=$(echo "$formats_json" | jq -r '._raw.r')
|
r=$(echo "$formats_json" | jq -r '._raw.r')
|
||||||
g=$(echo "$formats_json" | jq -r '._raw.g')
|
g=$(echo "$formats_json" | jq -r '._raw.g')
|
||||||
b=$(echo "$formats_json" | jq -r '._raw.b')
|
b=$(echo "$formats_json" | jq -r '._raw.b')
|
||||||
if [[ $json_mode -eq 1 ]]; then printf "\033[48;2;${r};${g};${b}m \033[0m\n"
|
if [[ $json_mode -eq 1 ]]; then
|
||||||
|
printf "\033[48;2;${r};${g};${b}m \033[0m\n"
|
||||||
else printf "\033[48;2;${r};${g};${b}m \033[0m "; fi
|
else printf "\033[48;2;${r};${g};${b}m \033[0m "; fi
|
||||||
fi
|
fi
|
||||||
echo -e "$output_text"
|
echo -e "$output_text"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ $copy_mode -eq 1 ]] && copy_to_clipboard "$output_text" || true
|
local copy_failed=0
|
||||||
[[ $notify_mode -eq 1 ]] && notify_result "$output_text" || true
|
if [[ $copy_mode -eq 1 ]]; then
|
||||||
|
if ! copy_to_clipboard "$output_text"; then
|
||||||
|
copy_failed=1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $notify_mode -eq 1 ]]; then
|
||||||
|
if [[ $copy_failed -eq 1 ]]; then
|
||||||
|
notify_error "Missing clipboard utility. Please install wl-clipboard (preferred) or xclip.
|
||||||
|
|
||||||
|
Value: $output_text"
|
||||||
|
else
|
||||||
|
notify_result "$output_text"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── Argument parsing ──────────────────────────────────────────────────────────
|
# ── Argument parsing ──────────────────────────────────────────────────────────
|
||||||
@@ -465,25 +491,40 @@ do_pick=0
|
|||||||
# First pass: collect CLI overrides
|
# First pass: collect CLI overrides
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
--help|-h) show_help; exit 0 ;;
|
--help | -h)
|
||||||
--output) cli_output="$2"; shift ;;
|
show_help
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
--output)
|
||||||
|
cli_output="$2"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
--json) cli_json=1 ;;
|
--json) cli_json=1 ;;
|
||||||
--no-json) cli_json=0 ;;
|
--no-json) cli_json=0 ;;
|
||||||
--alpha) cli_alpha=1; cli_output="hexa" ;;
|
--alpha)
|
||||||
--no-alpha)cli_alpha=0 ;;
|
cli_alpha=1
|
||||||
|
cli_output="hexa"
|
||||||
|
;;
|
||||||
|
--no-alpha) cli_alpha=0 ;;
|
||||||
--name) cli_name=1 ;;
|
--name) cli_name=1 ;;
|
||||||
--no-name) cli_name=0 ;;
|
--no-name) cli_name=0 ;;
|
||||||
--swatch) cli_swatch=1 ;;
|
--swatch) cli_swatch=1 ;;
|
||||||
--no-swatch)cli_swatch=0 ;;
|
--no-swatch) cli_swatch=0 ;;
|
||||||
--copy) cli_copy=1 ;;
|
--copy) cli_copy=1 ;;
|
||||||
--no-copy) cli_copy=0 ;;
|
--no-copy) cli_copy=0 ;;
|
||||||
--notify) cli_notify=1 ;;
|
--notify) cli_notify=1 ;;
|
||||||
--no-notify)cli_notify=0 ;;
|
--no-notify) cli_notify=0 ;;
|
||||||
--pick) cli_pick=1 ;;
|
--pick) cli_pick=1 ;;
|
||||||
--no-pick) cli_pick=0 ;;
|
--no-pick) cli_pick=0 ;;
|
||||||
--desktop) desktop_mode=1 ;;
|
--desktop) desktop_mode=1 ;;
|
||||||
--install) do_install; exit 0 ;;
|
--install)
|
||||||
-*) echo "Unknown option: $1" >&2; exit 1 ;;
|
do_install
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-*)
|
||||||
|
echo "Unknown option: $1" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
*) args+=("$1") ;;
|
*) args+=("$1") ;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
@@ -525,7 +566,8 @@ elif [[ ${#args[@]} -eq 0 ]]; then
|
|||||||
picked="$(run_color_picker)" || exit 1
|
picked="$(run_color_picker)" || exit 1
|
||||||
[[ -n "$picked" ]] && args+=("$picked")
|
[[ -n "$picked" ]] && args+=("$picked")
|
||||||
elif [[ -t 0 ]]; then
|
elif [[ -t 0 ]]; then
|
||||||
show_help; exit 0
|
show_help
|
||||||
|
exit 0
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user