Skip to content

Instantly share code, notes, and snippets.

@pborenstein
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pborenstein/5e37105c18b21b9636ad to your computer and use it in GitHub Desktop.
Save pborenstein/5e37105c18b21b9636ad to your computer and use it in GitHub Desktop.
Color functions
From: http://misc.flogisoft.com/bash/tip_colors_and_formatting
This is working for me for now (it's only lightly tested, fails gracefully if the numbers are too high, fails ungracefully if you give too few arguments, and uses features in bash new to me (hey, been away for awhile), so not sure if they are standard or new... with that out of the way:
Now I can do things like echo "$(c_grey 15)I'm light Grey$(c_clr)" and
echo "$(cb_rgb 5 0 0)Red$(cb_rgb 0 5 0)Green$(cb_rgb 0 0 5)Blue$(c_clr)"
At any rate, hope this helps someone.
function c_clr() { echo -en "\e[0m" ; }
function c_ansi() { [ $1 -lt 16 ] && echo -en "\e[38;5;$1m" || c_clr ; }
function c_grey() { [ $1 -lt 24 ] && echo -en "\e[38;5;$(($1+232))m" || c_clr ; }
function c_rgb() { [ $1 -lt 6 -a $2 -lt 6 -a $3 -lt 6 ] && echo -en "\e[38;5;$((16+(36*$1)+(6*$2)+$3))m" || c_clr ; }
function cb_ansi() { [ $1 -lt 16 ] && echo -en "\e[48;5;$1m" || c_clr ; }
function cb_grey() { [ $1 -lt 24 ] && echo -en "\e[48;5;$(($1+232))m" || c_clr ; }
function cb_rgb() { [ $1 -lt 6 -a $2 -lt 6 -a $3 -lt 6 ] && echo -en "\e[48;5;$((16+(36*$1)+(6*$2)+$3))m" || c_clr ; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment