Home » C programming language

Graphics in C/C++: Using Colors in Text Mode

Here, we are going to learn how to use colors in the text mode? Here, we are explaining some of the functions that can be used to use colors in Graphics mode.
Submitted by Mahima Rao, on October 22, 2018

In Advance Learning Tutorial, today we will learn about the colors in C / C + +. So far, you have used only two colors in your program Black and White, Black color in Background and White color foreground, i.e. to print the character.

By default, all compilers use these two colors. But if you want to print any character on your screen from the color of your choice, then you can do it with the help of the functions described below. Keep in mind that the functions described below will only run on Text Mode. Using the colors, you can make your program more attractive in C /C++.

Total 15 colors have been defined in C++. To use any color, you can use the name of that color or the corresponding value of that color. This value is already defined in the compiler.

All the 15 colors and their values are given in the table below.

Values of Colors

1. BLACK        0
2. BLUE         1
3. GREEN        2
4. CYAN         3
5. RED          4
6. MAGENTA      5
7. BROWN        6
8. LIGHTGRAY    7
9. DARKGRAY     8
10. LIGHTBLUE   9
11. LIGHTGREEN 10
12. LIGHTCYAN   11
13. LIGHTRED    12
14. LIGHTMAGENTA 13
15. YELLOW      14
16. WHITE       15

Note:

Before using Colors, you must include an important header file <conio.h> in your program.

The two Basic Functions for using colors in the Text Mode have been defined in C / C ++ and both these Functions are also declared in conio.h Header file.

  1. textcolor (int color)
  2. textbackground (int color)

That is why you are required to include this file. Otherwise, the program will show errors while compiling.

textcolor (int color):

It is used to set the color of the character in Text Mode. You will have to pass the name of the color or corresponding value (which is shown in the table) in the parameter of the function, in which color you want to display the character on the screen or print it.

textbackground (int color):

With the help of this function, you can set the background color of the character in Text Mode. This function will also be used in the same way, pass the color or color of the color you want to the background in the function's parameter.

Have a look at the example below to know the syntax more properly.

    textcolor( RED );
    textbackgrond( YELLOW ); 

Or you can use:

    textcolor( 4 );
    textbackgrond( 14 ); 

You can use any of the two syntaxes in your program. Both will give the same output. An example of the program is mentioned below. You run this program on your computer to understand better.

#include <conio.h> 

void main() 
{
	clrscr();
	textcolor(RED);
	cprintf ("Welcome to Tutorial of Graphics in C/C++\n");
	textcolor(LIGHTBLUE);
	cprintf("Hello\n");
	getch(); 
}

Note:

The main thing to note in the above-given program is that instead of printf the cprintf function has been used. If you use only the printf function, the color will not have any impact on the character in the output, and by default, the character in white color will be printed. cprintf is a console output function. This function works for functions that produce a direct text mode output on the screen.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.