The ANSI Terminal specification gives programs (including BBS door games) the ability to change the text color or background color (as well as move the cursor, amoung other things)

An ansi code begins with the ESC character* (ascii 27) and a left square bracket... followed by a number (or 2 or more separated by a semicolon) and a letter.

In the case of colour codes, the trailing letter is "m"...

So as an example, we have ESC[31m ... this will change the foreground colour to red.

The codes are as follows:

1m     -     Change text to hicolour (bold) mode
4m     -        "    "   "  Underline (doesn't seem to work)
5m     -        "    "   "  BLINK!!
8m     -        "    "   "  Hidden (same colour as bg)
30m    -        "    "   "  Black
31m    -        "    "   "  Red
32m    -        "    "   "  Green
33m    -        "    "   "  Yellow
34m    -        "    "   "  Blue
35m    -        "    "   "  Magenta
36m    -        "    "   "  Cyan
37m    -        "    "   "  White
40m    -     Change Background to Black
41m    -        "       "      "  Red
42m    -        "       "      "  Green
43m    -        "       "      "  Yellow
44m    -        "       "      "  Blue
45m    -        "       "      "  Magenta
46m    -        "       "      "  Cyan
47m    -        "       "      "  White
7m     -     Change to Black text on a White bg
0m     -     Turn off all attributes.

Now for example, say I wanted blinking, yellow text on a magenta background... I'd type ESC[45;33;5m

* - The Escape character looks like an arrow pointing left, and can be produced in a hex editor, or with dos' EDIT command by typing CTRL+P and then ESC.

Many terminals and terminal emulators display the bold variant as a different colour, rather than (or in addition to) actually thickening the characters. This provides increased flexibility when, say, customising your shell prompt, or configuring coloured ls output. However, it can also lead to problems on terminals that don't exhibit this behaviour if you are accustomed to having 16 distinct colours to work with.

The following is a quick C program I wrote that showcases the 8 ANSI colours and their bold variants. It is useful for quickly seeing what colours your terminal provides, and how, if at all, the bold variants differ from the regular colours.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void) {
	int c, p;
	char * colours;

	colours = malloc (64);
	memset (colours, 0, 64);

	strcpy (&colours[0*8], "black");
	strcpy (&colours[1*8], "red");
	strcpy (&colours[2*8], "green");
	strcpy (&colours[3*8], "yellow");
	strcpy (&colours[4*8], "blue");
	strcpy (&colours[5*8], "magenta");
	strcpy (&colours[6*8], "cyan");
	strcpy (&colours[7*8], "white");

	for (c=30; c<=37; c++) {
		p = (c-30)*8;
		printf ("\033[%im%s, \033[1mbold %s\033[0m\n",
			c, &colours[p], &colours[p]);
	}

	free (colours);
	exit (0);
}

Or in Perl:

#!/usr/bin/perl -w

use strict;

my @colours = ("black", "red", "green", "yellow", "blue", "magenta", "cyan",
    "white");

do {
    my $n = 37 - $#colours;
    my $c = shift @colours;
    print "\033[${n}m$c, \033[1mbold $c\033[0m\n";
} while (@colours);

Log in or register to write something here or to contact authors.