Skip to content

Instantly share code, notes, and snippets.

@khalidchawtany
Forked from XVilka/TrueColour.md
Last active August 29, 2015 14:22
Show Gist options
  • Save khalidchawtany/3ee7d71a7bbacefd98b4 to your computer and use it in GitHub Desktop.
Save khalidchawtany/3ee7d71a7bbacefd98b4 to your computer and use it in GitHub Desktop.

Colors in terminal

It's a common confusion about terminal colors... Actually we have this:

  • plain ascii
  • ansi escape codes (16 color codes with bold/italic and background)
  • 256 color palette (216 colors+16gray + ansi) (colors are 24bit)
  • 24bit true color (888 colors (aka 16 milion))
printf "\x1b[${bg};2;${red};${green};${blue}m\n"

The 256 color palete is configured at start, and it's a 666 cube of colors, each of them defined as a 24bit (888 rgb) color.

This means that current support can only display 256 different colors in the terminal, while truecolor means that you can display 16 milion different colors at the same time.

Truecolor escape codes doesnt uses a color palete. It just specifies the color itself.

Here's a test case:

printf "\x1b[38;2;255;100;0mTRUECOLOR\x1b[0m\n"

Keep in mind that it is possible to use both ';' and ':' as parameters delimiter.

According to Wikipedia[1], this is only supported by xterm and konsole.

[1] https://en.wikipedia.org/wiki/ANSI_color

Here are terminals discussions:

Now supporting truecolor

But there are bunch of libvte-based terminals for GTK2 so they are listed in the another section.

Parsing ANSI color sequences, but approximating them to 256 palette

  • xterm (though doing it wrong: "it uses nearest colour in RGB colour space, with a usualfalse assumption about orthogonal axes")

Note about colour differences: a) RGB axes are not orthogonal, so you cannot use sqrt(R^2+G^2+B^2) formula, b) for colour differences there is more correct (but much more complex) CIEDE2000 formula (which may easily blow up performance if used blindly) [2].

[2] neovim/neovim#793 (comment)

NOT supporting truecolor

[3] You can see the commit here https://github.com/rr-/mintty/commit/9bfac3353a093c904fcdb4a14f12aae483bf83c5

[4] You can download patched version here https://github.com/rdebath/PuTTY

Terminal multiplexers

[5] Currently you can use tmux_escape option as a workaround if you want true color in shell run under tmux. No true color in tmux statusline though.

[6] You can see the patch here https://gist.github.com/JohnMorales/0579990993f6dec19e83

Here are another console programs discussions:

Supporting True Color:

Not supporting True Color:

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