-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.cpp
More file actions
101 lines (90 loc) · 2.52 KB
/
color.cpp
File metadata and controls
101 lines (90 loc) · 2.52 KB
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
// /////////////////////////////////////////////////////////
//
// File: clerdle/color.cpp
// Author: William Moon / Michael Foster
// Date: 2022.04.29
//
// This file contains utilities for displaying formatted
// data onto the console.
//
// /////////////////////////////////////////////////////////
#include "color.h"
//--------------//
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
const std::vector<Color> Color::colormap{
{"black", Color::black, 30, 40},
{"red", Color::red, 31, 41},
{"green", Color::green, 32, 42},
{"yellow", Color::yellow, 33, 43},
{"blue", Color::blue, 34, 44},
{"purple", Color::purple, 35, 45},
{"cyan", Color::cyan, 36, 46},
{"white", Color::white, 37, 47}};
const std::string Color::escape{"\033["};
Color::Color(const std::string &name_str, name color_name, int fg_code, int bg_code) : m_name_str{name_str},
m_name{color_name},
m_fg{fg_code},
m_bg{bg_code}
{
}
const Color &Color::getByName(name color_name)
{
const Color &cc = colormap.front();
for (const auto &c : colormap)
{
if (c.getName() == color_name)
{
return c;
}
}
return cc;
}
std::string Color::setFg(name fg)
{
std::stringstream s;
s << escape << getByName(fg).getFgCode() << "m";
return s.str();
}
std::string Color::setBg(name bg)
{
std::stringstream s;
s << escape << getByName(bg).getBgCode() << "m";
return s.str();
}
std::string Color::setColor(name fg, name bg)
{
std::stringstream s;
s << escape << getByName(bg).getBgCode()
<< ";" << getByName(fg).getFgCode()
<< "m";
return s.str();
}
std::string Color::reset()
{
return escape + "0m";
}
void Color::test()
{
std::cout << " ";
for (const auto &c : colormap)
{
std::cout << setBg(c.getName()) << " " << std::setw(7) << std::left << c.getNameStr();
}
std::cout << reset() << std::endl;
for (const auto &fg : colormap)
{
std::cout << setFg(fg.getName()) << std::setw(8) << fg.getNameStr();
for (const auto &bg : colormap)
{
std::cout << setColor(fg.getName(), bg.getName())
<< " " << fg.getNameStr().substr(0, 2)
<< "|" << bg.getNameStr().substr(0, 2) << " ";
}
std::cout << reset() << std::endl;
}
std::cout << reset();
}