Home > Bash, Computers, Linux, Programming > Bash color code selector.

Bash color code selector.

#!/bin/bash
#####
## Program:
##    Linux Term Colors - bash script
## Author:
##    Kyle Rizzo
##    lifeforce0 {at} gmail {dot} com
##    https://lifeforce4.wordpress.com
## Summary:
##    This script will display all the different color combination's for a
##    terminal tested on Bash 3.2.25(1). Then prompt the user with a menu
##    that they can create their own color scheme. It will also display
##    the escape sequence required to make that scheme.
##    These special escape sequences can be used with any language for a
##    linux terminal.
##
##    "\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
#####

NC="\e[0;37;40m" ## No Color (reset to default)
SELECTION=0 ## The user selection of the menu
FGBOLD=0 ## Default foreground bold/lightness
FGCOLOR=37 ## Default foreground color 'gray'
BGCOLOR=40 ## Default background color 'black'
ENDM="m" ## End the escape sequence

menu ()
{
   echo -en "Menu)\n\t\
1) Display color table\n\t\
2) Set foreground color \n\t\
3) Set background color \n\t\
4) Display selected colors \n\t\
5) Quit\n> ";
read -e SELECTION;
}

displayTable ()
{
   echo -en "B;FG;BG\t";
   for i in {40..47};
   do
      echo -en "  $i\t";
   done
   echo;
   for fg in {30..37};
   do
      for h in {0..1};
      do
         echo -en "$NC$h;$fg";
         for bg in {40..47};
         do
            echo -en "\t\e[$h$ENDM\e[$fg$ENDM\e[$bg$ENDM  RgB  ";
         done
         echo;
      done
   done
   ## Reset the console to no colors.
   echo -e $NC;
}

setFG ()
{
   displayTable
   echo -en "Set the foreground color number [3x]: ";
   read -e FGCOLOR;
   echo -en "Light|Bold text y/n: ";
   read -e FGBOLD;
   if [ "$FGBOLD" == "Y" ] || [ "$FGBOLD" == "y" ]; then
      FGBOLD=1
   else
      FGBOLD=0
   fi
}

setBG ()
{
   displayTable
   echo -en "Set the background color number [4x]: ";
   read -e BGCOLOR;
}

testColors ()
{
   echo -en " \e[$FGBOLD;$FGCOLOR;$BGCOLOR$ENDM";
   echo -en " This is a test of the colors you selected.";
   echo -en "$NC \\";
   echo -en "e[$FGBOLD;$FGCOLOR;$BGCOLOR$ENDM\n";
   echo -e "$NC========================================================\n\
Press enter if you can not read the text above the line.";

}

main ()
{
   while [ "$SELECTION" -ne 5 ]
   do
      menu
      case "$SELECTION" in
         1 )
            clear;
            displayTable
            ;;
         2 )
            clear;
            setFG
            ;;
         3 )
            clear;
            setBG
            ;;
         4 )
            clear;
            testColors
            ;;
         5 ) exit 0;;
         * )
            clear;
            menu
      esac
   done
}

displayTable
main
Advertisements
Categories: Bash, Computers, Linux, 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

%d bloggers like this: