Cześć,
Co Wy na to aby razem usprawnić kod ? Jeżeli jesteście na tak to zapraszam ;)
Cel kodu: Kod funkcji changeColor jest zmiana koloru pisania tekstu w chwili jej wywołana na podstawie koloru tekstu oraz koloru tła.
Kod:
/*
Wrote by Shiro (www.shiro.pe.hu) Sebastian Hryszko, COPYRIGHT (c) 2017 Use but not signatures as you or copy.
Write name color with 'L' if light or 'D' if dark.
colors: "black", "blue", "green", "cyan", "red", "purple", "yellow", "white";
ex. Lblue, Dgreen, lcyan; You must not write 'l' or 'd' defauld is 'd' so can be blue (dblue), green (dgreen) etc. and is too gold and gray ;)
*/
int NOTTOUSEColorCode(string Color, bool Tone)
{
int Code;
const string colors[8] = { "black", "blue", "green", "cyan", "red", "purple", "yellow", "white" };
if (Color == "gold")Code = 6;
else if (Color == "gray")Code = 8;
else
{
Color = changeSizeLetterOnWord(Color, 's');
if (Color[0] == 'l')Tone = false;
if (Color[0] == 'l' || Color[0] == 'd')Color.erase(0, 1);
for (int i = 0; i < 8; i++)
{
if (colors[i] == Color)
{
Code = i;
break;
}
}
if (Tone == false) Code += 8;
} return Code;
}
/*
Write name color with 'L' if light or 'D' if dark.
colors: "black", "blue", "green", "cyan", "red", "purple", "yellow", "white";
ex. Lblue, Dgreen, lcyan; You must not write 'l' or 'd' defauld is 'd' so can be blue (dblue), green (dgreen) etc. and is too gold and gray ;)
*/
void changeColor(string fgColor, string bgColor)
{
#pragma once
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//fg - foreground = text, bg - background = back;
bool fgTone = true, bgTone; // is dark ?
int fgCode, bgCode; // code color between 0 and 15
const string colors[8] = { "black", "blue", "green", "cyan", "red", "purple", "yellow", "white" };
int colorCode;
fgCode = NOTTOUSEColorCode(fgColor, fgTone);
bgCode = NOTTOUSEColorCode(bgColor, bgTone);
colorCode = 16 * bgCode + fgCode;
SetConsoleTextAttribute(hConsole, colorCode);
}
Aby funkcja u Was zadziałała są potrzebne dwie inne mojego autorstwa dlatego należy użyć tego pliku: https://goo.gl/5PzQsG
Mam nadzieje że wyniknie z tego ciekawa dyskusja i niezła zabawa ;) Miłego dnia! Shiro!