This is a tiny (140B) bash script to convert rgb
(0-255,0-255,0-255
) into hexadecimal
(000000-FFFFFF
).
#!/bin/bash
function hex() {
printf "%02X%02X%02X" ${*//','/' '}
}
[ $(basename -- $0) == "hex" ] && [ $# -gt 0 ] && echo $(hex "${*}")
It can interpret a few different input styles:
hex 255 255 255 # FFFFFF
hex 255,255,255 # FF0000
hex "128 200 250" # 80C8FA
Just drop hex.sh
in your $PATH
, after you make sure it's executable:
sudo chmod +x hex.sh
# my preference is $HOME/.local/bin
mv hex.sh /usr/local/bin/hex
You can also opt to source the code into another script, to call hex
programmatically:
[ -f ./hex.sh ] && . ./hex.sh
echo $(hex 255 0 0) #FF0000
I've tested all this on macOS (darwin) and Debian/Ubuntu, without any issues.
This also works on iOS viaiSH
(Alpine Linux). Windows... not so much.
MIT © 2021 Nicholas Berlette