-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrealcolor.hpp
72 lines (61 loc) · 1.54 KB
/
realcolor.hpp
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
#pragma once
#include <iostream>
#include <cstdio>
#include <functional>
#include <sstream>
namespace realcolor
{
namespace
{
constexpr int red(int rgb)
{
return (rgb >> 16) & 0xFF;
}
constexpr int green(int rgb)
{
return (rgb >> 8) & 0xFF;
}
constexpr int blue(int rgb)
{
return rgb & 0xFF;
}
};
inline std::ostream& reset(std::ostream& stream)
{
stream << "\x1b[0m";
return stream;
}
inline std::string fg(int r, int g, int b)
{
std::stringstream color;
color << "\x1b[38;2;" << r << ";" << g << ";" << b << "m";
return color.str();
}
inline std::string bg(int r, int g, int b)
{
std::stringstream color;
color << "\x1b[48;2;" << r << ";" << g << ";" << b << "m";
return color.str();
}
//don't be scared of this template magic, this is just so we can pass all floating point types in (floats, doubles, etc)
template<typename floatType>
inline typename std::enable_if<std::is_floating_point<floatType>::value, std::string>::type
fg(floatType r, floatType g, floatType b)
{
return fg(static_cast<int>(r*0xFF), static_cast<int>(g*0xFF), static_cast<int>(b*0xFF));
}
template<typename floatType>
inline typename std::enable_if<std::is_floating_point<floatType>::value, std::string>::type
bg(floatType r, floatType g, floatType b)
{
return bg(static_cast<int>(r*0xFF), static_cast<int>(g*0xFF), static_cast<int>(b*0xFF));
}
inline std::string fg(int rgb)
{
return fg(red(rgb), green(rgb), blue(rgb));
}
inline std::string bg(int rgb)
{
return bg(red(rgb), green(rgb), blue(rgb));
}
};