CPPConsole
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1#ifndef __CPP_CONSOLE_COLOR_HPP__
2#define __CPP_CONSOLE_COLOR_HPP__
3
4#include <iostream>
5#include <string>
6
7#include "ansi.hpp"
8#include "graphics.hpp"
9
13namespace console {
19std::string colorize(const std::string& str, Graphics color) {
20 if (isANSIEnabled())
21 return "\033[" + std::to_string(color) + "m" + str + "\033[0m";
22 return str;
23}
24
36std::string colorize(const std::string& str, unsigned char color,
37 bool foreground = true) {
38 if (isANSIEnabled())
39 return "\033[" + std::to_string(foreground ? 38 : 48) + ";5;" +
40 std::to_string(color) + "m" + str + "\033[0m";
41 return str;
42}
43
54std::string colorize(const std::string& str, unsigned char r, unsigned char g,
55 unsigned char b, bool foreground = true) {
56 if (isANSIEnabled())
57 return "\033[" + std::to_string(foreground ? 38 : 48) + ";2;" +
58 std::to_string(r) + ";" + std::to_string(g) + ";" +
59 std::to_string(b) + "m" + str + "\033[0m";
60 return str;
61}
62
68void setColor(Graphics color) {
69 if (isANSIEnabled()) std::cout << "\033[" << std::to_string(color) << "m";
70}
71
83void setColor(unsigned char color, bool foreground = true) {
84 if (isANSIEnabled())
85 std::cout << "\033[" << (foreground ? "38" : "48") << ";5;"
86 << std::to_string(color) << "m";
87}
88
96void setColor(unsigned char r, unsigned char g, unsigned char b,
97 bool foreground = true) {
98 if (isANSIEnabled())
99 std::cout << "\033[" << (foreground ? "38" : "48") << ";2;"
100 << std::to_string(r) << ";" << std::to_string(g) << ";"
101 << std::to_string(b) << "m";
102}
103} // namespace console
104
105#endif
Namespace Containing all console commands.
Definition ansi.hpp:16
Graphics
Enumeration of builtin colors for ANSI escape codes, along with other graphics modes.
Definition graphics.hpp:19
void setColor(Graphics color)
Sets the color of the text to a predefined color.
Definition color.hpp:68
bool isANSIEnabled()
Checks if ANSI Codes are enable in windows.
Definition ansi.hpp:22
std::string colorize(const std::string &str, Graphics color)
Colors the text color to a predefined color.
Definition color.hpp:19