-
Notifications
You must be signed in to change notification settings - Fork 56
/
colors.cpp
137 lines (122 loc) · 10.4 KB
/
colors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* cpp-terminal
* C++ library for writing multi-platform terminal applications.
*
* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
*
* SPDX-License-Identifier: MIT
*/
#include "cpp-terminal/color.hpp"
#include "cpp-terminal/exception.hpp"
#include "cpp-terminal/iostream.hpp"
#include "cpp-terminal/style.hpp"
#include "cpp-terminal/tty.hpp"
#include "cpp-terminal/version.hpp"
#include <limits>
int main()
{
Term::cout << "Running cpp-terminal version: " << Term::Version::string() << " website : " << Term::homepage() << std::endl << std::endl;
try
{
if(Term::is_stdout_a_tty()) { Term::cout << "Standard output is attached to a terminal." << std::endl << std::endl; }
else { Term::cout << "Standard output is not attached to a terminal." << std::endl << std::endl; }
std::string mode;
if(Term::Terminfo::getColorMode() == Term::Terminfo::ColorMode::Bit24) { mode = "24bit"; }
else if(Term::Terminfo::getColorMode() == Term::Terminfo::ColorMode::Bit8) { mode = "8bit"; }
else if(Term::Terminfo::getColorMode() == Term::Terminfo::ColorMode::Bit4) { mode = "4bit"; }
else if(Term::Terminfo::getColorMode() == Term::Terminfo::ColorMode::Bit3) { mode = "3bit"; }
else if(Term::Terminfo::getColorMode() == Term::Terminfo::ColorMode::NoColor) { mode = "nocolor"; }
else { mode = "Unset"; }
Term::cout << "Terminal has " << mode << " color support" << std::endl << std::endl;
std::string text = "Some text with " + Term::color_fg(Term::Color::Name::Red) + color_bg(Term::Color::Name::Green) + "red on green" + color_bg(Term::Color::Name::Default) + color_fg(Term::Color::Name::Default);
text += " and some " + style(Term::Style::Bold) + "bold text" + style(Term::Style::Reset) + ".\n";
text += "Unicode works too: originally written by Ondřej Čertík.";
Term::cout << text << std::endl;
std::string rgb_text = "Some Text in " + Term::color_fg(std::numeric_limits<std::uint8_t>::max(), 0, 0) + 'R' + Term::color_fg(0, std::numeric_limits<std::uint8_t>::max(), 0) + 'G' + Term::color_fg(0, 0, std::numeric_limits<std::uint8_t>::max()) + 'B' + Term::color_fg(Term::Color::Name::Default);
Term::cout << rgb_text << std::endl;
Term::cout << "\n4bits colors:\n";
Term::cout << "*" << Term::color_bg(Term::Color::Name::Black) << " " << Term::color_bg(Term::Color::Name::Red) << " " << Term::color_bg(Term::Color::Name::Green) << " " << Term::color_bg(Term::Color::Name::Yellow) << " ";
Term::cout << Term::color_bg(Term::Color::Name::Blue) << " " << Term::color_bg(Term::Color::Name::Magenta) << " " << Term::color_bg(Term::Color::Name::Cyan) << " " << Term::color_bg(Term::Color::Name::White) << " ";
Term::cout << Term::color_bg(Term::Color::Name::Gray) << " " << Term::color_bg(Term::Color::Name::BrightRed) << " " << Term::color_bg(Term::Color::Name::BrightGreen) << " " << Term::color_bg(Term::Color::Name::BrightYellow) << " ";
Term::cout << Term::color_bg(Term::Color::Name::BrightBlue) << " " << Term::color_bg(Term::Color::Name::BrightMagenta) << " " << Term::color_bg(Term::Color::Name::BrightCyan) << " " << Term::color_bg(Term::Color::Name::BrightWhite) << " ";
Term::cout << Term::color_bg(Term::Color::Name::Default) << "*\n";
Term::cout << "\n8bits colors:\n";
Term::cout << "*";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 1) { Term::cout << Term::color_bg(i) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n";
Term::cout << "\n24bits color chart: \n";
Term::cout << "*";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(i, 0, 0) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n*";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(0, i, 0) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n*";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(0, 0, i) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n*";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(i, i, i) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n";
Term::cout << "\nColor conversion (24bit)\n";
/* red color space */
Term::cout << "24bit original: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(i, 0, 0) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 8bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(i, 0, 0).to8bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 4bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(i, 0, 0).to4bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 3bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(i, 0, 0).to3bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
/* green color space */
Term::cout << "*\n24bit original: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(0, i, 0) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 8bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(0, i, 0).to8bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 4bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(0, i, 0).to4bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 3bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(0, i, 0).to3bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
/* blue color space */
Term::cout << "*\n24bit original: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(0, 0, i) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 8bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(0, 0, i).to8bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 4bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(0, 0, i).to4bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 3bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(0, 0, i).to3bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
/* black / grey color space */
Term::cout << "*\n24bit original: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(i, i, i) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 8bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(i, i, i).to8bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 4bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(i, i, i).to4bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n24bit to 3bit: *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 3) { Term::cout << Term::color_bg(Term::Color(i, i, i).to3bits()) << " " << Term::color_bg(Term::Color::Name::Default); }
Term::cout << "*\n";
Term::cout << "\nColor conversion (8bit)\n";
Term::cout << "to 4bit *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 1) { Term::cout << Term::color_bg(Term::Color(i).to4bits()) << " " << Term::color_bg(Term::Color::Name::Default) << ""; }
Term::cout << "*\n";
Term::cout << "to 3bit *";
for(std::uint8_t i = 0; i < (std::numeric_limits<std::uint8_t>::max)(); i += 1) { Term::cout << Term::color_bg(Term::Color(i).to3bits()) << " " << Term::color_bg(Term::Color::Name::Default) << ""; }
Term::cout << "*\n";
Term::cout << "\nColor conversion (4bit)\n";
Term::cout << "to 3bit : *" << Term::color_bg(Term::Color(Term::Color::Name::Black).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::Red).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::Green).to3bits()) << " ";
Term::cout << Term::color_bg(Term::Color(Term::Color::Name::Yellow).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::Blue).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::Magenta).to3bits()) << " ";
Term::cout << Term::color_bg(Term::Color(Term::Color::Name::Cyan).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::White).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::Gray).to3bits()) << " ";
Term::cout << Term::color_bg(Term::Color(Term::Color::Name::BrightRed).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::BrightGreen).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::BrightYellow).to3bits()) << " ";
Term::cout << Term::color_bg(Term::Color(Term::Color::Name::BrightBlue).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::BrightMagenta).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::BrightCyan).to3bits()) << " ";
Term::cout << Term::color_bg(Term::Color(Term::Color::Name::BrightWhite).to3bits()) << " " << Term::color_bg(Term::Color::Name::Default) << " ";
Term::cout << "*" << std::endl; //Windows prob
}
catch(const Term::Exception& re)
{
Term::cerr << "cpp-terminal error: " << re.what() << std::endl;
return 2;
}
catch(...)
{
Term::cerr << "Unknown error." << std::endl;
return 1;
}
return 0;
}