Skip to content

Instantly share code, notes, and snippets.

@mecab
Last active December 15, 2016 02:54
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 mecab/f1f8a87e06970ea9f46c8948897a9f31 to your computer and use it in GitHub Desktop.
Save mecab/f1f8a87e06970ea9f46c8948897a9f31 to your computer and use it in GitHub Desktop.
hashed_color(), which outputs sets unique background for given string and eye-friendly foregrand (white or black).
#!/bin/bash
color256()
{
# Output the color code from given RGB value.
# If the sole argument given, return it directly.
# One arg usage:
# - $1: color code [0-255]
#
# Three args usage:
# - $1: red [0-5]
# - $2: green [0-5]
# - $3: blue [0-5]
# To make fg256() and bg256() be convenient,
# when only one argument given, return it directly.
if [ "$#" -eq 1 ]; then
echo -n $1
return
fi
local red=$1;
local green=$2;
local blue=$3;
echo -n $[$red * 36 + $green * 6 + $blue + 16]
}
fg256()
{
# Output the escape sequence to set the foreground color to given color code
# or the color code calculated by given RGB value.
#
# One arg usage:
# - $1: color code [0-255]
#
# Three args usage:
# - $1: red [0-5]
# - $2: green [0-5]
# - $3: blue [0-5]
echo -n "\e[38;5;"$(color256 "$@")"m"
}
bg256()
{
# Output the escape sequence to set the foreground color to given color code
# or the color code calculated by given RGB value.
#
# One arg usage:
# - $1: color code [0-255]
#
# Three args usage:
# - $1: red [0-5]
# - $2: green [0-5]
# - $3: blue [0-5]
echo -n "\e[48;5;"$(color256 "$@")"m"
}
hashed_color() {
local md5=$(
(which md5sum || which md5 || \
echo ""
) | tail -n 1)
if [ -z "${md5}" ]; then
return
fi
local bgval_hex=$(echo $1|${md5}|cut -c1-2)
# `bgval` should be in range of 17-231
# as 0-15 represent system color and 232-255 is grayscale pallete.
# Restict range: 0-255 => 0-215.
# This value is used later to determine foreground color.
local bgval_clipped=$(printf "%d" "215*0x${bgval_hex}/255.0")
# Bias range: 0-216 => 16-231
local bgval=$(printf "%d" "${bgval_clipped}+16")
# (color code), (value in bgval_clipped) => (foreground color)
#
# 16-33 (0-17) => white
# 34-51 (18-35) => black
# 52-69 (36-53) => white
# ...
# 196-213 (180-197) => white
# 214-231 (198-215) => black
#
# Hence the foreground color can be represented as follows:
#
# ```
# is_fg_black = (bgval_clipped / 18) % 2 ? true : false
# ```
#
# See http://askubuntu.com/a/821163
local is_fg_black=$(printf "%d" "(${bgval_clipped}/18)%2")
if [ "${is_fg_black}" -eq 1 ]; then
local fgval=232 # system color INDEPENDENT white.
else
local fgval=255 # system color INDEPENDENT black.
fi
echo $(fg256 ${fgval})$(bg256 ${bgval})
}
hostcolor() {
hashed_color `hostname`
}
@mecab
Copy link
Author

mecab commented Nov 29, 2016

Example of using hostcolor() in the prompt

Example image

The prompt is provided by following:

autoload -Uz colors
colors

GREEN="%{$fg[green]%}"
LGREEN="%{$fg_bold[green]%}"
RED="%{$fg_bold[red]%}"
LRED="%{$fg_bold[red]%}"
RESET="%{$reset_color%}"
WHITE="%{$fg[white]%}"
LWHITE="%{$fg_bold[white]%}"
USERCOLOR="%(!.${LRED}.${LGREEN})"

prompt_1="${USERCOLOR}[%n@`hostcolor`%m${RESET} ${WHITE}%~${USERCOLOR}] ${RESET}${WHITE} %(1j,(%j,)${RESET}"
prompt_2="[%h]%{%B%}%(!.`emojify :anger:` .`emojify :moneybag:` )%{%b%"
PROMPT='${prompt_1}
${prompt_2} '

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment