Hey guys! This is part 11 of my computer programming tutorials. This tutorial will be in C++. If you want to catch up, here are my other posts:
In this tutorial, I am going over how to print colored text to a Linux terminal. This will add a bit of life to your text-only programs! Note that not all Linux systems support this. All you have to do is insert a little bit of code into your cout statement. Example:
cout << "\033[1;32mLight green text!\033[0m\n";
- This will print "Light green text!" in light green font.
"\33" is the ESC character. When you follow this with an open bracket, two numbers with a semicolon in between, and an "m," this will change the font color. To change the color back to normal, insert "\033[0m" after the text. The values you can use for the first number are 0 and 1. The values for the second number are 30 - 37.
Here is a program I wrote to demonstrate all of the colors:
#include
using namespace std;
int main()
{
cout << "\033[1;30mDark Gray text!\033[0m\n";
cout << "\033[1;31mLight red text!\033[0m\n";
cout << "\033[1;32mLight green text!\033[0m\n";
cout << "\033[1;33mYellow text!\033[0m\n";
cout << "\033[1;34mLight blue text!\033[0m\n";
cout << "\033[1;35mlight purple text!\033[0m\n";
cout << "\033[1;36mLight cyan text!\033[0m\n";
cout << "\033[1;37mWhite text... Boring...\033[0m\n";
cout << "\033[0;30mBlack text!\033[0m\n";
cout << "\033[0;31mRed text!\033[0m\n";
cout << "\033[0;32mGreen text!\033[0m\n";
cout << "\033[0;33mDark yellow text!\033[0m\n";
cout << "\033[0;34mBlue text!\033[0m\n";
cout << "\033[0;35mPurple text!\033[0m\n";
cout << "\033[0;36mCyan text!\033[0m\n";
cout << "\033[0;37mLight gray text!\033[0m\n";
cout << "\033[1;32mLight green text!\n";
cout << "Hello!\n";
return 0;
}
Here is the output:
Notice how on the last line, I didn't set the color back to normal. This even turned my prompt green after the program was over!
I hope this was interesting! Leave suggestions in the comments!