8

I was messing around with debug and colors.js to get more colors than the limited 4-6 colors but I'm stuck at figuring out this coloring syntax

args[0] = ' \u001b[9' + c + 'm' + name + ' '
  + '\u001b[3' + c + 'm\u001b[90m'
  + args[0] + '\u001b[3' + c + 'm'
  + ' +' + exports.humanize(ms) + '\u001b[0m';
'blue' : ['\x1B[34m', '\x1B[39m'],
'cyan' : ['\x1B[36m', '\x1B[39m'],
'green' : ['\x1B[32m', '\x1B[39m'],
'magenta' : ['\x1B[35m', '\x1B[39m'],
'red' : ['\x1B[31m', '\x1B[39m'],
'yellow' : ['\x1B[33m', '\x1B[39m'],

I know windows console allows more colors than just those six, as color /? shows

0 = Black       8 = Gray
1 = Blue        9 = Light Blue
2 = Green       A = Light Green
3 = Aqua        B = Light Aqua
4 = Red         C = Light Red
5 = Purple      D = Light Purple
6 = Yellow      E = Light Yellow
7 = White       F = Bright White

How do I go about understanding this syntax and adding the extra colors that windows has to offer?

laggingreflex
  • 32,948
  • 35
  • 141
  • 196

1 Answers1

15

Those are ANSI terminal escape codes. Specifically, they're "select graphic rendition" (SGR) escape codes, which consist of:

  • the "command sequence introducer", consisting of the characters \x1B (ESC) and [,
  • one or more numeric commands, separated by semicolons, and
  • the letter m, ending the code and indicating that this is an SGR code.

There are many possible numeric commands (and many other escape codes besides SGR), but the most relevant ones are:

  • 30–37: set text color to one of the colors 0 to 7,
  • 40–47: set background color to one of the colors 0 to 7,
  • 39: reset text color to default,
  • 49: reset background color to default,
  • 1: make text bold / bright (this is the standard way to access the bright color variants),
  • 22: turn off bold / bright effect, and
  • 0: reset all text properties (color, background, brightness, etc.) to their default values.

Thus, for example, one could select bright purple text on a green background (eww!) with the code \x1B[35;1;42m.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153