Skip to content

Commit

Permalink
Implement 24-bit true color RGB stream augmentaion
Browse files Browse the repository at this point in the history
with test cases that show most of the features
(recommended compile flags: gnu++11)
  • Loading branch information
moshiba committed Oct 8, 2019
1 parent 17f6227 commit 6417a42
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
41 changes: 30 additions & 11 deletions aesc/csi.hpp
Expand Up @@ -60,7 +60,8 @@ class smanipiii { // manipulator that takes triple ints as arguments
};

template <typename CharT, typename Traits>
std::ostream& operator<<(std::ostream& os, const smanipiii& m) {
std::basic_ostream<CharT, Traits>& operator<<(
std::basic_ostream<CharT, Traits>& os, const smanipiii& m) {
m.f(os, m.i1, m.i2, m.i3); // call m’s function with m’s stored value i
return os;
}
Expand Down Expand Up @@ -431,23 +432,23 @@ constexpr const char* background_8bit_expr = "48;5;";
*/
} // anonymous namespace
namespace RGB {
inline smanip foreground(int r = 0, int g = 0, int b = 0) {
const int rgb = 16 + 36 * r + 6 * g + b;
inline smanipiii foreground(int r = 0, int g = 0, int b = 0) {
// @todo: assert 0 <= r,g,b <= 5
auto h = [](std::ostream& s, int c) -> std::ostream& {
s << CSI_expr << foreground_8bit_expr << c << SGR::color_end_expr;
auto h = [](std::ostream& s, int r, int g, int b) -> std::ostream& {
s << CSI_expr << foreground_8bit_expr << 16 + 36 * r + 6 * g + b
<< SGR::color_end_expr;
return s;
};
return smanip(h, rgb);
return smanipiii(h, r, g, b);
}
inline smanip background(int r = 0, int g = 0, int b = 0) {
const int rgb = 16 + 36 * r + 6 * g + b;
inline smanipiii background(int r = 0, int g = 0, int b = 0) {
// @todo: assert 0 <= r,g,b <= 5
auto h = [](std::ostream& s, int c) -> std::ostream& {
s << CSI_expr << background_8bit_expr << c << SGR::color_end_expr;
auto h = [](std::ostream& s, int r, int g, int b) -> std::ostream& {
s << CSI_expr << background_8bit_expr << 16 + 36 * r + 6 * g + b
<< SGR::color_end_expr;
return s;
};
return smanip(h, rgb);
return smanipiii(h, r, g, b);
}
} // namespace RGB

Expand Down Expand Up @@ -481,6 +482,24 @@ constexpr const char* foreground_24bit_expr = "38;2;";
constexpr const char* background_24bit_expr = "48;2;";
} // anonymous namespace
namespace RGB {
inline smanipiii foreground(int r = 0, int g = 0, int b = 0) {
// @todo: assert 0 <= r,g,b <= 255
auto h = [](std::ostream& s, int r, int g, int b) -> std::ostream& {
s << CSI_expr << foreground_24bit_expr << r << ";" << g << ";" << b
<< SGR::color_end_expr;
return s;
};
return smanipiii(h, r, g, b);
}
inline smanipiii background(int r = 0, int g = 0, int b = 0) {
// @todo: assert 0 <= r,g,b <= 255
auto h = [](std::ostream& s, int r, int g, int b) -> std::ostream& {
s << CSI_expr << background_24bit_expr << r << ";" << g << ";" << b
<< SGR::color_end_expr;
return s;
};
return smanipiii(h, r, g, b);
}
} // namespace RGB
} // namespace truecolor
} // namespace aesc
Expand Down
27 changes: 19 additions & 8 deletions test/show_color.cpp
Expand Up @@ -23,26 +23,37 @@
using namespace std;
using namespace aesc;
int main(int argc, char** argv) {
//cout << truecolor::RGB.foreground(200, 0, 0) << "haha" << SGR::reset << endl;
cout << "line1" << endl << "line2" << endl << cursor::up(1) << "33" << cursor::EL(cursor::clear::entire) << "a" << endl;
cout << SGR::bold << SGR::blink::slow << " bold" << SGR::reverse_color << " reversed" << SGR::reset_intensity << " reset bold" << SGR::cancel_blink << " reset blink" << SGR::cancel_inverse << " cancel reverse" << endl;
cout << "line1" << endl
<< "line2" << endl
<< cursor::up(1) << "33" << cursor::EL(cursor::clear::entire) << "a"
<< endl;
cout << SGR::bold << SGR::blink::slow << " bold" << SGR::reverse_color
<< " reversed" << SGR::reset_intensity << " reset bold"
<< SGR::cancel_blink << " reset blink" << SGR::cancel_inverse
<< " cancel reverse" << endl;
cout << " pre-reset" << SGR::reset << " post-reset" << endl;

cout << color::red << color::background::cyan << " bg_cyan, fg_red" << SGR::reset << endl;
cout << color::bright::red << color::bright::background::cyan << " bright bg_cyan, bright fg_red" << SGR::reset << endl;
cout << color::red << color::background::cyan << " bg_cyan, fg_red"
<< SGR::reset << endl;
cout << color::bright::red << color::bright::background::cyan
<< " bright bg_cyan, bright fg_red" << SGR::reset << endl;

cout << "24 level grey FOREground:" << endl << "start";
for (int i=0; i!=24; ++i) {
for (int i = 0; i != 24; ++i) {
cout << color256::grey::foreground(i) << "";
}
cout << SGR::reset << "done" << endl;
cout << "24 level grey BACKground:" << endl << "start";
for (int i=0; i!=24; ++i) {
for (int i = 0; i != 24; ++i) {
cout << color256::grey::background(i) << " ";
}
cout << SGR::reset << "done" << endl;

cout << color256::RGB::foreground(2,3,0) << "256 color RGB test sequence" << SGR::reset << endl;
cout << color256::RGB::foreground(2, 3, 0) << "256 color RGB test sequence"
<< SGR::reset << endl;

cout << truecolor::RGB::foreground(130, 250, 0)
<< "24-bit true color RGB test sequence" << SGR::reset << endl;

return 0;
}

0 comments on commit 6417a42

Please sign in to comment.