Thread: Console colors

  1. #1
    Registered User e.zebtron's Avatar
    Join Date
    May 2011
    Location
    Arnold, Maryland, United States
    Posts
    2

    Console colors

    For all of you out there that are trying to change the Text color and or the background color and you want to use a simple code use the SetConsoleTextAttribute command. I spent a long time looking online for someone that had a list of all the numbers and the color they corresponded with. So her is a simple program that will print out all 256 color combinations and the corresponding number of that color combination. Just copy and paste and it should work I am also giving you a screen shot of the final product. Good luck!

    Code:
    #include <stdio.h>
    #include <windows.h>
    int main ()
    {   
         int count;
         HANDLE hConsole;
         hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
         
         for (count=0; count<257; count++)
         {   SetConsoleTextAttribute(hConsole, count);
              printf ("This color combination has the number of %i\n",count); 
         }
         
         SetConsoleTextAttribute(hConsole, 7);
         system ("pause");
         return 0;
    }
    Attached Images Attached Images Console colors-new-picture-22-jpg 

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Probably not entirely necessary.
    All you have to do is understand that the color is an 8 bit number, 4 bits for background and 4 for foreground (text).
    For each 4-bit nybble,

    Code:
      R G B
    3 2 1 0
    x x x x
    The bits are arranged so that the red, green, and blue components are bits 2,1,0 respectively.
    Bit 3 may be used for blinking text, or BRIGHT colors for the background. If they are implemented.

    Then all you need to do is visualize color mixing in your head:
    R, G, B all off = black.
    G+B = cyan
    R+B = magenta
    R+G = brown/yellow
    R+G+B = gray/white

    If you play with these long enough then visualizing the color mix will become easy. Play with collared lights, filters, etc.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummm... this has been well documented for like 30 years...

    Why wouldn't you just include conio.h and use the textcolor() and textbackground() functions?
    It's not a standard header but it's included with most Windows compilers.

    From the Pelles C helpfile...

    Code:
     _textcolor, _textbackground functions [not standard C] [4.00]
     
    Purpose:
    Sets the text or background color for the console.
     
    Syntax:
    void _textcolor(int color);
    
    void _textbackground(int color);
     
    Declared in:
    <conio.h>
     
    Description:
    The _textcolor function sets the text color, and the _textbackground function sets the background color - to be used by subsequent calls to the _clreol, _clrscr, _cprintf, _cscanf, _cwprintf, _putch and _getche functions. The colors can either be specified as a bitmask where there is one bit each for blue, green, red, and intensity, starting from the lowest bit - or a number from 0 to 15.
     
    If you define _CONIO_RETRO_COLORS before including <conio.h> you can also use the following macros to specify the color:
    
    Macro Value Comment 
    BLACK 0 - 
    BLUE 1 BLUE 
    GREEN 2 GREEN 
    CYAN 3 BLUE + GREEN 
    RED 4 RED 
    MAGENTA 5 BLUE + RED 
    BROWN 6 GREEN + RED 
    LIGHTGRAY 7 BLUE + GREEN + RED 
    DARKGRAY 8 INTENSITY 
    LIGHTBLUE 9 BLUE + INTENSITY 
    LIGHTGREEN 10 GREEN + INTENSITY 
    LIGHTCYAN 11 BLUE + GREEN + INTENSITY 
    LIGHTRED 12 RED + INTENSITY 
    LIGHTMAGENTA 13 BLUE + RED + INTENSITY 
    YELLOW 14 GREEN + RED + INTENSITY 
    WHITE 15 BLUE + GREEN + RED + INTENSITY 
    
    Returns:
    Nothing
    Last edited by CommonTater; 05-02-2011 at 03:25 PM.

  4. #4
    Registered User e.zebtron's Avatar
    Join Date
    May 2011
    Location
    Arnold, Maryland, United States
    Posts
    2
    Quote Originally Posted by nonoob View Post
    Probably not entirely necessary.
    All you have to do is understand that the color is an 8 bit number, 4 bits for background and 4 for foreground (text).
    For each 4-bit nybble,

    Code:
      R G B
    3 2 1 0
    x x x x
    The bits are arranged so that the red, green, and blue components are bits 2,1,0 respectively.
    Bit 3 may be used for blinking text, or BRIGHT colors for the background. If they are implemented.

    Then all you need to do is visualize color mixing in your head:
    R, G, B all off = black.
    G+B = cyan
    R+B = magenta
    R+G = brown/yellow
    R+G+B = gray/white

    If you play with these long enough then visualizing the color mix will become easy. Play with collared lights, filters, etc.
    Ya true I used that a little bit befor but for what I needed to do it was just to complicated. i just need to change the foreground and background in a program I'm working on and didn't need or want all the extra code needed for the R.G.B combination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use different colors in console
    By sakshamgupta in forum C Programming
    Replies: 7
    Last Post: 06-01-2007, 06:03 PM
  2. location specific console colors
    By bennyandthejets in forum C Programming
    Replies: 7
    Last Post: 07-24-2002, 07:32 AM
  3. colors in DOS console based applications
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 10:37 PM
  4. Colors in DOS console
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 07:32 PM
  5. console text colors??
    By Agent89 in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2002, 10:08 PM

Tags for this Thread