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

Privacy Policy

My Photo Keyboard built the  Keyboard app as an Ad Supported app. This SERVICE is provided by Keyboard App at no cost and is intended for use as is. This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service. If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy. The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions, which are accessible at My Photo Keyboard unless otherwise defined in this Privacy Policy. Information Collection and Use For a better experience, while using our Service, I may require you to provide us with certain personally identifiable information, including but not limited to Contact...

Fibonacci series C program

Fibonacci series C program #include <stdio.h>   int  main ( ) {    int  n ,  first  =   0 ,  second  =   1 ,  next ,  c ;      printf ( "Enter the number of terms \n " ) ;    scanf ( "%d" ,   & n ) ;      printf ( "First %d terms of Fibonacci series are: \n " ,  n ) ;      for   ( c  =   0 ;  c  <  n ;  c ++ )    {      if   ( c  <=   1 )       next  =  c ;      else      {       next  =  first  +  second ;       first  =  second ;       second  =  next ;      }      printf ( "%d \n " ,  next ) ;    }      return   0 ; } Output : Ent...