Skip to main content

Graphics in C: Using Colors in Text Mode

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


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 cprintffunction 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

Popular posts from this blog

KALI LINUX COMMANDS

KALI LINUX COMMANDS A-Z of Kali Linux commands are here below: a apropos  Search Help manual pages (man -k) apt-get  Search for and install software packages (Debian) aptitude  Search for and install software packages (Debian) aspell  Spell Checker awk  Find and Replace text, database sort/validate/index b basename  Strip directory and suffix from filenames bash  GNU Bourne-Again SHell bc  Arbitrary precision calculator language bg  Send to background break  Exit from a loop builtin  Run a shell builtin bzip2  Compress or decompress named file(s) c cal  Display a calendar case  Conditionally perform a command cat  Concatenate and print (display) the content of files cd  Change Directory cfdisk  Partition table manipulator for Linux chgrp  Change group ownership chmod  Change access permissions chown  Change file owner and group chroot  Run a command ...

Difference between C and C++

Difference between C and C++ C & C++  are computer programming languages that are used to write programs to communicate with computer. Here we will learn what is C and C++ programming languages and what are the differences between them. Firstly we have to know, what is computer programming language? A computer programming language is the medium of communication between computer hardware and user, where user instruct to the computer through some instructions (written in any programming language) to perform some specific types. Now a day there are many popular computers programming languages like – Assembly, C, C++, Java, Android, Ruby, Python etc. C and C++ programming differences C programming language C is a middle level computer programming language developed at Bell Lab at 1972 by Dennis Ritchie, C is considered as Middle Level Language because of its features. C language contains features of Low Level Language as well as High Level Language. C++ programmi...