Skip to content

Instantly share code, notes, and snippets.

@Screwtapello
Created March 26, 2012 04:04
Show Gist options
  • Save Screwtapello/2202884 to your computer and use it in GitHub Desktop.
Save Screwtapello/2202884 to your computer and use it in GitHub Desktop.
Which are the bright terminal colours?
#!/bin/bash
get_color_from_palette() {
# Query the terminal for the current value of a palette entry.
# Prints three decimal numbers from 0-65535 representing the red, green and
# blue components of the given palette entry.
local paletteNum="$1"
# This function only works in xterm and (some) derivatives
if [ "${TERM:0:5}" != "xterm" ]; then
return
fi
# Issue the query
echo -n $'\e]4;'"$paletteNum"$';?\e\\' > /dev/tty 2> /dev/null ||
return # Assume we have no tty to talk to, give up.
# xterm responds with a string terminated with ^G, if it responds at all.
read -d $'\a' -s -t 1 < /dev/tty
if [ "$REPLY" == $'\n' ]; then
# We got no response, give up.
return
fi
# Strip the escape-code bits off the beginning; read already changed the
# terminating ^G to a safe newline. We assume the response is of the form:
#
# rgb:RRRR/GGGG/BBBB
#
# where RRRR, GGGG and BBBB are hex-digits.
local raw_result=${REPLY#*:} # strip off everything up to the first :
local result=${raw_result//\// } # change "/"s to " "s
local component
for component in $result; do
echo $(( 0x$component ))
done
}
get_palette() {
# Query the terminal for the current palette.
# Returns one line per palette entry, each containing three numbers each
# from 0-65535 representing the red, green and blue components of the given
# entry.
local index
local pal_size=$(tput colors)
local raw_result
local result
local component
# Check we can actually talk to the terminal, first.
echo -n "" > /dev/tty 2> /dev/null || return
# Run this in a subshell so we can do the redirections once and hopefully
# not miss any bytes.
(
# Send our query
echo -n $'\e]4;'
for (( index=0 ; index < $pal_size ; index++ )); do
echo "$index;?;"
done
echo -n $'\a'
# Read our responses
for (( index=0 ; index < $pal_size ; index++ )); do
# xterm responds with a string terminated with ^G, if it responds
# at all.
read -d $'\a' -s -t 1
if [ $? -gt 0 ]; then
# We got no response, give up.
echo "no response" >&2
return
else
raw_result=${REPLY#*:} # strip off everything up to the first :
result=${raw_result//\// } # change "/"s to " "s
for component in $result; do
echo -n "$(( 0x$component )) "
done
echo
fi
done
) #< /dev/tty > /dev/tty
}
get_luminance_from_color() {
# Return the luminance of a color as a number 0-65535.
local red=$1
local green=$2
local blue=$3
# Colour weights from Wikipedia. Note bash doesn't have floats.
echo $(( (2126 * $red + 7152 * $green + 722 * $blue) / 10000 ))
}
get_luminance_from_palette() {
local color=$(get_color_from_palette "$@")
if [ -z "$color" ]; then
# can't get colors from this terminal.
return
fi
get_luminance_from_color $color
}
get_bright_colors() {
# Print a list of palette indices visible against a bright background.
local index
local colors=$(tput colors)
for (( index=0 ; index < colors ; index++ )); do
local luma=$(get_luminance_from_palette $index)
if [ -z "$luma" ]; then
# can't get colours from this terminal.
return
fi
if [ "$luma" -ge 16384 ]; then
tput setaf $index; tput setab 0
printf "%s" $index
tput sgr0
printf " "
fi
done
echo
}
get_bright_colors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment