where does the colour code come from?

This is a discussion on where does the colour code come from? within the C Programming forums, part of the General Programming Boards category; this something my partner hav work on...pls take a look at this.. conio.h is for TUI..and windows.h for API right? ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    this something my partner hav work on...pls take a look at this..
    conio.h is for TUI..and windows.h for API right?
    but,there's smthng that i cant understand...where does the colour code come from??
    there he define black colour as 0 and green as 2...where did he get 0 for black and 2 for green??
    what about other colour??it there any table that contains all the colour code??can u help me in this??

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<conio.h>
    #include<windows.h>
    #define BLACK  0
    #define GREEN  2    //define colour code
    
    void choose1(void);
    void choose2(void);
    void choose3(void);
    void pricecalc(void);
    void pricecalc1(void);
    void seat(void);
    void clrscr(void);
    void fullScreen(void);
    void Color(int BackgroundColor,int TextColor);
    void pas(void);
    void main()
    {
    
        int option;
    	fullScreen();
        Color(BLACK,GREEN);
    
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                printf("   ##       ##   ######  ##      #####  #####  ##   ##   #   #####    ##  ## \n");
                printf("  ##         ## ##    ## ##     ### ## ##   ## ###     ###  ##    ##   ##  ## \n");
                printf("  ##   ##    # # ##    ## ##     ##   # ##   ## ## ## ## ## ###    ##   ##  ## \n");
                printf("  ##  ####   ## ######## ##     ##     ##   ## ##  ##   ## #########   ##  ## \n");
                printf("  ### ## ## ### ###      ##     ##     ##   ## ##       ## ##          ##  ## \n");
                printf("  #####  #####  ###      ##     ##   # ##   ## ##       ## ##          ##  ##  \n"); 
                printf("   ####  ####   ###    # ##     ### ## ##   ## ##       ## ##     #          \n"); 
                printf("    ##    ##     ######  ######  ####   #####  ##       ##  ######     ##  ## \n");
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\t");
    
    	printf("\t\twelcome to MS PEARL AIRLINES...\n");
    	system("pause");
    	system("cls");
        printf("\n\n\n\n\n\n\n\n\n\n\n");
        printf("Enter your option..?\n1=first class\n2=economy
     class\n3=Interactive menu\n\nOption= ");
    
    	scanf("&#37;d",&option);
        if(option==1)
    	{
    		printf("welcome to the first class seat\n");
    	    choose1();
    	
    	}
    	else if(option==2)
    	{
    	       printf("welcome to the economy class\n");
    		   choose2();
    	}
    	else if(option>=3)
    	{
    		   printf("wrong option sir\n");
    		   choose3();
    	}
    
    
    }
    void choose1(void)
    {
    	
    	int age;
    	char cus[25];
    	char fname[25];
    	char lname[25];
    	char gend[7];
    	char mail[50];
    	printf("please enter your name sir=");
    	scanf("%s",&cus);
    
    	clrscr();
    
    	printf("\n\n\n\t\tonline ticket booking to mr/miss %s
    	\n\t___________________________________________________\n",cus);
    
    	printf("please enter your particular details\n");
    	
    	printf("FIRST NAME      : ");
        scanf("%s",&fname);
    	printf("LAST NAME       : ");
    	scanf("%s",&lname);
    	printf("AGE             : ");
    	scanf("%d",&age);
    	printf("GENDER          : ");
    	scanf("%s",&gend);
    	printf("mailing address : ");
    	scanf("%s",&mail);
    
        clrscr();
    	pas();
        
    	clrscr();
    		
    
    	printf("Please choose your seat\n");
    	printf(" _______________________________\n");
    
    	printf("|\t\t\t\t\|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|________________________________\n\n\n");
    
    	seat();
    
    	clrscr();
    
    	printf("\t\t\t     Booking Form
     Confirmation\n\t\t\t*********************************\n\n\n");
    	
    
    	
    	printf("Name            = %s\n",strcat(fname, lname));
    	printf("Mailing Addreses= %s\n\n ",mail);
    	
    	
    }
    void choose2 (void)
    {
    	int age3;
    	char cus3[25];
    	char fname3[25];
    	char lname3[25];
    	char gend3[7];
    	char mail3[50];
    	printf("please enter your name sir=");
    	scanf("%s",&cus3);
    
    	clrscr();
    
    	printf("\n\n\n\t\tonline ticket booking to mr/miss %s
    	\n\t___________________________________________________\n",cus3);
    
    	printf("please enter your particular details\n");
    	
    	printf("FIRST NAME      : ");
        scanf("%s",&fname3);
    	printf("LAST NAME       : ");
    	scanf("%s",&lname3);
    	printf("AGE             : ");
    	scanf("%d",&age3);
    	printf("GENDER          : ");
    	scanf("%s",&gend3);
    	printf("mailing address : ");
    	scanf("%s",&mail3);
    
        clrscr();
    	pas();
    	clrscr();
    
    printf("Please choose your seat\n");
    	printf(" _______________________________\n");
    
    	printf("|\t\t\t\t\|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t|\n|________________________________\n\n\n");
    
    
    
    }
    void choose3 (void)
    {
    	printf("jhsda\n");
    }
    void seat(void)
    {
    	char seat[4];
    	printf("please choose you seat= ");
    	scanf("%s",seat);
    	return seat;
    }
    void clrscr(void)
    {
    
      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD coord = {0, 0};
      DWORD count;
    
      CONSOLE_SCREEN_BUFFER_INFO csbi;
      GetConsoleScreenBufferInfo(hStdOut, &csbi);
    
      FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X *
     csbi.dwSize.Y, coord, &count);
    
      SetConsoleCursorPosition(hStdOut, coord);
    }
    void fullScreen( void )
    {
    	keybd_event( VK_MENU , 0x38 , 0 , 0 );
    	keybd_event( VK_RETURN , 0x1c , 0 , 0 );
    	keybd_event( VK_RETURN , 0x1c , KEYEVENTF_KEYUP , 0 );
    	keybd_event( VK_MENU , 0x38 , KEYEVENTF_KEYUP , 0 );
    }
    void Color(int BackgroundColor,int TextColor)
    {
    	int SeclectedColor=(7*BackgroundColor)+(TextColor);
    	HANDLE hConsole;
    	hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleTextAttribute(hConsole,SeclectedColor);
    }
    void pas(void)
    {
    
        int ages;
    	char fname1[25],lname1[25];
    
        printf("\n\n\n\n\n");
    	printf("Please enter your particular passenger order\n");
    
        printf("FIRST NAME      : ");
        scanf("%s",&fname1);
    	printf("\nLAST NAME     : ");
    	scanf("%s",&lname1);
    	printf("\nAGE           : ");
    	scanf("%d",&ages);
    
        if(ages<=7)
    	{
    		pricecalc();
    	}
    	else if(ages>=8)
    	{
    		printf("\nFor an adult the price is RM120\n"); 
    	}
    	else if(ages>=56)
    	{
    		printf("For gold citizen the price is RM90\n");
    	}
    }
    void pricecalc(void)
    {
    	int price;
    	price=120/2;
    	printf("\nFor the children the price is =RM%d due to 50 percent
     discounts\n",price);
    	return price;
    }
    Last edited by virtual_el; 03-04-2008 at 10:14 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Don't use void main, use int main. Don't use conio.h, it's not standard.
    system("pause") should be changed to getchar(). system("cls") should be removed completely, if can be.
    Ifs after first scanf in menu can be changed to a switch.
    Reading strings with scanf is tricky, so read my signature about it or use fgets.

    scanf("&#37;s",&cus);

    Do not use & on arrays. You'll get char**, which means undefined behavior.
    ...And this has nothing to do with this thread, anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Moved to a new thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure Color() is a non-standard function - if nothing else, there aren't many standard functions that start with a capital.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    That code uses functions and structures declared in windows.h

    Which is perfectly fine, if you want to your code to be windows-dependant.

    Also, I didn't see any reason for conio.h to be included, which is good, because that library is obsolete.
    GCC on Fedora Core 10

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Those colors coincide with DOS colors, for VGA, from years back. Still used today, by the way.

    If you want to see the full list, in Windows, open a console windows:

    Start >> Run >> cmd.exe,

    and at the prompt type: color /?

    All the colors and their number values, are listed there.

    Code:
    #include <conio.h>
    
    int main(void)
    {
       int i;
    
       for (i=0; i<15; i++)
       {
           textcolor(i);
           cprintf("Foreground Color\r\n");
       }
    
       return 0;
    }
    Note: This is for conio.h - printf won't show the colors, but cprintf, will.
    Last edited by Adak; 03-05-2008 at 10:50 AM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Others have said it before, and I'll say it again: conio.h is out of date. It was part of Borland, and Dev-C++ has a partial implementation of it -- but it's really old, and should be avoided if possible.

    A more modern alternative: http://adrianxw.dk/SoftwareSite/Consoles/Consoles4.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    cool site dwk

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's adrianxw's, not mine -- but I have to agree.

    I don't know if anyone noticed or not, but the OP is basically using that type of code already:
    Code:
    void clrscr(void)
    {
    
      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD coord = {0, 0};
      DWORD count;
    
      CONSOLE_SCREEN_BUFFER_INFO csbi;
      GetConsoleScreenBufferInfo(hStdOut, &csbi);
    
      FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X *
     csbi.dwSize.Y, coord, &count);
    
      SetConsoleCursorPosition(hStdOut, coord);
    }
    void fullScreen( void )
    {
    	keybd_event( VK_MENU , 0x38 , 0 , 0 );
    	keybd_event( VK_RETURN , 0x1c , 0 , 0 );
    	keybd_event( VK_RETURN , 0x1c , KEYEVENTF_KEYUP , 0 );
    	keybd_event( VK_MENU , 0x38 , KEYEVENTF_KEYUP , 0 );
    }
    void Color(int BackgroundColor,int TextColor)
    {
    	int SeclectedColor=(7*BackgroundColor)+(TextColor);
    	HANDLE hConsole;
    	hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleTextAttribute(hConsole,SeclectedColor);
    }
    BTW, with this, printf() and cout should be coloured. No need for cprintf().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    thnx alot guys...
    there one thing i want to know...
    does not need to use conio??than,what should i replace it with???
    one more thing...
    i wish i can make the "WELCOME" become 3D...
    how can i do it???

    help me plz....

  11. #11
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by virtual_el View Post
    thnx alot guys...
    there one thing i want to know...
    does not need to use conio??than,what should i replace it with???
    It depends on your compiler. For gcc, mingw32 and probably a few others the ncurses library is great.
    one more thing...
    i wish i can make the "WELCOME" become 3D...
    how can i do it???

    help me plz....
    You can try this. Move the welcome printing statements to a function, but they must be converted so that the cursor position where they start to print 'WELCOME' depends on relative cursor position (x,y as arguments). Then call the function to print it on say:
    (x=5,y=5) with a dark gray foreground color and then on (x=4,y=4) with a light green foreground color, et voila! Pseudo 3d look with shade.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by virtual_el View Post
    thnx alot guys...
    there one thing i want to know...
    does not need to use conio??than,what should i replace it with???
    If you're feeling very adventurous, try ncurses. Otherwise, and if you're using a Windows compiler, read adrianxw's site. That's the one I linked to. Here it is again. http://adrianxw.dk/SoftwareSite/index.html

    See, conio.h is non-standard. On most modern implementations of this (like Dev-C++'s), clrscr() etc are just implemented with Windows functions, like you have. (HANDLE this, COORD that, and so on.) If you're implementing clrscr() etc by yourself, then you don't want to include conio.h. You still need windows.h, but not conio.h.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Quote Originally Posted by xuftugulus View Post
    It depends on your compiler. For gcc, mingw32 and probably a few others the ncurses library is great.

    You can try this. Move the welcome printing statements to a function, but they must be converted so that the cursor position where they start to print 'WELCOME' depends on relative cursor position (x,y as arguments). Then call the function to print it on say:
    (x=5,y=5) with a dark gray foreground color and then on (x=4,y=4) with a light green foreground color, et voila! Pseudo 3d look with shade.

    i don't really understand what does u mean..
    can u giv an example code??
    i've try work on it..but i really dont understand....
    plz..

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    this is what he meant...

    by varying the coordinates we can create a 3d effect...


    Code:
    #include<stdio.h>
    #include<graphics.h>
    int main()
    {
    	int gd=DETECT,gm;int i;
    	initgraph(&gd,&gm,"");
    	for(i=5;i>=0;i--)
    	{
    		if(i==0)
    			setcolor(15);
    		else
    			setcolor(3);
    		outtextxy(200-i,200-i,"HI");
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21