DOCUMENT ID: 1480-02
SYNOPSIS: Setting Console Colors on x86
OS RELEASE: Solaris x86
PRODUCT: OS
KEYWORDS: console colors ansi escape sequence
DESCRIPTION:
How to setup up Console Colors on x86.
SOLUTION:
ANSI ESCAPE SEQUENCE
______________________________________________________________________________
Wherever you see '#', that should be replaced by the appropriate number.
ESC code sequence Function
------------------- ---------------------------
Cursor Controls:
ESC[#;#H or ESC[#;#f Moves cusor to line #, column #
ESC[#A Moves cursor up # lines
ESC[#B Moves cursor down # lines
ESC[#C Moves cursor forward # spaces
ESC[#D Moves cursor back # spaces
ESC[#;#R Reports current cursor line & column
ESC[s Saves cursor position for recall later
ESC[u Return to saved cursor position
Erase Functions:
ESC[2J Clear screen and home cursor
ESC[K Clear to end of line
Set Graphics Rendition:
ESC[#;#;....;#m Set display attributes where # is
0 for normal display
1 for bold on
4 underline (mono only)
5 blink on
7 reverse video on
8 nondisplayed (invisible)
30 black foreground
31 red foreground
32 green foreground
33 yellow foreground
34 blue foreground
35 magenta foreground
36 cyan foreground
37 white foreground
40 black background
41 red background
42 green background
43 yellow background
44 blue background
45 magenta background
46 cyan background
47 white background
ESC[=#;7h or Put screen in indicated mode where # is
ESC[=h or 0 for 40 x 25 black & white
ESC[=0h or 1 for 40 x 25 color
ESC[?7h 2 for 80 x 25 b&w
3 for 80 x 25 color
4 for 320 x 200 color graphics
5 for 320 x 200 b & w graphics
6 for 640 x 200 b & w graphics
7 to wrap at end of line
ESC[=#;7l or ESC[=l or Resets mode # set with above command
ESC[=0l or ESC[?7l
Keyboard Reassignments:
ESC[#;#;...p Keyboard reassignment. The first ASCII
or ESC["string"p code defines which code is to be
or ESC[#;"string";#; changed. The remaining codes define
#;"string";#p what it is to be changed to.
E.g. Reassign the Q and q keys to the A and a keys (and vice versa).
ESC [65;81p A becomes Q
ESC [97;113p a becomes q
ESC [81;65p Q becomes A
ESC [113;97p q becomes a
E.g. Reassign the F10 key to a DIR command.
ESC [0;68;"dir";13p The 0;68 is the extended ASCII code
for the F10 key and 13 is the ASCII
code for a carriage return.
Other function key codes F1=59,F2=60,F3=61,F4=62,F5=63
F6=64,F7=65,F8=66,F9=67,F10=68
____________________________________________________________________________
Here is a program to change the display colors:
/* ASCII Setcolors
Needs for UNIX check term types and dissable under wrong term type
Add Numberic color input
accept other color abriviations
Allow other Ascii modes, IE: Blinking or inverse
To compile just cc -o scolor scolor.c
*/
#include
#include
#define COL_DEF3 "bla red gre yel blu mag cya whi hig "
#define COL_DEF2 "blk rd grn bro bu mgn cyn wht hi "
#define COL_DEF "bk r g y b m c w h "
/* Will Never Hit 'b' for blue, black gets it */
#define ESCB "\033["
#define ESCA "ESC["
#define HIGH "7;"
int what_color(arg)
char *arg;
{
char str[5];
char *COL,*res,*t,*s;
int colr;
strncpy(str,arg,4);
str[3] = '\0';
/* Force lower case before search */
for( s = str;*s != '\0';s++)
if (*s < 'Z' & *s > 'A')
*s = *s + 'a' - 'A';
/* Convert define to mem location */
COL = COL_DEF;
res = strstr(COL,str);
if (res != NULL)
{
colr = res - COL;
return (colr/4 +1) ;
}
/* Convert define to mem location */
COL = COL_DEF2;
res = strstr(COL,str);
if (res != NULL)
{
colr = res - COL;
return (colr/4 +1) ;
}
/* Convert define to mem location */
COL = COL_DEF3;
res = strstr(COL,str);
if (res != NULL)
{
colr = res - COL;
return (colr/4 +1) ;
}
return 0;
}
int main(argc,argv)
int argc; char* argv[];
{
int fg,bg,hi,c[3],ind;
char *his;
if (argc < 2 | argc > 4)
{
printf("%smUsage:setcolor forground background\n",ESCB);
return 1;
}
c[0]=c[1]=c[2]=fg=bg=hi = 0;
ind = 1;
while(ind < argc)
{
c[ind - 1] = what_color(argv[ind]);
if(c[ind -1] == 9)
hi = 1;
ind++;
}
/*printf("%i,%i,%i,%i\n",c[0],c[1],c[2],hi);*/
ind = 0;
if(c[ind] >= 9) ind++;
fg = c[ind++];
if(c[ind] >= 9) ind++;
bg = c[ind++];
if (hi == 1) his = "7;";
else
{ his = "\0";
printf("%sm",ESCB);
}
if (fg == bg & hi == 0)
{
printf("%smError:Same forground and background color\n",ESCB);
return 1;
}
if (fg != 0 & bg != 0)
printf("%s%s3%i;4%im\n",ESCB,his,fg-1,bg-1);
else
if (bg != 0)
printf("%s%s4%im\n",ESCB,his,bg-1);
else
if (fg != 0)
printf("%s%s3%im\n",ESCB,his,fg-1);
}
DATE APPROVED: 07/28/95