Skip to content

Instantly share code, notes, and snippets.

@tsibley
Created March 21, 2025 17:28
Show Gist options
  • Save tsibley/180a7228a8517c122722e1e06b5a3ab3 to your computer and use it in GitHub Desktop.
Save tsibley/180a7228a8517c122722e1e06b5a3ab3 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
main() {
pick | parse | convert | tee >(copy >/dev/null) | format
}
pick() {
gdbus call --session \
--dest org.gnome.Shell.Screenshot \
--object-path /org/gnome/Shell/Screenshot \
--method org.gnome.Shell.Screenshot.PickColor
}
parse() {
# parse this: ({'color': <(1.0, 1.0, 0.8666666666666667)>},)
jq --raw-input --slurp '
capture("<[(](?<r>\\S+), (?<g>\\S+), (?<b>\\S+)[)]>")
| map_values(tonumber * 255 | nearbyint)
'
}
convert() {
perl -MJSON -0nle '
$_ = decode_json($_);
$_->{rgb} = join ", ", @$_{qw<r g b>};
$_->{hex} = "#" . join "", map { sprintf "%02x", $_ } @$_{qw<r g b>};
print encode_json($_);
'
}
format() {
jq --raw-output --join-output '
"rgb: \(.rgb)\n",
"hex: \(.hex)\n"
'
}
copy() {
jq --raw-output --join-output .hex | xclip -i -selection clipboard
echo copied hex to clipboard >&2
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment