θέλω χρώμα σε c++

luckyb | Τετ, 12/17/2008 - 21:05 | 1' | 5

γνωρίζει κανείς πως να τυπώνω χρωματιστά σε τερματικό ??

πχ με

cout << "luckyb";

θέλω το luckyb να βγαίνει μπλε, κόκκινο, ροζ ή ότι του πω τέλος πάντων...

Tags
Δώσε αστέρια!

MO: (ψήφοι: 0)

Σχόλια

Αυτό ίσος σε βοηθήσει.. Good Luck ;)

Επίσης βρήκα και αυτό

You want colors? Here is some sample code on how to use the attached header file:

// GPLed | Stephan T. Lavavej | http://nuwen.net

#include "color.hh"
#include <cstdlib>
#include <cstdio>
using namespace std;
using namespace nuwen;

int main() {
atexit(reset_color);

set_fore_color(1, 1, 0, 0);
set_back_color(0, 0, 0, 1);

printf("printf(): Red on Dark Blue.

");

reset_color();

cout << black << "Black." << endl;
cout << darkblue << "Dark Blue." << endl;
cout << darkgreen << "Dark Green." << endl;
cout << darkcyan << "Dark Cyan." << endl;
cout << darkred << "Dark Red." << endl;
cout << darkpurple << "Dark Purple." << endl;
cout << brown << "Brown." << endl;
cout << gray << "Gray." << endl;
cout << darkgray << "Dark Gray." << endl;
cout << blue << "Blue." << endl;
cout << green << "Green." << endl;
cout << cyan << "Cyan." << endl;
cout << red << "Red." << endl;
cout << purple << "Purple." << endl;
cout << yellow << "Yellow." << endl;
cout << white << "White." << endl;

cout << red << bg_gray << "Red on Gray." << endl;
cout << black << bg_cyan << "Black on Cyan." << endl;
cout << darkblue << bg_green << "Dark Blue on Green." << endl;
cout << darkcyan << bg_yellow << "Dark Cyan on Yellow." << endl;
cout << darkred << bg_white << "Dark Red on White." << endl;
cout << darkgreen << bg_purple << "Dark Green on Purple." << endl;
cout << darkgray << bg_red << "Dark Gray on Red." << endl;
cout << cyan << bg_darkpurple << "Cyan on Dark Purple." << endl;
cout << darkpurple << bg_blue << "Dark Purple on Blue." << endl;
cout << brown << bg_darkgray << "Brown on Dark Gray." << endl;
cout << blue << bg_brown << "Blue on Brown." << endl;
cout << gray << bg_black << "Gray on Black." << endl;
cout << green << bg_darkblue << "Green on Dark Blue." << endl;
cout << purple << bg_darkgreen << "Purple on Dark Green." << endl;
cout << yellow << bg_darkcyan << "Yellow on Dark Cyan." << endl;
cout << white << bg_darkred << "White on Dark Red." << endl;
cout << green << bg_green << "Green on Green." << endl;
}

NOTE: I did not any of this, some guy I know did and he hasn't
published it on his website yet, but I had a copy so I figured you guys
would benifit. The colors work on Windows and Linux

// GPLed | Stephan T. Lavavej | http://nuwen.net

#ifndef PHAM_COLOR_HH
#define PHAM_COLOR_HH

#ifdef __WIN32__ // Windows
#include <windows.h>
#define PHAM_STD_OUTPUT_HANDLE 0xFFFFFFF5
#define PHAM_INVALID_HANDLE_VALUE reinterpret_cast<HANDLE>(-1)
#endif // End of: Windows

#include <iostream>

namespace nuwen {
inline void set_fore_color(bool bold, bool red, bool green, bool blue) throw ();
inline void set_back_color(bool bold, bool red, bool green, bool blue) throw ();
inline void reset_color() throw ();

template <typename C, typename T> inline std::basic_ostream<C, T>& black     (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& darkblue  (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& darkgreen (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& darkcyan  (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& darkred   (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& darkpurple(std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& brown     (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& gray      (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& darkgray  (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& blue      (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& green     (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& cyan      (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& red       (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& purple    (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& yellow    (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& white     (std::basic_ostream<C, T>& os) throw ();

template <typename C, typename T> inline std::basic_ostream<C, T>& bg_black     (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_darkblue  (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_darkgreen (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_darkcyan  (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_darkred   (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_darkpurple(std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_brown     (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_gray      (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_darkgray  (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_blue      (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_green     (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_cyan      (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_red       (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_purple    (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_yellow    (std::basic_ostream<C, T>& os) throw ();
template <typename C, typename T> inline std::basic_ostream<C, T>& bg_white     (std::basic_ostream<C, T>& os) throw ();
}

namespace pham {
#ifdef __WIN32__ // Windows
inline void set_win32_color(int color, int mask) throw ();
#endif // End of: Windows
}

inline void nuwen::set_fore_color(const bool bold, const bool red, const bool green, const bool blue) throw () {
#ifdef __WIN32__ // Windows
pham::set_win32_color(
bold * FOREGROUND_INTENSITY | red * FOREGROUND_RED | green * FOREGROUND_GREEN | blue * FOREGROUND_BLUE,
BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
);
#else // GNU/Linux
switch (bold * 8 + red * 4 + green * 2 + blue) {
case 0: std::cout << "\033[30;22m"; break;
case 1: std::cout << "\033[34;22m"; break;
case 2: std::cout << "\033[32;22m"; break;
case 3: std::cout << "\033[36;22m"; break;
case 4: std::cout << "\033[31;22m"; break;
case 5: std::cout << "\033[35;22m"; break;
case 6: std::cout << "\033[33;22m"; break;
case 7: std::cout << "\033[37;22m"; break;
case 8: std::cout << "\033[30;1m"; break;
case 9: std::cout << "\033[34;1m"; break;
case 10: std::cout << "\033[32;1m"; break;
case 11: std::cout << "\033[36;1m"; break;
case 12: std::cout << "\033[31;1m"; break;
case 13: std::cout << "\033[35;1m"; break;
case 14: std::cout << "\033[33;1m"; break;
case 15: std::cout << "\033[37;1m"; break;
}
#endif // End of: Known operating systems
}

inline void nuwen::set_back_color(const bool bold, const bool red, const bool green, const bool blue) throw () {
#ifdef __WIN32__ // Windows
pham::set_win32_color(
bold * BACKGROUND_INTENSITY | red * BACKGROUND_RED | green * BACKGROUND_GREEN | blue * BACKGROUND_BLUE,
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
);
#else // GNU/Linux
switch (bold * 8 + red * 4 + green * 2 + blue) {
case 0: std::cout << "\033[40;25m"; break;
case 1: std::cout << "\033[44;25m"; break;
case 2: std::cout << "\033[42;25m"; break;
case 3: std::cout << "\033[46;25m"; break;
case 4: std::cout << "\033[41;25m"; break;
case 5: std::cout << "\033[45;25m"; break;
case 6: std::cout << "\033[43;25m"; break;
case 7: std::cout << "\033[47;25m"; break;
case 8: std::cout << "\033[40;5m"; break;
case 9: std::cout << "\033[44;5m"; break;
case 10: std::cout << "\033[42;5m"; break;
case 11: std::cout << "\033[46;5m"; break;
case 12: std::cout << "\033[41;5m"; break;
case 13: std::cout << "\033[45;5m"; break;
case 14: std::cout << "\033[43;5m"; break;
case 15: std::cout << "\033[47;5m"; break;
}
#endif // End of: Known operating systems
}

inline void nuwen::reset_color() throw () {
set_fore_color(0, 1, 1, 1);
set_back_color(0, 0, 0, 0);
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::black     (std::basic_ostream<C, T>& os) throw () {

set_fore_color(0, 0, 0, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::darkblue  (std::basic_ostream<C, T>& os) throw () {

set_fore_color(0, 0, 0, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::darkgreen (std::basic_ostream<C, T>& os) throw () {

set_fore_color(0, 0, 1, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::darkcyan  (std::basic_ostream<C, T>& os) throw () {

set_fore_color(0, 0, 1, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::darkred   (std::basic_ostream<C, T>& os) throw () {

set_fore_color(0, 1, 0, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::darkpurple(std::basic_ostream<C, T>& os) throw () {

set_fore_color(0, 1, 0, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::brown     (std::basic_ostream<C, T>& os) throw () {

set_fore_color(0, 1, 1, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::gray      (std::basic_ostream<C, T>& os) throw () {

set_fore_color(0, 1, 1, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::darkgray  (std::basic_ostream<C, T>& os) throw () {

set_fore_color(1, 0, 0, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::blue      (std::basic_ostream<C, T>& os) throw () {

set_fore_color(1, 0, 0, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::green     (std::basic_ostream<C, T>& os) throw () {

set_fore_color(1, 0, 1, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::cyan      (std::basic_ostream<C, T>& os) throw () {

set_fore_color(1, 0, 1, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::red       (std::basic_ostream<C, T>& os) throw () {

set_fore_color(1, 1, 0, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::purple    (std::basic_ostream<C, T>& os) throw () {

set_fore_color(1, 1, 0, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::yellow    (std::basic_ostream<C, T>& os) throw () {

set_fore_color(1, 1, 1, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::white     (std::basic_ostream<C, T>& os) throw () {

set_fore_color(1, 1, 1, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_black     (std::basic_ostream<C, T>& os) throw () {

set_back_color(0, 0, 0, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_darkblue  (std::basic_ostream<C, T>& os) throw () {

set_back_color(0, 0, 0, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_darkgreen (std::basic_ostream<C, T>& os) throw () {

set_back_color(0, 0, 1, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_darkcyan  (std::basic_ostream<C, T>& os) throw () {

set_back_color(0, 0, 1, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_darkred   (std::basic_ostream<C, T>& os) throw () {

set_back_color(0, 1, 0, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_darkpurple(std::basic_ostream<C, T>& os) throw () {

set_back_color(0, 1, 0, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_brown     (std::basic_ostream<C, T>& os) throw () {

set_back_color(0, 1, 1, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_gray      (std::basic_ostream<C, T>& os) throw () {

set_back_color(0, 1, 1, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_darkgray  (std::basic_ostream<C, T>& os) throw () {

set_back_color(1, 0, 0, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_blue      (std::basic_ostream<C, T>& os) throw () {

set_back_color(1, 0, 0, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_green     (std::basic_ostream<C, T>& os) throw () {

set_back_color(1, 0, 1, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_cyan      (std::basic_ostream<C, T>& os) throw () {

set_back_color(1, 0, 1, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_red       (std::basic_ostream<C, T>& os) throw () {

set_back_color(1, 1, 0, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_purple    (std::basic_ostream<C, T>& os) throw () {

set_back_color(1, 1, 0, 1); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_yellow    (std::basic_ostream<C, T>& os) throw () {

set_back_color(1, 1, 1, 0); return os;
}

template <typename C, typename T> inline std::basic_ostream<C, T>&
nuwen::bg_white     (std::basic_ostream<C, T>& os) throw () {

set_back_color(1, 1, 1, 1); return os;
}

#ifdef __WIN32__ // Windows
inline void pham::set_win32_color(const int color, const int mask) throw () {
CONSOLE_SCREEN_BUFFER_INFO s;
HANDLE h = GetStdHandle(PHAM_STD_OUTPUT_HANDLE);

if (h != PHAM_INVALID_HANDLE_VALUE && h != NULL && GetConsoleScreenBufferInfo(h, &s) != 0) {
SetConsoleTextAttribute(h, color | s.wAttributes & mask);
}
}
#endif // End of: Windows

#endif // End of: #ifndef PHAM_COLOR_HH

EDIT: Rename the attached file to color.hh, this stupid forum likes to rename things for no reason :mad:

Ελπίζω να βοήθησα ..

Έλεγα μπας και υπάρχει κανένας πιο απλός τρόπος
...αλλά τελικά η χρήση ενός (η μερικών) ακόμα header είναι αναπόφευκτη.
(α ρε slack, τι μου κάνεις :-p)

thank u SmokematiC
:) :) :)

O tropos pou protathike einai o lathos tropos mias kai xrisimopoiei ansi codes ta opoia de tha doulepsoun se opoiodipote termatiko.

O sostos tropos legetai ncurses, einai vivliothiki gia xrisi ton advanced dynatotiton tou termatikou me platform-independent tropo. Google it.

--
John Tsiombikas
http://nuclear.sdf-eu.org/

Εκεί που googlαρα, κόλλησα με το manual από το tldp και το διάβαζα μανιωδώς (εντάξει, μέχρι τη μέση έφτασα :-p).

Αισθάνομαι σα να μου έβγαλε κάποιος τις παρωπίδες από τα μάτια !!