Thread: colors (Linux)

  1. #1
    Registered User daboost's Avatar
    Join Date
    Aug 2004
    Location
    Belgium
    Posts
    5

    colors (Linux)

    Hi all

    Does somebody have a tutorial on colors in c++ (Linux). I searched the forum multiple times, but all i can find are tutorials for win32

    Thx in advance

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    Registered User daboost's Avatar
    Join Date
    Aug 2004
    Location
    Belgium
    Posts
    5
    thx
    do you have one for C++ or is it threaded the same way?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It works the same in C++, just convert the printf() calls to cout<< calls.

    Make sure you read "The Catch" section, then read up on NCURSES.

    gg

  5. #5
    Registered User daboost's Avatar
    Join Date
    Aug 2004
    Location
    Belgium
    Posts
    5
    i converted the printf to cout <<
    but now i dont see any colors

    [color.cpp]
    Code:
    /* Program by Da Boost */
    #include <iostream.h>
    #include "colors.h"
    
    void Color(int, int, int);
    int main()
    {
        Color(Bright, Black, White);
        cout << "HELLO WORLD!!!" << endl;
        Color(Reset, White, Black);
    
        return 0;
    }
    
    void Color ( int Attr, int Fgc, int Bgc )$
    {
       cout << "0x1B[" << Attr << ";" << Fgc + 30 << ";" << Bgc + 40  << "m";
    }
    [colors.h]
    Code:
    // colors.h
    // This file defines color codes used in Linux
    // By Da Boost
    
    // Attributes
    #define Reset          0
    #define Bright          1
    #define Dim             2
    #define Underline    3
    #define Blink            5
    #define Reverse       7
    #define Hidden        8
    
    // Colors$
    #define Black           0
    #define Red             1
    #define Green         2
    #define Yellow         3
    #define Blue            4
    #define Magenta     5
    #define Cyan           6
    #define White          7
    [ouput]
    0x1B[1;30;47mHELLO WORLD!!!
    0x1B[0;37;40m

    how can i fix this ?

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> i converted the printf to cout <<
    No, you didn't.

    C
    printf("%s", command);

    C++
    cout << command;

    gg

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Uh... That is the character 0x1B, not the string "0x1B".

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > cout << "0x1B["
    Should be wriiten as
    Code:
    cout << "\x1B["
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User daboost's Avatar
    Join Date
    Aug 2004
    Location
    Belgium
    Posts
    5
    thx all

    but isn't there an alternative for the sprintf() ?
    what does it do ?

    thx in advance

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    sprintf just writes something to a string. It's for synthesizing strings from fragments of data and constants, etc...

    edit: It's quite useful for writing clean output statements.

  11. #11
    Registered User daboost's Avatar
    Join Date
    Aug 2004
    Location
    Belgium
    Posts
    5
    thx all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 11:40 AM
  2. Wireless Network Linux & C Testbed
    By james457 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-11-2009, 11:03 AM
  3. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  4. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM