Is there a way to create an Orange color from ANSI escape characters?

Alexander Mills picture Alexander Mills · Sep 14, 2015 · Viewed 9k times · Source

I am looking for a control code to create orange text in a terminal using ANSI or some other standard, is this possible? I only see yellow and red available, and I don't think you can mix red and yellow for the same character :)

Answer

Thomas Dickey picture Thomas Dickey · Oct 9, 2015

Strictly speaking, no: ANSI never standardized anything (for a terminal's control sequences) which was called "orange". Some terminals can do this, but that lies outside the scope of the standards.

xterm (see XTerm Control Sequences) uses control sequences following the "ANSI" (actually withdrawn from standardization long ago) ECMA-48 syntax.

  • Some of those are the standard 8 colors via SGR (which do not include orange).
  • It also (using the aixterm SGR extension) adds another 8 colors. Still no orange.
  • However, xterm extends (in a way not exactly standard) SGR codes 38 and 48 to provide a palette of colors using RGB (red/green/blue) values. The maximum palette size is compiled in as either 88 or 256 colors.

Orange can be composed by an RGB code. For instance X's rgb.txt file defines it as

255 165   0             orange

(bright red plus a moderately bright green). To get that in a shell script, one could use

#!/bin/sh
printf "\033[48:2:255:165:0m%s\033[m\n" "Hello world"
printf "\033[48;2;255;165;0m%s\033[m\n" "Hello world"

The difference between the two lines is the use of colon as a parameter separator (ECMA-48 has some verbiage which prefers that, for subparameters as used in xterm, which was overlooked early on). For compatibility (including with terminals copied from xterm's early implementation), xterm honors both.

xterm also supports a "non-ANSI" scheme referred to as dynamic colors, which accepts a color name, but that sets the text color for the whole screen.

Finally, xterm can assign colors to display instead of blink, bold, italics, reverse and underline (see the discussion of colorBD for instance). That is done though X resource settings (with some help from escape sequences). The colors assigned to those resources can be set via the same 88/256 color extension.