3 ms·
I wanted to do something similar to figure out which of the 256 colours available in my terminal-emulator would be reasonably visible against a black background
by thristian 16y ago
I wanted to do something similar to figure out which of the 256 colours available in my terminal-emulator would be reasonably visible against a black background. I wrote a bash script to answer the question, at the heart of which was this function:
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 ))
}
It works like this:
$ get_luminance_from_color 16384 32768 16384
28101
...which is to say, then input is an RGB tuple where each field is 0-65535, and the output is a single integer between 0-65535 representing the luminance. If the luminance is > 32767, it's a light colour, otherwise it's a dark colour.