Skip to content

Tyger375/Colors-for-Cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Colors for C++

A module for printing easily coloured texts in C++

How to use

Colors, styles and functions are divided by different namespaces

Colors namespace

namespace Colors
{
  int DEF = 0;
  int RED = 31;
  int GREEN = 32;
  int YELLOW = 33;
  int BLUE = 34;
  int PURPLE = 35;
  int AQUA = 36;
};

This namespace contains all the codes for every color

Styles namespace

namespace Styles
{
  int BOLD = 1;
  int ITALIC = 3;
  int UNDERLINED = 4;
  int BLINK = 5;
  int REVERSE = 7; //Background -> forground; forground -> background
  int TRANSPARENT = 8;
};

This namespace contains all the codes for every style a text can have
You can add multiple styles for a single text

Codes namespace

This namespace contains all the function for make your code easy to write and read

Can show colors function

Not all terminals can show colors
You can check if the user's terminal can by calling the function "can_show_colors"

bool can_show_colors();

This function watches the environment variable "TERM", and checks if the value is in the vector "validTerminals"

vector<string> validTerminals = {
  "xterm",
  "xterm-256color",
};

If your terminal supports colors but it's not in the array, please, make a pull request and add the terminal's name
(You can see the name by debugging the variable "term")

String function

string String(int code);

This function is used for making the final string to print.
You don't have to call this function, it will be called when you need a color

Color functions

//Example of a function that returns a color
string red();
//Example of a function that returns a style
string bold();

These are the functions that you have to call when you need to style or color a text.
You just have to call this function when printing before the text:

cout << Codes::red() << Codes::bold() << "this is a red bold text" << endl;

Resetting style

If you want to reset the style after using it, you can choose between:

  • Resetting everything by using the "def" function
cout << Codes::red() << Codes::bold() << "this is a red bold text" << Codes::def() << endl; //Resetting every style added
  • Resetting a single style (not a color or bold style) by using the "off" function, with the style code which you can find inside the "Styles" namespace
cout << Codes::red() << Codes::bold() << "this is a red bold text" << Codes::off(Styles::BOLD) << endl; //Resetting only the bold style

Editing text's style using vectors

If you don't want to add every single style everytime you need it, you can use vectors and the "make" function

string make(
  string String, //Text to style
  vector<string> styles, //Styles to add
  vector<string> ends={def()} //Styles to reset at the end, by default it resets everything
  );

For example:

vector<string> styles = {
  Codes::red(),
  Codes::underline()
};

vector<string> styles_to_remove = {
  Codes::off(Styles::UNDERLINED)
};

cout << Codes::make("Red and underlined text", styles, styles_to_remove);
cout << "Red text, because I only reset the underline style" << endl;

Backgrounds

If you want to set a background color for your text, you use the "background" function.

string background(int code);

Available color codes are inside the "Colors" namespace.

Links and socials

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages