Home > Bash, C++, Linux, Perl, Programming > Linux terminal/bash color code C++, Perl, Bash

Linux terminal/bash color code C++, Perl, Bash

C++

/******************************************************************************
*  Program:
*     Linux Term Colors
*  Author:
*     Kyle Rizzo
*     lifeforce0 {at} gmail {dot} com
*     http://lifeforce4.wordpress.com
*  Summary:
*     This loops though all the font colors on each background for a linux
*     terminal. Tested on Bash 3.2.25(1). These special escape sequences can
*     be used with any language for a linux terminal. See my bash and perl
*     code for other examples of the same output.
*
*     "\E[" begins the escape sequence, you can also use "\033" or "\x1B".
*
*     You can use a semicolon to separated the numbers
*     (eg 1;30;46 = Bold font (making it lighter in color)
*                   FG as Black (bolding it makes it a dark gray)
*                   BG as Cyan )
*        Note: The foreground and background numbers do not overlap so order
*              does not matter, for formatting reasons I will have it always
*              be Text-format / Foreground / Background.
*
*     "m" terminates the escape sequence, the text begins immediately after.
*
*     FG hew bit: 0/1 (dark/light)
*     Foreground Colors: 3x
*     Background Colors: 4x
*
*     x representing a different color
*        0 = Black   1 = Red
*        2 = Green   3 = Yellow
*        4 = Blue    5 = Magenta
*        6 = Cyan    7 = White
******************************************************************************/
#include <iostream>
using namespace std;

// Set a few standards to make formatting easier.
const string NC = "\E[0m"; // No Color (reset to default)
const string HOME_CURSOR  = "\E[0;0H"; // Place the cursor at 0;0 position.
const string CLEAR_SCREEN = "\E[2J";

/******************************************************************************
*  Loop though each background color for both normal and bold fonts of a given
*  color.
******************************************************************************/
int main(int argc, char **argv)
{
   // Clear the screen and reset the cursor to the top left.
   cout << CLEAR_SCREEN << HOME_CURSOR;

   // print program name.
   cout << endl << argv[0] << endl;

   // display the color table.
   cout << "B;FG;BG\t";
   for (int i = 40; i < 48; i++)
      cout << "  " << i << "m\t";
   cout << endl;
   for (int fg = 30; fg < 38; fg++)
      for (int h = 0; h < 2; h++)
      {
         cout << NC << h << ";" << fg << "m";
         for (int bg = 40; bg < 48; bg++)
         {
            cout << "\t"
                 << "\E[" << h << "m"
                 << "\E[" << fg << "m"
                 << "\E[" << bg << "m"
                 << "  RgB  ";
         }
         cout << endl;
      }

   // Reset the console to no colors.
   cout << NC << endl;

   return 0;
}

Perl

#!/use/bin/perl
#####
## Program:
##    Linux Term Colors - perl script
## Author:
##    Kyle Rizzo
##    lifeforce0 {at} gmail {dot} com
##    http://lifeforce4.wordpress.com
## Summary:
##    This loops though all the font colors on each background for a linux
##    terminal. Tested on Bash 3.2.25(1). These special escape sequences can
##    be used with any language for a linux terminal. See my bash and C++
##    code for other examples of the same output.
##
##    "\E[" begins the escape sequence, you can also use "\033" or "\x1B".
##
##    Semicolon-separated numbers "HEW" "COLOR1" and "COLOR2".
##       Note: The foreground and background numbers do not overlap so order
##             does not matter, for formatting reasons I will have it always
##             be Foreground then Background.
##
##    "m" terminates the escape sequence, the text begins immediately after.
##
##    FG hew bit: 0/1 (dark/light)
##    Foreground Colors: 3x
##    Background Colors: 4x
##
##    x representing a different color
##       0 = Black   1 = Red
##       2 = Green   3 = Yellow
##       4 = Blue    5 = Magenta
##       6 = Cyan    7 = White
#####

## Set a few standards to make formatting easier.
my $NC = "\e[0;37;40m"; ## No Color (reset to default)

print "\n$0\n";

print "B;FG;BG\t";
foreach $i (40..47)
{
   print "  " . $i . "m\t";
}

print "\n";

foreach $fg (30..37)
{
   foreach $h (0..1)
   {
      print $NC . $h . ";" . $fg . "m";
      foreach $bg (40..47)
      {
         print "\t" . "\e[" . $h . "m" . "\e[" . $fg . "m" . "\e[" . $bg . "m" . "  RgB  ";
      }
      print "\n";
   }
}

## Reset the console to no colors.
print $NC . "\n";

Bash

#!/bin/bash
#####
## Program:
##    Linux Term Colors - bash script
## Author:
##    Kyle Rizzo
##    lifeforce0 {at} gmail {dot} com
##    http://lifeforce4.wordpress.com
## Summary:
##    This loops though all the font colors on each background for a linux
##    terminal. Tested on Bash 3.2.25(1). These special escape sequences can
##    be used with any language for a linux terminal. See my perl and C++
##    code for other examples of the same output.
##
##    "\E[" begins the escape sequence, you can also use "\033" or "\x1B".
##
##    Semicolon-separated numbers "HEW" "COLOR1" and "COLOR2".
##       Note: The foreground and background numbers do not overlap so order
##             does not matter, for formatting reasons I will have it always
##             be Foreground then Background.
##
##    "m" terminates the escape sequence, the text begins immediately after.
##
##    FG hew bit: 0/1 (dark/light)
##    Foreground Colors: 3x
##    Background Colors: 4x
##
##    x representing a different color
##       0 = Black   1 = Red
##       2 = Green   3 = Yellow
##       4 = Blue    5 = Magenta
##       6 = Cyan    7 = White
#####

## Set a few standards to make formatting easier.
NC="\e[0;37;40m"; ## No Color (reset to default)

echo;
echo "$0";

echo -en "B;FG;BG\t";
for i in {40..47};
do
   echo -en "  $i";
   echo -en "m\t";
done
echo;

for fg in {30..37};
do
   for h in {0..1};
   do
      echo -en "$NC$h;$fg";
      echo -en "m";
      for bg in {40..47};
      do
         echo -en "\t";
         echo -en "\e[$h";
         echo -en "m";
         echo -en "\e[$fg";
         echo -en "m";
         echo -en "\e[$bg";
         echo -en "m";
         echo -en "  RgB  ";
      done
      echo;
   done
done

## Reset the console to no colors.
echo -e $NC;
About these ads
Categories: Bash, C++, Linux, Perl, Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: