PRODUCT : Borland C++ NUMBER : 1153 VERSION : 3.1 OS : DOS DATE : October 19, 1993 PAGE : 1/3 TITLE : Using a BLACK foreground with BGI. A common issue that you may encounter when using BGI graphics is when trying to use black as a foreground color. You will instead get the background color. If your background color was black, you will get black on black. If your background color was blue, you will get blue on blue, and so on. Here's what's going on. The function call setcolor( BLACK ); might be interpreted as, "set my foreground color to black." However, since BLACK is merely an enumerated constant (defined in GRAPHICS.H) that has the value 0, the call is really setcolor( 0 ); which one should really interpret as, "set the foreground color to the color that is in palette # 0". However, palette # 0 also happens to be the palette in which the background color is always stored. (This fact isn't very clear in the documentation as of this writing.) It is important to understand that if you've changed your background color with setbkcolor(), then you've also changed the color in palette # 0. If you then use setcolor( 0 ); which is the same as setcolor( BLACK ); your foreground and background colors will be the same. The solution is to use setpalette() to set a palette number other than 0 to be black. For instance, if we set palette # 2 to be black, we can then change the background color to whatever we want and it will be unaffected by setcolor( 2 ), which will give us the desired black as the foreground color. The following example of code demonstrates the above scenario using black text on a cyan background. Don't forget to change the third parameter of initgraph(), if necessary! #include #include #include #include PRODUCT : Borland C++ NUMBER : 1153 VERSION : 3.1 OS : DOS DATE : October 19, 1993 PAGE : 2/3 TITLE : Using a BLACK foreground with BGI. void main(void) { int gdriver = DETECT, gmode, errorcode; /* Change the third parameter to initgraph() to be a direct path to your BGI drivers. Remember to double up the backslashes! */ initgraph( &gdriver, &gmode, "c:\\borlandc\\bgi" ); if ( ( errorcode = graphresult() ) != grOk ) { printf( "Error %d while changing to graphics mode.\n", errorcode ); exit(0); } /* This section shows the problem */ /* ** The next line really means, "set palette # 0 ** to the color in palette # 3 (CYAN)" ** ** setbkcolor( CYAN ); ** ** At this point, the background color ** (palette # 0) would be cyan. ** ** The next line really means, "set the foreground ** color to the color that is in palette # 0" ** ** setcolor( BLACK ); ** ** At this point, the foreground color (palette # 0) ** would be cyan. We now would have cyan on cyan. */ /* The following section shows the solution */ /* Set palette # 2 to the color that is in palette # 0. */ setpalette( 2, BLACK ); /* At this point, palette # 2 contains black. */ PRODUCT : Borland C++ NUMBER : 1153 VERSION : 3.1 OS : DOS DATE : October 19, 1993 PAGE : 3/3 TITLE : Using a BLACK foreground with BGI. /* Set palette # 0 to the color in palette # 3 (CYAN). */ setbkcolor( CYAN ); /* At this point, the background color (palette # 0) would be cyan. */ /* Set the foreground color to the color that is in palette # 2. */ setcolor( 2 ); /* At this point, the foreground color (palette # 0) would be black. */ outtextxy( 200, 150, "Frank Borland" ); getch(); closegraph(); } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.