koutil
Loading...
Searching...
No Matches
color_operators.h
Go to the documentation of this file.
1#ifndef KOUTIL_TERM_COLOR_OPERATORS_H
2#define KOUTIL_TERM_COLOR_OPERATORS_H
3
4#include "koutil/term/color.h"
5#include "koutil/util/utils.h"
6#include <cstddef>
7#include <ostream>
8#include <string_view>
9
10namespace koutil::term {
11
12namespace color_literals {
13
21 consteval Color operator""_color(const char* str, std::size_t size) {
22 std::string_view hex(str, size);
23 return Color::from_hex(hex);
24 }
25
33 consteval ColorBG operator""_bg(const char* str, std::size_t size) {
34 std::string_view hex(str, size);
35 return Color::from_hex(hex).as_bg();
36 }
37
45 consteval ColorFG operator""_fg(const char* str, std::size_t size) {
46 std::string_view hex(str, size);
47 return Color::from_hex(hex).as_fg();
48 }
49
50}
51
52std::ostream& operator<<(std::ostream& stream, ColorBG bg) {
53
54 switch (bg.color.tag) {
55 case Color::Tag::RGB:
56 stream << util::ESC << "[48;2;" << +bg.color.red << ';' << +bg.color.green << ';' << +bg.color.blue << 'm';
57 break;
58 case Color::Tag::ID:
59 stream << util::ESC << '[' << +bg.color.id() + 10 << 'm';
60 break;
61 }
62
63 return stream;
64}
65
66std::ostream& operator<<(std::ostream& stream, ColorFG fg) {
67
68 switch (fg.color.tag) {
69 case Color::Tag::RGB:
70 stream << util::ESC << "[38;2;" << +fg.color.red << ';' << +fg.color.green << ';' << +fg.color.blue << 'm';
71 break;
72 case Color::Tag::ID:
73 stream << util::ESC << '[' << +fg.color.id() << 'm';
74 break;
75 }
76
77 return stream;
78}
79
85std::ostream& reset_bg(std::ostream& stream) {
86 stream << util::ESC << "[49m";
87 return stream;
88}
89
95std::ostream& reset_fg(std::ostream& stream) {
96 stream << util::ESC << "[39m";
97 return stream;
98}
99
105std::ostream& reset_color(std::ostream& stream) {
106 stream << util::ESC << "[39;49m";
107 return stream;
108}
109
110}
111
112#endif
Definition color.h:11
std::ostream & operator<<(std::ostream &stream, ColorBG bg)
Definition color_operators.h:52
std::ostream & reset_fg(std::ostream &stream)
Resets foreground color to default.
Definition color_operators.h:95
std::ostream & reset_color(std::ostream &stream)
Resets foreground and background color to default.
Definition color_operators.h:105
std::ostream & reset_bg(std::ostream &stream)
Resets background color to default.
Definition color_operators.h:85
constexpr char ESC
Definition utils.h:20
Structure representing a background color.
Definition color.h:198
const Color color
Definition color.h:202
Structure representing a foreground color.
Definition color.h:208
const Color color
Definition color.h:212
Structure representing a color.
Definition color.h:23
constexpr channel_t id() const
Gets the ID value of the color.
Definition color.h:143
constexpr ColorBG as_bg() const
Converts the color to a ColorBG object.
Definition color.h:215
const channel_t red
Definition color.h:32
constexpr ColorFG as_fg() const
Converts the color to a ColorFG object.
Definition color.h:217
const Tag tag
Definition color.h:35
const channel_t blue
Definition color.h:34
const channel_t green
Definition color.h:33
static consteval Color from_hex(std::string_view hex)
Creates a Color object from a hexadecimal string.
Definition color.h:106