Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
color.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
16
17#include <sham/term/color.hpp>
18#include <string>
19
20#define TERM_ESCAPTE_CHAR "\x1b["
21namespace {
25
26 const char *_empty_str = "";
27 const char *_esc_char = TERM_ESCAPTE_CHAR;
28 const char *_reset = TERM_ESCAPTE_CHAR "0m";
29 const char *_bold = TERM_ESCAPTE_CHAR "1m";
30 const char *_faint = TERM_ESCAPTE_CHAR "2m";
31 const char *_underline = TERM_ESCAPTE_CHAR "4m";
32 const char *_blink = TERM_ESCAPTE_CHAR "5m";
33 const char *_col8b_black = TERM_ESCAPTE_CHAR "30m";
34 const char *_col8b_red = TERM_ESCAPTE_CHAR "31m";
35 const char *_col8b_green = TERM_ESCAPTE_CHAR "32m";
36 const char *_col8b_yellow = TERM_ESCAPTE_CHAR "33m";
37 const char *_col8b_blue = TERM_ESCAPTE_CHAR "34m";
38 const char *_col8b_magenta = TERM_ESCAPTE_CHAR "35m";
39 const char *_col8b_cyan = TERM_ESCAPTE_CHAR "36m";
40 const char *_col8b_white = TERM_ESCAPTE_CHAR "37m";
41} // namespace
42
43namespace sham::term {
44
45 ColorLevel color_level() { return color_level_value; }
46 void set_color_level(ColorLevel level) { color_level_value = level; }
47
51
54
57
58 namespace style {
59 const char *reset() { return are_colors_enabled() ? _reset : _empty_str; }
60 const char *bold() { return are_colors_enabled() ? _bold : _empty_str; }
61 const char *faint() { return are_colors_enabled() ? _faint : _empty_str; }
62 const char *underline() { return are_colors_enabled() ? _underline : _empty_str; }
63 const char *blink() { return are_colors_enabled() ? _blink : _empty_str; }
64 } // namespace style
65
66 namespace colors_8b {
67 const char *black() { return are_colors_enabled() ? _col8b_black : _empty_str; }
68 const char *red() { return are_colors_enabled() ? _col8b_red : _empty_str; }
69 const char *green() { return are_colors_enabled() ? _col8b_green : _empty_str; }
70 const char *yellow() { return are_colors_enabled() ? _col8b_yellow : _empty_str; }
71 const char *blue() { return are_colors_enabled() ? _col8b_blue : _empty_str; }
72 const char *magenta() { return are_colors_enabled() ? _col8b_magenta : _empty_str; }
73 const char *cyan() { return are_colors_enabled() ? _col8b_cyan : _empty_str; }
74 const char *white() { return are_colors_enabled() ? _col8b_white : _empty_str; }
75 } // namespace colors_8b
76
77 namespace colors_256 {
78 namespace {
81 std::string build(int mode, std::uint8_t index) {
82 if (color_level_value < ColorLevel::ANSI256) {
83 return "";
84 }
85 return std::string(TERM_ESCAPTE_CHAR) + std::to_string(mode) + ";5;"
86 + std::to_string(index) + "m";
87 }
88 } // namespace
89
90 std::string foreground(std::uint8_t index) { return build(38, index); }
91 std::string background(std::uint8_t index) { return build(48, index); }
92 } // namespace colors_256
93
94 namespace colors_24b {
95 namespace {
98 std::string build(int mode, std::uint8_t r, std::uint8_t g, std::uint8_t b) {
99 if (color_level_value < ColorLevel::TrueColor) {
100 return "";
101 }
102 return std::string(TERM_ESCAPTE_CHAR) + std::to_string(mode) + ";2;"
103 + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b)
104 + "m";
105 }
106 } // namespace
107
108 std::string foreground(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
109 return build(38, r, g, b);
110 }
111 std::string background(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
112 return build(48, r, g, b);
113 }
114 } // namespace colors_24b
115
116} // namespace sham::term
Terminal color escape sequences and enable/disable control.
ColorLevel
The possible levels of terminal color support.
Definition color.hpp:27
@ NoColor
No color support, plain ASCII output only.
Definition color.hpp:29
@ Basic
ANSI/16 colors (basic SGR codes), \x1b[3Xm.
Definition color.hpp:31
ColorLevel color_level()
Query the currently detected/forced terminal color support level.
Definition color.cpp:45
void set_color_level(ColorLevel level)
Set the terminal color support level.
Definition color.cpp:46
void enable_colors()
Enable colors.
Definition color.cpp:50
void disable_colors()
Disable all colors.
Definition color.cpp:53
bool are_colors_enabled()
Query whether terminal color output is currently enabled.
Definition color.cpp:56
std::string foreground(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Escape sequence to set the given RGB foreground text color.
Definition color.cpp:108
std::string background(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Escape sequence to set the given RGB background color.
Definition color.cpp:111
std::string background(std::uint8_t index)
Escape sequence to set the given palette index as background color.
Definition color.cpp:91
std::string foreground(std::uint8_t index)
Escape sequence to set the given palette index as foreground text color.
Definition color.cpp:90
const char * red()
Escape sequence to set red text color.
Definition color.cpp:68
const char * green()
Escape sequence to set green text color.
Definition color.cpp:69
const char * black()
Escape sequence to set black text color.
Definition color.cpp:67
const char * white()
Escape sequence to set white text color.
Definition color.cpp:74
const char * magenta()
Escape sequence to set magenta (pink) text color.
Definition color.cpp:72
const char * cyan()
Escape sequence to set cyan text color.
Definition color.cpp:73
const char * blue()
Escape sequence to set blue text color.
Definition color.cpp:71
const char * yellow()
Escape sequence to set yellow text color.
Definition color.cpp:70
const char * blink()
escape sequence to set blinking text formatting.
Definition color.cpp:63
const char * reset()
escape sequence to reset the terminal text formatting.
Definition color.cpp:59
const char * bold()
escape sequence to set bold text formatting.
Definition color.cpp:60
const char * underline()
escape sequence to set underlined text formatting.
Definition color.cpp:62
const char * faint()
escape sequence to set faint (dim) text formatting.
Definition color.cpp:61