Code Monkey home page Code Monkey logo

colors's Introduction

Terminal Colors

(Previously published and discussed at https://gist.github.com/XVilka/8346728.)

There exists common confusion about terminal colors. This is what we have right now:

  • Plain ASCII
  • ANSI escape codes: 16 color codes with bold/italic and background
  • 256 color palette: 216 colors + 16 ANSI + 24 gray (colors are 24-bit)
  • 24-bit truecolor: "888" colors (aka 16 million)
printf "\x1b[${bg};2;${red};${green};${blue}m\n"

The 256-color palette is configured at start and is a 666-cube of colors, each of them defined as a 24-bit (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 million different colors at the same time.

Truecolor escape codes do not use a color palette. They just specify the color directly.

For a quick check of your terminal, run:

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

which will print TRUECOLOR in brown if it understands Xterm-style true-color escapes.

For a more thorough test, run:

awk 'BEGIN{
    s="/\\/\\/\\/\\/\\"; s=s s s s s s s s;
    for (colnum = 0; colnum<77; colnum++) {
        r = 255-(colnum*255/76);
        g = (colnum*510/76);
        b = (colnum*255/76);
        if (g>255) g = 510-g;
        printf "\033[48;2;%d;%d;%dm", r,g,b;
        printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
        printf "%s\033[0m", substr(s,colnum+1,1);
    }
    printf "\n";
}'

Some other tests:

You can download these scripts and inspect them before running them, by:
# make a sandbox
mkdir tmp$$
cd tmp$$

# downloads scripts
wget https://github.com/robertknight/konsole/raw/master/tests/color-spaces.pl \
     https://gist.github.com/lilydjwg/fdeaf79e921c2f413f44b6f613f6ad53/raw/94d8b2be62657e96488038b0e547e3009ed87d40/colors.py \
     https://github.com/JohnMorales/dotfiles/raw/master/colors/24-bit-color.sh \
     https://gitlab.gnome.org/GNOME/vte/-/raw/master/perf/img.sh

# read the scripts with your editor
$EDITOR *

Stop! Only if you're satisfied that the scripts are trustworthy should you proceed:

# if you trust them, run them
perl color-spaces.pl
python colors.py
bash 24-bit-color.sh
bash img.sh

Keep in mind that it is possible to use both ';' and ':' as Control Sequence delimiters.

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

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

Truecolor Detection

Checking for COLORTERM

VTE, Konsole and iTerm2 all advertise truecolor support by placing COLORTERM=truecolor in the environment of the shell user's shell. This has been in VTE for a while, but is relatively new in Konsole and iTerm2 and has to be enabled at compile time (most packages do not, so you have to compile them yourself from the git source repo).

The S-Lang library has a check that $COLORTERM contains either "truecolor" or "24bit" (case sensitive).

Terminfo has supported the 24-bit TrueColor capability since ncurses-6.0-20180121, under the name "RGB". You need to use the "setaf" and "setab" commands to set the foreground and background respectively.

Having an extra environment variable (separate from TERM) is not ideal: by default it is not forwarded via sudo, ssh, etc, and so it may still be unreliable even where support is available in programs. (It does however err on the side of safety: it does not advertise support when it is not actually supported, and the programs should fall back to using 8-bit color.)

These issues can be ameliorated by adding COLORTERM to:

  • the SendEnv list in /etc/ssh/ssh_config on ssh clients;
  • the AcceptEnv list in /etc/ssh/sshd_config on ssh servers; and
  • the env_keep list in /etc/sudoers.

Despite these problems, it's currently the best option, so checking $COLORTERM is recommended since it will lead to a more seamless desktop experience where only one variable needs to be set.

App developers can freely choose to check for this variable, or introduce their own method (e.g. an option in their config file). They should use whichever method best matches the overall design of their app.

Ideally any terminal that really supports truecolor would set this variable; but as a work-around you might need to put a check in /etc/profile to set COLORTERM=truecolor when $TERM matches any terminal type known to have working truecolor.

case $TERM in
  iterm            |\
  linux-truecolor  |\
  screen-truecolor |\
  tmux-truecolor   |\
  xterm-truecolor  )    export COLORTERM=truecolor ;;
  vte*)
esac

Querying The Terminal

In an interactive program that can read terminal responses, a more reliable method is available, that is transparent to sudo & ssh.

Simply try sending a truecolor value to the terminal, followed by a query to ask what color it currently has. If the response indicates the same color as was just set, then truecolor is supported.

If the response indicates an 8-bit color, or does not indicate a color, or if no response is forthcoming within a few centiseconds, then assume that truecolor is not supported.

$ ( printf '\e[48:2:1:2:3m\eP$qm\e\\' ; xxd -g1 )

^[P1$r48:2:1:2:3m^[\
00000000: 1b 50 31 24 72 34 38 3a 32 3a 31 3a 32 3a 33 6d  .P1$r48:2:1:2:3m

Here we set the background color to RGB(1,2,3) - an unlikely default choice - and request the value that we just set. The response comes back that the request was understood (1), and that the color is indeed 48:2:1:2:3. This tells us also that the terminal supports the colon delimiter. If instead, the terminal did not support truecolor we might see a response like

^[P1$r40m^[\
00000000: 1b 50 31 24 72 34 30 6d 1b 5c 0a              .P1$r40m.\.

This terminal replied that the color is 40 - it has not accepted our request to set 48:2:1:2:3.

^[P0$r^[\
00000000: 1b 50 30 24 72 1b 5c 0a                      .P0$r.\.

This terminal did not even understand the DECRQSS request - its response was CSI+0$r. This does not indicate whether it set the color, but since it doesn't understand how to reply to our request it is unlikely to support truecolor either.

Truecolor Support in Output Devices

Fully Supporting

Terminal Emulators

There are a bunch of libvte-based terminals for GTK2, so they are listed in the another section.

Multiplexers

  • dvtm - not yet supporting truecolor martanne/dvtm#10
  • pymux - tmux clone in pure Python (to enable truecolor run pymux with --truecolor option)
  • screen - has support in 'master' branch, need to be enabled (see 'truecolor' option)
  • tmux - starting from version 2.2 (support since 427b820...)

Re-players

Partial Support

These terminal emulators parse ANSI color sequences, but approximate the true color using a palette or limit number of true colors that can be used at the same time. A 256-color (8-bit) palette is used unless specified.

Note about color differences

Human eyes are sensitive to the primary colors in such a way that the simple Gaussian distance √(R²+G²+B²) gives poor results when trying to find the "nearest" available color as perceived by most humans.

The CIEDE2000 formula provides much better perceptual matching, but it is considerably more complex and may perform very slowly if used blindly [2].

[2] neovim/neovim#793 (comment)

Not Supporting Truecolor

Console Programs + Truecolor

Console Programs Supporting Truecolor

Console Programs Not Supporting Truecolor

colors's People

Contributors

a1346054 avatar calinou avatar cjbassi avatar coolmian avatar craigbarnes avatar cyrusyip avatar dankamongmen avatar data-man avatar equal-l2 avatar geeknees avatar kurahaupo avatar kylefhartzenberg avatar labhanshagrawal avatar lapingenieur avatar leonerd avatar pickfire avatar pocc avatar sschuberth avatar tyriar avatar xaizek avatar xvilka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

colors's Issues

xterm and urxvt correction

xterm (as of 344, but looks like long before as well) seems to have full support for truecolor escape sequences, as well as the proper terminfo entry for that: xterm-direct.

rxvt-unicode (cvs head) does not approximate colors. Instead it uses limited palette to store required colors and then use them instead of generating colors on the fly. This is supposed to result in less resource usage and, as a side effect, limited amount of colors available in the window at the same time. From what I can tell, this may result in some collisions as the palette entries get reused. But I haven't done much testing. It is very quirky, yes, but it is definitely not an approximation.

CSI u

I know this is unrelated to colors but I wish there is a similar comparison for CSI u.

Only iterm2 that I know supports it https://iterm2.com/documentation-csiu.html, maybe other terminals do as well. IIRC neovim supports it out of the box.

Not sure where to create an issue for this so putting it in this issue.

Change repo name to truecolor

I think "colors" is ambiguous. I think it should be changed to "truecolor" for these reasons:

  • It's more descriptive
  • It's the name of the gist that this repo comes from
  • It's one of the values of $COLORTERM (other being 24bit) indicating truecolor support
  • It's the term (with a space) that is used to search on google for this concept

This repo was only recently created, so changing the name shouldn't be too impactful (and github will still provide redirects). Thoughts?

Prominent link between Gist and Repo

I propose to update the Gist as follows:

# Terminal Colors

**Now at https://github.com/termstandard/colors, this Gist is no longer updated.**

---

There exists common confusion about terminal colors. ...

and README.md as follows:

# Terminal Colors

_(Previously published and discussed at https://gist.github.com/XVilka/8346728.)_

There exists common confusion about terminal colors. ...

Terminus supports truecolor

Terminus supports true color now, or at least it does on my machine. (Fedora 30)

I'm not familiar with it, so I don't know when this happened, or if it happens on windows/mac.

How to get 256 color support for FreeBSD 13 headless machine?

Hello I have FreeBSD 13 headless with a graphics card of "AMD Radeon PRO WX 2100".

Can this color library provide 256 color support on the headless machine?

I basically want to avoid the installing a full package of desktop environment just to have color support for one application which is terminal/console.

Thanks.

People are still adding comments to the old gist

The comments thread on the old gist is so long that it's very easy for people to miss the "obvious" notice at the top, especially after they've spend 10 minutes reading down the rest of it.

Please modify the gist to:

  • hide all its comments; and
  • remove all its current content (don't worry, it has a git log like everything else, so it's not really gone); and
  • add an explanation and link pointing to this repo.

Use SemVer

SemVer is the industry standard means of software versioning. Github allows projects to adhere to this with tagging.

This document (as gist) has over 140 commits, so it's seen significant changes. I am proposing this usage of SemVer for this project:

  • MAJOR : Change in the scope of this document ( #7 )
  • MINOR : Change in status of one of the tracked programs. Change in background/detection infos
  • PATCH : Spelling/Grammar

I would like to hear what people's thoughts are on this.

"ANSI" has never defined "16 colors"

The readme states:

There exists common confusion about terminal colors. This is what we have right now:

  • Plain ASCII
  • ANSI escape codes: 16 color codes with bold/italic and background
  • 256 color palette: 216 colors + 16 ANSI + 24 gray (colors are 24-bit)
  • 24-bit true color: "888" colors (aka 16 milion)

...but the ANSI X3.64 spec has never defined "16 colors" -- it defines 8 colors, which can be applied to the foreground and/or background. The extra 8 "bright" colors are an aixterm thing (which were later adopted by xterm) and aren't mentioned anywhere in the ANSI spec.

It seems odd to say "there exists common confusion about terminal colors" and then follow it with more confusion.

TrueColor Terminal Changes, Mar 2019 edition

Add to Compatible

  • guake (tested on Ubuntu 18.04): A top-down terminal for GNOME
  • upterm (Tested on Ubuntu 18.04, Macos 10.14): A terminal emulator for the 21st century.

Add to Not compatible

  • Terminal.app: (Tested on Macos 10.14) Macos Terminal builtin
  • Cmder: (Tested on Windows 10) Portable console emulator for Windows
  • Terminus: (Tested on Macos 10.14) highly configurable terminal emulator for Windows, macOS and Linux

Anybody know of a patch for urxvt to support 24-bit color?

urxvt does not support 24 bit colors properly because it can't display more than a limited subset of colors at one time.

Is there any patch we could get into urxvt that would allow true (non paletted) 24 bit support?

It might be a big job (?). Certainly each colored string would need to remember the full 3 bytes of rgb color info, as opposed to one byte.

If each character in a 100 column, 10k line scrollback buffer got two bytes of color info added, that'd be another 2MB memory use.

If memory consumption is an issue. Color data could be a sparse array.

As a text artist, I find Urxvt completely fails with such images which display correctly in xterm, and other true 24bit terminals:

baroque-fantasy-city

baroque-fantasy-city.ans.gz

Have a `Scope` section

I think it would be beneficial to define the scope for this document (in a level-1 header). Something like "This document exists to document how terminal colors work and which terminals support them".

General purpose command to query for color support programatically

I would like to discuss a similar command as DECRQSS that would be recommended to be implemented by commonly used terminals across different OS.

That way there would be a reliable way to detect actual color support. For now it's not reliable at all by checking for environment variables.

I did not yet think of an ideal solution and would first like to know if there's interest in drafting such command.

True color colon syntax

About Querying the Terminal: the response should actually look like ...;48:2::1:2:3m - note the double colon which allows a position for the optional color space value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.