Terminal color theory

colorsterminalvim

TLDR: What is the theory behind terminal colors?

I have a fixed value of LS_COLORS in numbers:
no=00:fi=00:di=01;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;33:*.cmd=00;33:*.exe=00;33:*.com=00;33:*.btm=00;33:*.bat=00;33:*.sh=00;33:*.csh=00;33:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:*.c=00;41:*.cpp=00;41:*.h=00;44:*.cu=00;43:*.cuh=00;43:ex=00;32

I wonder what causes gnome-terminal, urxvt and yakuake to show perceptibly different colors for the same file type? The difference ranges from very similar (and yet clearly different) to drastically different- brown vs yellow.

Similarly, there are differences in how vim color shows up, I see very different colors in all three terminals. Both when t_Co=8 (vim supporting 8 colors) and t_Co=256(vim supporting 256 colors).

What's worse is that when I copy .XResources from internet which has english names for colors, the same colorscheme in vim shows much different colors. (I think I can attribute this to the use of names in vimscheme, and color redefintion by .Xresources)

Could someone help me with understanding the theory behind how terminal color works? A link would work great.
If someone's feeling extra generous- maybe they can give tips too to ensure that I always have a standard environment, specially in the context of vim?

I should note that on yakuake, my color theme is: "Linux theme" (which seems to be only standard theme) and on gnome-terminal I have no theme. When I check the 'user system colors' option, the colors are even more disastrous, and the output itself is mangled (e.g. vanilla ls showing each file on a different line). For urxvt, the only color related fields I have defined in .Xresources and xrdb -q yields are:

Urxvt.background: #000000 
Urxvt.foreground: #FFFFFF

Ok so some screenshots to explain what I'm talking about (first is on yakuake, second on urxvt)

On yakuake
On urxvt
The difference is clearly major…

Using: http://www.vim.org/scripts/script.php?script_id=1349 (first is yakuake, second on urxvt)

enter image description here
enter image description here

Best Answer

The indexed color palette has the actual rendering open to interpretation - on actual hardware, there were different standards (especially brown vs. dark yellow, brown is more useful and nicer to look at).

Just check out this: https://en.wikipedia.org/wiki/Color_Graphics_Adapter

On terminal emulators, it depends on the configuration. Most emulators have a mode defined (echo $TERM), singifying which old-school hardware it is emulating. The color is the least of the changes - other control codes (invisible character strings that control the cursor position, bold/invert video, blinking and whatever) can have different codes too. That's the garbage you sometimes get, when you press cursors when the terminal type is incorrectly set (over ssh, for instance). Some more sophisticated emulators actually don't care about the ancient color standards and let you define your own colors (Konsole - which Yakuake uses - has color profiles). Modern terminal emulators actually support more colors than that (256), but of course, the application running in the terminal has to recognize this capability (through termcap or something, or by reading $TERM variable), and output the correct character combinations to use them.

What you actually want to read is this, there is even a color table: https://en.wikipedia.org/wiki/ANSI_escape_code