Filled Under: ,

How to Get Color in C Program

Changing the color of text or shapes in your C program can help them pop when the user runs your program. Changing the color of your text and objects is a fairly straightforward process, and the necessary functions are included in the standard libraries. You can change the color of anything you output on the screen.

Steps

Changing Output Text Color

  1. Include the Standard Input and Output library. This common library allows you to change the color that the text output displays. Add the following code to the top of your program:[1]
  2. Include the Console Input and Output library. This will make it easier to capture keyboard input from the user. Add the library below the library:
  3. Use the function to define what color you want to use for text. You can use this function to vary the text colors of your output. Colors must be written in all caps, or expressed as a numeral:
    [[Image:Get Color in C Program Step 3 Version 2.jpg|center]]
    #include<stdio.h>[[Image:Get Color in C Program Step 4.jpg|center]]
    #include<conio.h>
     
    main()
    {
       textcolor(RED); // You could type "4" instead of "RED", but it is not as readable
    }
    

    0 |row2 = BLUE1 |row3 = GREEN2 |row4 = CYAN3 |row5 = RED4 |row6 = MAGENTA5 |row7 = BROWN6 |row8 = LIGHTGRAY7 |row9 = DARKGRAY8 |row10 = LIGHTBLUE9 |row11 = LIGHTGREEN10 |row12 = LIGHTCYAN11 |row13 = LIGHTRED12 |row14 = LIGHTMAGENTA13 |row15 = YELLOW14 |row16 = WHITE15 }}

    • There are more colors than this. The colors available depend on the installed graphics drivers and current mode. Colors must be written in all caps.[2]
  4. Add output text and finish the program. Include a function to display some text in your new color. Use a function at the end to close the program when the user presses a key.
    [[Image:Get Color in C Program Step 5.jpg|center]]
    #include<stdio.h>[[Image:Get Color in C Program Step 6.jpg|center]]
    #include<conio.h>
     
    main()
    {
       textcolor(RED); // You could type "4" instead of "RED", but it is not as readable
       cprintf("Hello, World!");
     
       getch();
       return 0;
    }
    
Get Color in C Program Step 7.jpg

Changing Drawing Color

  1. Include the graphics library. The C graphics library allows you to draw objects, as well as adjust their color. You can get access to the graphics library by including it at the top of your program:
  2. Include the Console Input and Output library. You can use this library to easily capture a user's input. Add the library below the library:
  3. Initialize the variables for the graphics driver and mode. You'll need to do this before you begin drawing objects, so that the program has access to the system graphics drivers. This will create an area on the screen that the object will be drawn on.
    [[Image:Get Color in C Program Step 10.jpg|center]]
    #include<graphics.h>
    #include<conio.h>
     
    main()
    {
       int gd = DETECT, gm;
       initgraph(&gd, &gm, "C:\\TC\\BGI"); // Change this to the path of your compiler
    }
    
  4. Set the color of the object you want to draw. Before coding in an object, use the function to define the color of the object you are about to draw:[3]
    #include<graphics.h>
    #include<conio.h>
     
    main()
    {
       int gd = DETECT, gm;
       initgraph(&gd, &gm, "C:\\TC\\BGI");
     
       setcolor(BLUE); // You can enter "1" instead of "BLUE" to get the same color, but this is not as readable
    }
    
  5. Draw an object of your choice. For this example, you'll be drawing a rectangle using the function. You can use any of the drawing tools to draw in the color that you set.
    #include<graphics.h>
    #include<conio.h>
     
    main()
    {
       int gd = DETECT, gm;
       initgraph(&gd, &gm, "C:\\TC\\BGI");
     
       setcolor(BLUE); 
       rectangle(50,50,100,100); // These numbers indicate the location of the left-top and right-bottom corners
    }
    
  6. Finish off the program and test it. Add the command and turn off the graphics area as you close the program. Compile it and give it a test run.
    #include<graphics.h>
    #include<conio.h>
     
    main()
    {
       int gd = DETECT, gm;
       initgraph(&gd, &gm, "C:\\TC\\BGI");
     
       setcolor(BLUE); 
       rectangle(50,50,100,100);
     
       getch();
       closegraph();
       return 0;
    }
    

Examples

// Colored Hello World.cpp : main project file.
 
#include <stdafx.h> // Used with MS Visual Studio Express. Delete line if using something different
#include <conio.h> // Just for WaitKey() routine
#include <iostream>
#include <string>
#include <windows.h>
 
using namespace std;
 
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()
 
void WaitKey();
 
int main()
{
 
   int len = 0,x, y=240; // 240 = white background, black foreground
 
   string text = ''Hello World. I feel pretty today!'';
   len = text.length();
   cout << endl << endl << endl << ''\t\t''; // start 3 down, 2 tabs, right
   for ( x=0;x<len;x++)
   {
      SetConsoleTextAttribute(console, y); // set color for the next print
      cout << text[x];
      y++; // add 1 to y, for a new color
      if ( y >254) // There are 255 colors. 255 being white on white. Nothing to see. Bypass it
      y=240; // if y > 254, start colors back at white background, black chars
      Sleep(250); // Pause between letters
   }
 
   SetConsoleTextAttribute(console, 15); // set color to black background, white chars
   WaitKey(); // Program over, wait for a keypress to close program
}
 
 
void WaitKey()
{
   cout  << endl << endl << endl << ''\t\t\tPress any key'';
   while (_kbhit()) _getch(); // Empty the input buffer
   _getch(); // Wait for a key
   while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}


#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm, drawing_color;
   char a[100];
 
   initgraph(&gd,&gm,''C:\\TC\\BGI'');
 
   drawing_color = getcolor();
 
   sprintf(a,''Current drawing color = %d'', drawing_color);
   outtextxy( 10, 10, a );
 
   getch();
   closegraph();
   return 0;
}

Video

Sources and Citations


Cite error: <ref> tags exist, but no <references/> tag was found




from How to of the Day http://ift.tt/1MFfgYY