color

Hi,

I want to change color of my text with <stdlib.h> but when I run my program I see this : sh: 1: color: not found
and then my program runs normally but text is white as always

Can someone help me?

P.S I'm begginer :)
Sorry for bad english
You probably tried system("color xx").

1. http://www.cplusplus.com/forum/articles/11153/
2. Use cstdlib instead of stdlib.h
3. There is no portable way to change the console's color, and, personally, I know nothing about this topic on unix, but I just wanted to give you a heads-up.
Thanks,

but...
I found a way to make colours just with iostream :)

http://lifeforce4.wordpress.com/2010/09/03/linux-terminalbash-color-code-c-perl-bash/

I don't understand everything but it's working for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

// Set a few standards to make formatting easier.
const string NC = "\E[0m"; // No Color (reset to default)
const string HOME_CURSOR  = "\E[0;0H"; // Place the cursor at 0;0 position.
const string CLEAR_SCREEN = "\E[2J";

int main(int argc, char **argv)
{
   // Clear the screen and reset the cursor to the top left.
   cout << CLEAR_SCREEN << HOME_CURSOR;

   // print program name.
   cout << endl << argv[0] << endl;

   // display the color table.
   cout << "B;FG;BG\t";
   for (int i = 40; i < 48; i++)
      cout << "  " << i << "m\t";
   cout << endl;
   for (int fg = 30; fg < 38; fg++)
      for (int h = 0; h < 2; h++)
      {
         cout << NC << h << ";" << fg << "m";
         for (int bg = 40; bg < 48; bg++)
         {
            cout << "\t"
                 << "\E[" << h << "m"
                 << "\E[" << fg << "m"
                 << "\E[" << bg << "m"
                 << "  RgB  ";
         }
         cout << endl;
      }

   // Reset the console to no colors.
   cout << NC << endl;

   return 0;
}
Now I understand here everything :)

simple version of this code (working and much shorter) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;


int main()
{
   // display the color table.
   cout << "B;FG;BG\t";
   for (int i = 40; i < 48; i++)
      cout << "  " << i << "m\t";
   cout << endl;
   for (int fg = 30; fg < 38; fg++)
      for (int h = 0; h < 2; h++)
      {
         cout << "\E[0m" << h << ";" << fg << "m";
         for (int bg = 40; bg < 48; bg++)
         {
            cout << "\t"
                 << "\E[" << h << "m"
                 << "\E[" << fg << "m"
                 << "\E[" << bg << "m"
                 << "  RgB  ";
         }
         cout << endl;
      }

   // Reset the console to no colors.
   cout << "\E[0m" << endl;

    ///cout << "\t\E[0m\E[30m\E[40m";
    /// 0 txt type; 30 text colour; 40 background colour
    /// you can change numbers before m to change text and background colour and text type
    /// 0 italic, 1 bold
    /// 30,40 = black
    /// 31,41 = red
    /// 32,42 = green
    /// 33,34 = yellow
    /// and: blue, pink, electric blue, white

   return 0;
}


short colour output: cout << "\t\E[0m\E[37m\E[40m";

maybe I'm not a total newbie as I think I am?
thanks again for wasting your time :)
You could even implement a manipulator for color setting, like chrisname does in his hex viewer.

Hex viewer
http://www.cplusplus.com/articles/NwTbqMoL/

Andy
Last edited on
closed account (Dy7SLyTq)
theres also ncurses and pdcurses
ty
I believe they are called ANSI escape sequences you are using to color the console. http://en.wikipedia.org/wiki/ANSI_escape_code

They work good in linux but unfortunately if you are using windows you can not use ANSI AFAIK.
closed account (Dy7SLyTq)
actually you can... ansi is a character set. i believe you mean escaped ansi color sequences.

edit:
and you can use pdcurses on windows for colors. *curses is an attempt to make code like that portable or seem like it, when in reality its not
Last edited on
You can also just use the windows api for color on windows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<windows.h>
enum COLOR
{
    black , blue , green , cyan , red , magenta , brown , normal , darkgrey , lightblue , lightgreen , lightcyan , lightred , lightmagenta , yellow , white
};

std::ostream& operator<<( std::ostream &stm , const COLOR &c)
{
    HANDLE out_handle = GetStdHandle( STD_OUTPUT_HANDLE );
    SetConsoleTextAttribute( out_handle , c );
    return( stm );
}

int main()
{
    std::cout << red << "apple\n" << yellow << "banana" << normal << std::endl;
}


*edit added default after banana so you don't have yellow return stuff
Last edited on
But we're into the Unix section /:

I just came back from a offline week and i'm now online from mobile - copypasted
Heh heh heh... this comes up every so often and I usually miss it, but here's a cookie for y'all.

http://www.cplusplus.com/forum/general/18200/#msg92479

Enjoy!
One of these days I'm just gonna stop trying...
Topic archived. No new replies allowed.