What's the deal with terminal colors?

2

1

There are 256 terminal color escapes available:

  • 8 "system" colors (0 - 7)
  • 8 "bright" system colors (8 - 15)
  • 216, or 6*6*6 "RGB" colors (16 - 231)
  • 24 "grayscale" colors (232 - 255)

The "RGB" colors are what I'm wondering about.

The first six colors, from 16 to 21, map to the following hex RGB values:

  • 16: 000000
  • 17: 000054
  • 18: 000087
  • 19: 0000af
  • 20: 0000d7
  • 21: 0000ff

Now the blue values only, in decimal:

  • 16: 0
  • 17: 84
  • 18: 135
  • 19: 175
  • 20: 215
  • 21: 255

In other words, the RGB value goes up by 84, then 51 (84 + 51 = 135), then 40 the rest of the way.

This cycle of 6 values from 0 to 255 is repeated 36 times, first with blue, then green, then red, eventually forming our "cube" of 216 terminal colors:

All 256 terminal colors

My question is: Why?

Does anyone know if there is a historical/technical reason for this, or was it just an arbitrary decision made by whoever had to decide what the color codes meant? Did this mapping come to exist before RGB was such a universal convention? Why not go up by 42, 42, 42, 43, 43, 43?

Max Cantor

Posted 2014-07-15T23:17:33.537

Reputation: 436

For me, color number 17 is #00005F (tested in xterm, gnome-terminal, konsole, terminology, st). That is, it goes up by 95, and then 5 times by 40. Still doesn't make too much sense, though. – egmont – 2015-04-25T21:38:39.207

"Why not go up by 42, 42, 42, 43, 43, 43?" You did the math incorrectly. 6x6x6 cube means 5 steps from one end to the other, not 6. That is, you should increment by 51 in each step. I'm really not sure though that that'd look evenly distributed to the human eye. – egmont – 2015-04-25T21:39:45.093

Just for reference, let's mention that several terminal emulators (e.g. gnome-terminal, konsole, st) support arbitrary #rrggbb colors via escape sequences. Alas, support from applications is quite poor. – egmont – 2015-04-25T21:41:23.897

To me, the big question is not why the 6x6x6 color cube looks like this. To me, the big question is why palette-free direct RGB hasn't taken over yet, and why developer are not pushing hard for this. – egmont – 2015-04-25T21:45:12.580

Answers

1

In the sense suggested, it was "arbitrary": a developer chose to split up the set of 256 colors by reserving the first 16 colors to match existing usage, and then noticed that he could get a cube after discounting a conveniently-sized grayscale ramp. Neither of the developers (256 or 88) referred to human-factors publications. They simply chose the steps of color for convenience in computation and what "looked nice".

The technical details as such are in the xterm FAQ Can I set a color by its number?

Thomas Dickey

Posted 2014-07-15T23:17:33.537

Reputation: 6 891

0

In a course that I took in college, it was said to me that the human eye can detect about 256 shades of any one color [or so close that a byte can effectively match that], so 256 of each of the three primary colors allows for all of the colors that the human eye can see. The rest I am unsure of.

Jeff Clayton

Posted 2014-07-15T23:17:33.537

Reputation: 918

Found this on wikipedia: http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors "A set of colors was needed that could be shown without dithering on 256-color displays; the number 216 was chosen partly because computer operating systems customarily reserved sixteen to twenty colors for their own use; it was also selected because it allows exactly six equally-spaced shades of red, green, and blue (6 × 6 × 6 = 216), each from 00 to FF (including both limits)."

– Jeff Clayton – 2014-07-16T01:33:36.497

The color mapping has me intrigued. – Jeff Clayton – 2014-07-16T01:34:58.237

They're not equally spaced, though--the first two go up by 84 and 51, then the rest by 40. It's entirely possible that the decision was totally arbitrary, but I'm just terribly fascinated as to the potential reasoning if it wasn't. – Max Cantor – 2014-07-16T14:43:42.770

The base 16 system colors are most likely throwbacks that never got changed when 256 color VGA entered int the picture (that really wasn't meant as a play on words :) – Jeff Clayton – 2014-07-16T16:28:18.730