Skip to content

Instantly share code, notes, and snippets.

@stenyak
Created September 22, 2014 10:21
Show Gist options
  • Save stenyak/0c43c9a1df3f9f207157 to your computer and use it in GitHub Desktop.
Save stenyak/0c43c9a1df3f9f207157 to your computer and use it in GitHub Desktop.
#!/bin/bash
function addcolor
{
local newcolor="\e[${3}m\033[$1;${2}m"
colors+=("$newcolor")
#echo -e "${newcolor}Adding new color$RESET: $1, $2, $3"
}
function askAllColors
{
if [ -f "$cfg" ]
then
echo "About to replace existing list. Press ^C to cancel, enter to continue."
read
fi
for i in $(seq 1 1); do for j in $(seq 29 37); do for k in $(seq 39 47)
do
local newcolor="\E[${k}m\033[$i;${j}m"
echo -ne "Use this ${newcolor}color combination$RESET? (y/n): "
local answer=""
read answer
if [ "$answer" == "y" ]
then
echo "addcolor $i $j $k" >> $cfg
fi
done; done; done
}
function addReadableColors
{
if [ -f "$cfg" ]
then
source $cfg
else
addcolor 1 37 41
addcolor 1 32 41
addcolor 1 33 41
addcolor 1 34 41
addcolor 1 36 41
addcolor 1 35 42
addcolor 1 34 42
addcolor 1 33 42
addcolor 1 31 42
addcolor 1 31 43
addcolor 1 34 43
addcolor 1 35 43
addcolor 1 30 44
addcolor 1 31 44
addcolor 1 32 44
addcolor 1 36 44
addcolor 1 32 45
addcolor 1 33 45
addcolor 1 34 45
addcolor 1 36 45
addcolor 1 31 46
addcolor 1 34 46
addcolor 1 35 46
addcolor 1 31 47
addcolor 1 34 47
addcolor 1 35 47
fi
}
function colorize
{
local str="$*"
local offset=1
local num="$(( ($offset+$(echo $str |sum |sed "s/^0*//g" |sed "s/ .*//g")) % ${#colors[@]} ))"
echo -ne "${colors[$num]}"
}
function colorize_main
{
local RESET="\033[00m"
local colors=()
local cfg="$HOME/.colors.txt"
if [ -z "$*" ]
then
askAllColors
else
addReadableColors
colorize "$@"
fi
}
#if run directly, colorize stuff
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && colorize_main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment