Skip to content

Instantly share code, notes, and snippets.

@2bard
Last active January 27, 2024 11:24
Show Gist options
  • Save 2bard/6197239 to your computer and use it in GitHub Desktop.
Save 2bard/6197239 to your computer and use it in GitHub Desktop.
command line rgb to hex converter
EDIT: a quick google search revealed digitalcolor meter can show hex values so this task was pointless!
I've been doing some Android ui work recently. I keep finding myself needing to use a color from an image stored on my machine. I use OSX's digitalcolor meter tool to grab the rgb values from the image on screen then convert it into hex by asking google (e.g. searching '168 in hex').
Repeatedly doing this task can be frustrating (you might argue that I should just convert them in my head, but why do that when a computer can do it?). So I made a quick function in my .bash_profile which allows me to do it at the command line:
rgb2hex(){
for var in "$@"
do
printf '%x' "$var";
done
printf '\n'
}
example:
rgb2hex 168 255 124
a8ff7c
There are probably a million ways to do this, this is just my quick hacky method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment