Skip to content

Instantly share code, notes, and snippets.

@nelsonaloysio
Forked from nberlette/hex.sh
Last active January 10, 2024 21:04
Show Gist options
  • Save nelsonaloysio/0f3a432d779de14c64bd0e569db0c07c to your computer and use it in GitHub Desktop.
Save nelsonaloysio/0f3a432d779de14c64bd0e569db0c07c to your computer and use it in GitHub Desktop.
[bash] rgb to hex

hex.sh

This is a tiny bash function to convert rgb (0-255,0-255,0-255) into hexadecimal (000000-FFFFFF).

hex() { bash -c """printf "%02X%02X%02X" $(echo $* | tr , \ )"""; }

Usage

It can interpret a few different input styles:

hex 255 255 255   # FFFFFF
hex 255,255,255   # FF0000
hex "128 200 250" # 80C8FA

Install

Just drop the function in your ~/.bash_aliases (or .zshrc.local if you are using ZSH) and restart your terminal.

I've tested all this on macOS (darwin) and Debian/Ubuntu, without any issues.
This also works on iOS via iSH (Alpine Linux). Windows... not so much.

License

MIT © 2021 Nicholas Berlette

hex() { bash -c """printf "%02X%02X%02X" $(echo $* | tr , \ )"""; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment