stc
Loading...
Searching...
No Matches
Colour.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <cstdint>
5#include <ostream>
6#include "Environment.hpp"
7
41namespace stc::colour {
42
57enum class FourBitColour {
58 BLACK = 30,
59 RED = 31,
60 GREEN = 32,
61 YELLOW = 33,
62 BLUE = 34,
63 MAGENTA = 35,
64 CYAN = 36,
65 WHITE = 37,
66
67 BRIGHT_BLACK = 90,
68 BRIGHT_RED = 91,
69 BRIGHT_GREEN = 92,
70 BRIGHT_YELLOW = 93,
71 BRIGHT_BLUE = 94,
72 BRIGHT_MAGENTA = 95,
73 BRIGHT_CYAN = 96,
74 BRIGHT_WHITE = 97,
75};
76
87enum class Typography {
88 BOLD = 1,
89 FAINT = 2,
90 ITALIC = 3,
91 UNDERLINE = 4,
92 SLOW_BLINK = 5,
93
97 RESET_INTENSITY = 22,
98 NO_ITALIC = 23,
99 NO_UNDERLINE = 24,
100 NO_BLINKING = 25
101};
102
103namespace _detail {
104
105const int FOREGROUND = 38;
106const int BACKGROUND = 48;
107
108// forced: iword defaults to 0
109const int MODE_AUTO = 0;
110const int MODE_FORCE = 1;
111
112
113inline int getStreamConfigIdx() {
114 static int idx = std::ios::xalloc();
115 return idx;
116}
117
118template <typename CharT>
119static bool shouldPrintColour(std::basic_ostream<CharT>& ss) {
120 auto iword = ss.iword(getStreamConfigIdx());
121 if (iword == MODE_FORCE) {
122 return true;
123 }
124
125 return isCppStreamTTY(ss);
126}
127
128template <int Mode>
134 template <FourBitColour Colour, typename CharT>
135 static constexpr std::basic_ostream<CharT>& fourBit(std::basic_ostream<CharT>& stream) {
136 if (shouldPrintColour(stream)) {
137 stream << "\033["
138 << (Mode == _detail::FOREGROUND ? static_cast<int>(Colour) : (static_cast<int>(Colour) + 10))
139 << "m";
140 }
141 return stream;
142 }
143
150 template <uint8_t code, typename CharT>
151 static constexpr std::basic_ostream<CharT>& eightBit(std::basic_ostream<CharT>& stream) {
152 if (shouldPrintColour(stream)) {
153 stream << "\033[" << Mode << ";5;" << code << "m";
154 }
155 return stream;
156 }
157
168 template <uint8_t r, uint8_t g, uint8_t b, typename CharT>
169 static constexpr std::basic_ostream<CharT>& truecolour(std::basic_ostream<CharT>& stream) {
170 if (shouldPrintColour(stream)) {
171 stream << "\033[" << Mode << ";2;" << r << ";" << g << ";" << b << "m";
172 }
173 return stream;
174 }
175};
176
177}
178
181
186template <Typography feature, typename CharT>
187static constexpr std::basic_ostream<CharT>& use(std::basic_ostream<CharT>& stream) {
188 if (_detail::shouldPrintColour(stream)) {
189 stream << "\033[" << static_cast<int>(feature) << "m";
190 }
191 return stream;
192}
193
205template <bool val = true, typename CharT>
206static constexpr std::basic_ostream<CharT>& force(std::basic_ostream<CharT>& stream) {
208
209 return stream;
210}
211
215template <typename CharT>
216static constexpr std::basic_ostream<CharT>& reset(std::basic_ostream<CharT>& stream) {
217 if (_detail::shouldPrintColour(stream)) {
218 stream << "\033[0m";
219 }
220 return stream;
221}
222
228template <FourBitColour Colour, typename CharT>
229static constexpr std::basic_ostream<CharT>& fg(std::basic_ostream<CharT>& stream) {
231}
232
238template <uint8_t code, typename CharT>
239static constexpr std::basic_ostream<CharT>& fg(std::basic_ostream<CharT>& stream) {
240 return FgColour::eightBit<code, CharT>(stream);
241}
242
248template <uint8_t r, uint8_t g, uint8_t b, typename CharT>
249static constexpr std::basic_ostream<CharT>& fg(std::basic_ostream<CharT>& stream) {
251}
252
258template <FourBitColour Colour, typename CharT>
259static constexpr std::basic_ostream<CharT>& bg(std::basic_ostream<CharT>& stream) {
261}
262
268template <uint8_t code, typename CharT>
269static constexpr std::basic_ostream<CharT>& bg(std::basic_ostream<CharT>& stream) {
270 return BgColour::eightBit<code, CharT>(stream);
271}
272
278template <uint8_t r, uint8_t g, uint8_t b, typename CharT>
279static constexpr std::basic_ostream<CharT>& bg(std::basic_ostream<CharT>& stream) {
281}
282
283
284}
const int MODE_FORCE
Definition Colour.hpp:110
const int BACKGROUND
Definition Colour.hpp:106
const int FOREGROUND
Definition Colour.hpp:105
static bool shouldPrintColour(std::basic_ostream< CharT > &ss)
Definition Colour.hpp:119
const int MODE_AUTO
Definition Colour.hpp:109
int getStreamConfigIdx()
Definition Colour.hpp:113
Module for ANSI colours.
static constexpr std::basic_ostream< CharT > & bg(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:259
static constexpr std::basic_ostream< CharT > & fg(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:229
static constexpr std::basic_ostream< CharT > & use(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:187
Typography
Definition Colour.hpp:87
FourBitColour
Definition Colour.hpp:57
static constexpr std::basic_ostream< CharT > & reset(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:216
static constexpr std::basic_ostream< CharT > & force(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:206
bool isCppStreamTTY(std::basic_ostream< CharT > &ss)
Definition Environment.hpp:644
Definition Colour.hpp:129
static constexpr std::basic_ostream< CharT > & fourBit(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:135
static constexpr std::basic_ostream< CharT > & eightBit(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:151
static constexpr std::basic_ostream< CharT > & truecolour(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:169