About a year ago, I started writing a little database program for the console (just a simple little thing to modify, add, and subtract items from a SQL-Lite database) and I wanted it to be colourful. However, the codes aren’t that much fun to type in so I came up with a crude implementation of something that could create a header file for me to handle exactly that.
It wasn’t pretty, and a year on with a little extra C++ knowledge under my belt I came up with this to replace it.
This creates a header file and also a nice output listing to print out.
Edit: I’ve since given it a third revision that outputs the codes to a console window rather than to an output file, which kind of made a bit more sense. I can copy/paste it to a future post if someone wants it.
// Program to output a definition header file for Windows Console colours
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int main()
{
/* Output Header file */
ofstream outFile;
outFile.open("ConsoleColours.h");
outFile << "#ifndef ConsoleColoursH" << endl;
outFile << "#define ConsoleColoursH" << endl;
outFile << " " << endl;
/* Output Nice Listing */
ofstream niceList;
niceList.open("ConsoleColours-Nice.txt");
string listColours[16] = { "DARK_BLUE", "GREEN", "TEAL", "RED",
"PINK", "KHAKI", "WHITE", "GREY",
"BRIGHT_BLUE", "BRIGHT_GREEN", "BRIGHT_TEAL", "BRIGHT_RED",
"BRIGHT_PINK", "BRIGHT_YELLOW", "BRIGHT_WHITE", "BLACK" };
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
// Output to console window
if (k % 16 == 0) cout << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i); cout << " " << i << " ";
// Output to header file
outFile << "#define " << listColours[j] << "_WITH_" << listColours[k] << "_BACKGROUND" " SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), " << i << ");" << endl;
// Create a nice title for each colour in our textfile
if (k % 16 == 0)
{
niceList << endl << listColours[j] << endl;
niceList << string(listColours[j].length(), '=') << endl;
}
// Output to nice listing
niceList << listColours[j] << "_WITH_" << listColours[k] << "_BACKGROUND" << endl;
i++;
if (i == 255) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
}
}
}
outFile << " " << endl; outFile << "#endif" << endl; outFile.close();
// Reset Console to normal
cout << endl << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
return 0;
}
