Thread: color text in C programming

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    8

    Smile color text in C programming

    I 'm using DEV-C++ to write my c program. And also I want to change the text color in the program in windows console environment. From my knowledge, I know that we can change 15 colors by using

    SetConsoleTextAttribute (GetStdHandl(STD_OUTPUT_HANDLE),FOREGROUND_RED);

    However I need more colors. I heard that curses and ncurses can do it. But in DEV-C++, there are not <curses.h> header file. And I don't really understand how to use it or if it can help me to use with more colorful text color. Also, I tried to download ncurses 5.6 and curses 1.23. However, I don't know if they are useful or not. And I don't know how to use them.

    So, please help me to answer the question.
    And I need a example program which can be compiled in DEV-C++. Also, I'm using windows but not unix or linux. And my knowledge is very limited, please give me more details about it. Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to use "pdcurses" if you are using Windows.

    Edit: But I'm not sure you can achieve more than 16 colours there either - it is essentially a limitation of how the text-data is stored for a console application, it uses 16 bits to store the character. 8 bits indicate which character it is, and 8 bits indicate the intensity and colour (4 bits each, where the 3 low bits represent R, G and B (bits 2, 1, 0 respectively, and intensity basicaly adds 1 to each of the RGB, so intensifies the colour choosen)).

    This link shows what each of 16 combinations give:
    http://etricks.in/2008/03/changing-d...nd-colour.html

    --
    Mats
    Last edited by matsp; 08-05-2008 at 08:43 AM.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    Can you give me more details. Even though I'm searching about it now?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by paakfaat View Post
    Can you give me more details. Even though I'm searching about it now?
    More details of what? Pdcurses, no more details from me - I have never really used it, just read others recommend it.

    --
    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
    Join Date
    Aug 2008
    Posts
    8

    Smile

    After I installed, what can I do?
    ok thanks. That's really a good suggestion. I think I get the right one even though I don't know how to install it

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    After I download pdcurses. I don't know how to use it in DEV c++. Or use it to compile programs. Also, I'm not quiet understand about the source part in the web site. So anyone can help me?
    http://gnuwin32.sourceforge.net/install.html

    I download many things. But I don't know which one is useful. So anyone can help me..
    Last edited by paakfaat; 08-05-2008 at 09:23 AM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's why they make manuals.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Could you perhaps be a bit more specific about what you are having problems with?

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

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    This will print in green:
    Code:
    printf("\033[32mHello.\n");
    (Should work on DOS/Windows/Linux - any ANSI capable terminal)
    EDIT: on DOS if ansi.sys is loaded !

    http://en.wikipedia.org/wiki/ANSI_color
    Last edited by rasta_freak; 08-05-2008 at 09:58 AM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rasta_freak View Post
    This will print in green:
    Code:
    printf("\033[32mHello.\n");
    (Should work on DOS/Windows/Linux - any ANSI capable terminal)

    http://en.wikipedia.org/wiki/ANSI_color
    However, like all other solutions, it doesn't give more than at most 16 foreground and 16 background colours.

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

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    I can't use with escape value to change color. As I mention before, I'm using DEV-C++
    when I compiled, it doesn't work at all.

    For my problem, I want to use C to write a program which have more than 16 colors which is given by Windows. So I still can't find any useful codes that can help me. Or it may not be possible. I just heard that there are a header file called curses.h but I didn't use it before.
    So I need your help.

    Thanks Mats, that is my problem. I find many solutions but not greater than 16 colors.

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by matsp View Post
    However, like all other solutions, it doesn't give more than at most 16 foreground and 16 background colours.

    --
    Mats
    But isn't that impossible on classic text terminal/DOS prompt?

  13. #13
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    really...? No method at all?

  14. #14
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I want to use C to write a program which have more than 16 colors which is given by Windows
    You'll need to use something else than the console if you want more than 16 colors. If you are staying with the console, then it's just plain impossible.
    I hate real numbers.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rasta_freak View Post
    But isn't that impossible on classic text terminal/DOS prompt?
    I do believe it is a limitation of how a console works, so the only solution is to implement a completely new console handler - which is substantial work, not to mention that such an application would not be compatible with the current Windows Console executables [although old applications could perhaps be made to run within the new environment, but it would require replacing some pretty basic functionality within Windows itself, which I'm not at all sure is documented enough to be replaced without serious reverse engineering work].

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

Popular pages Recent additions subscribe to a feed