Monday, August 28, 2017

We Love Colors - ANSI Functions II in C

You have several options to give your program a more attractive look in text-mode. Ncurses,for one, in Linux is an excellent choice. But if you want to keep it simple and do things fast, you can resort to using ANSI escape sequences. They work like a charm on Linux terminals. Unfortunately, its support was removed from Windows terminals. Although I hear Windows 10 has brought back support to these sequences. Alternatively, check Ansicon for windows support. 

From Wikipedia:


In C:

int gotoxy(int x,int y)
//Sets the cursor at the desired position.
{
  printf("%c[%d;%df",0x1B,y,x);
  return 0;
}

int outputcolor(int foreground, int background)
//Changes format foreground and background colors of display.
{
  printf("%c[%d;%dm", 0x1b,foreground,background);
  return 0;
}

int screencol(int x)
// Changes color of the entire screen.
{
  gotoxy(0,0);
  outputcolor(0,x);
  printf("%c[2J", 0x1b);
  outputcolor(0,0);
  return 0;
}

No comments:

Post a Comment