Colours in Linux
2002-02-05 13:28:00
Category: c:linux
Description: Display different colours on your linux console.
Author: detour
Viewed: 3635
Rating: (32 votes)


/* colour.c written by detour@metalshell.com
 *
 * This is an example of how to use colours on your linux
 * console.  It is a simple loop that cycles through the
 * different options, and the different colours.
 *
 * Colours:
 *  Black       0;30     Dark Gray     1;30
 *  Blue        0;34     Light Blue    1;34
 *  Green       0;32     Light Green   1;32
 *  Cyan        0;36     Light Cyan    1;36
 *  Red         0;31     Light Red     1;31
 *  Purple      0;35     Light Purple  1;35
 *  Brown       0;33     Yellow        1;33
 *  Light Gray  0;37     White         1;37
 *  ---------------------------------------
 *
 * You can also do really leet things like make the colour
 * blink by changing the option to 5. Everyone loves blinking
 * things. To get the background colour simply add 10.
 *
 * http://www.metalshell.com/
 *
 */
 
#include <stdio.h>
 
void setFG(int opt, int colour);
void setBG(int colour);
void resetcolour();
 
int main() {
  int x, y;
 
  for(x = 1; x < 9; x++)
    for(y = 30; y < 38; y++) {
      setFG(x,y);
      printf("Wheee Colour!\n");
    }
 
  resetcolour();
  printf("Now doing background colours\n");
 
  for(x = 40; x < 48; x++) {
    setBG(x);
    printf("Wheee Colour!\n");
  }
  resetcolour();
}
 
void setFG(int opt, int colour) {
  printf("\033[%d;%dm", opt, colour);
}
 
void setBG(int colour) {
  printf("\033[%dm", colour);
}
 
void resetcolour() {
  printf("\033[0;m");
}