Thread: ANSI control character and colour

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

    Question ANSI control character and colour

    Hi:

    I'm trying to get my printf text output to be different colours depending on what is being outputted but the tutorials I've read aren't very clear and dont give any examples especially when it comes to printing out variables in different colours.

    The few a have tried or simple turned all of my text output the same colour which is not what I wanted.

    printf("\033[22;31m", mode) which instead of printing my mode var the red colour, it changed the entire output to that colour

    So if a kind soul could point me in the right direction with some examples it would be appreciated

    Christian

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Hey, you can Google "ANSI control character and colour " as easy as I can

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    8

    ansi control chars and colours

    Thanks to kcpilot for that rather unhelpful reply, I had spent many hours Googling exactly that phrase and only getting demonstrations, of the colour changes on Static strings and not variables. So after another hour or so of trying to solve this puzzle myself, with no success, I decided to get some help.

    I hope someone can help me.
    Not just suggesting something that would have obviously been tried prior to posting here.

    Once Again, thanks,

    Christian

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I used this (windows only) function which uses the API to change colors a few years ago.

    Code:
    void setcolor (int color) 
    {
    	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleTextAttribute(hCon,color);
    	return;
    }
    Make sure you #include <windows.h>
    I used to have a table of all the color codes, but I can't find it now.
    Play around with it until you figure it out. I think it accepts integers from 0-16 as an arguement.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cljones81 View Post
    Thanks to kcpilot for that rather unhelpful reply, I had spent many hours Googling exactly that phrase and only getting demonstrations, of the colour changes on Static strings and not variables. So after another hour or so of trying to solve this puzzle myself, with no success, I decided to get some help.

    I hope someone can help me.
    Not just suggesting something that would have obviously been tried prior to posting here.

    Once Again, thanks,

    Christian
    Are you saying that your mode variable isn't getting printed? That's because you need a %d or %s or whatever. (If you don't know what I mean by %d, you need to look up "printf", not ANSI.) Are you saying that your color change doesn't get reverted back? You would need to add the ANSI control code to change it back.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    used to have a table of all the color codes, but I can't find it now.
    you can just read the documentation about the function http://msdn2.microsoft.com/en-us/lib...88(VS.85).aspx
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Thanks to kcpilot for that rather unhelpful reply, I had spent many hours Googling exactly that phrase and only getting demonstrations, of the colour changes on Static strings and not variables. So after another hour or so of trying to solve this puzzle myself, with no success, I decided to get some help.

    I hope someone can help me.
    Not just suggesting something that would have obviously been tried prior to posting here.

    Once Again, thanks,

    Christian

    Sorry. Have a bad cold and I was in a snit at the time. My apologies.

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    I have a bash script that shows exactly what you need.
    I hope it clarifys your puzzle.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM