This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathterminal_color.cc
130 lines (116 loc) · 4.05 KB
/
terminal_color.cc
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "terminal_color.h"
#include <math.h>
#include "log.h"
TerminalColor::TerminalColor(std::unique_ptr<::Theme> theme)
: theme_(std::move(theme)) {
Log() << "CAN_CHANGE_COLOR: " << can_change_color();
Log() << "HAS_COLORS: " << has_colors();
}
TerminalColor::LAB TerminalColor::RGB2LAB(RGB rgb) {
float r = static_cast<float>(std::get<0>(rgb)) / 255.0f;
float g = static_cast<float>(std::get<1>(rgb)) / 255.0f;
float b = static_cast<float>(std::get<2>(rgb)) / 255.0f;
float x, y, z;
r = (r > 0.04045f) ? powf((r + 0.055f) / 1.055f, 2.4f) : r / 12.92f;
g = (g > 0.04045f) ? powf((g + 0.055f) / 1.055f, 2.4f) : g / 12.92f;
b = (b > 0.04045f) ? powf((b + 0.055f) / 1.055f, 2.4f) : b / 12.92f;
x = (r * 0.4124f + g * 0.3576f + b * 0.1805f) / 0.95047f;
y = (r * 0.2126f + g * 0.7152f + b * 0.0722f) / 1.00000f;
z = (r * 0.0193f + g * 0.1192f + b * 0.9505f) / 1.08883f;
x = (x > 0.008856f) ? powf(x, 1.0f / 3.0f) : (7.787f * x) + 16.0f / 116.0f;
y = (y > 0.008856f) ? powf(y, 1.0f / 3.0f) : (7.787f * y) + 16.0f / 116.0f;
z = (z > 0.008856f) ? powf(z, 1.0f / 3.0f) : (7.787f * z) + 16.0f / 116.0f;
return LAB((116.0f * y) - 16.0f, 500.0f * (x - y), 200.0f * (y - z));
}
template <int I, class T>
static T sqdiff(std::tuple<T, T, T> a, std::tuple<T, T, T> b) {
T d = std::get<I>(a) - std::get<I>(b);
return d * d;
}
float TerminalColor::RGBDistance(RGB a, RGB b) {
LAB la = RGB2LAB(a);
LAB lb = RGB2LAB(b);
return sqdiff<0>(la, lb) + sqdiff<1>(la, lb) + sqdiff<2>(la, lb);
}
int TerminalColor::ColorToIndex(Color c) {
RGB rgb(c.r, c.g, c.b);
auto it = color_cache_.find(rgb);
if (it != color_cache_.end()) {
return it->second;
}
if (can_change_color()) {
int n = next_color_++;
short r, g, b;
color_content(n, &r, &g, &b);
Log() << "crrent to " << r << "," << g << "," << b;
Log() << "COLOR[" << n << "]: " << (int)(c.r * 1000 / 255) << " "
<< (int)(c.g * 1000 / 255) << " " << (int)(c.b * 1000 / 255);
Log() << init_color(n, c.r * 1000 / 255, c.g * 1000 / 255,
c.b * 1000 / 255);
color_content(n, &r, &g, &b);
Log() << "set to " << r << "," << g << "," << b;
color_cache_.insert(std::make_pair(rgb, n));
return n;
} else {
float best_diff = -1;
int best_clr = -1;
for (int i = 0; i < COLORS; i++) {
short r, g, b;
color_content(i, &r, &g, &b);
RGB sys(r * 255 / 1000, g * 255 / 1000, b * 255 / 1000);
float diff = RGBDistance(rgb, sys);
Log() << "color[" << i << " is " << r << "," << g << "," << b
<< " ; diff=" << diff;
if (i == 0 || best_diff > diff) {
best_diff = diff;
best_clr = i;
}
}
color_cache_.insert(std::make_pair(rgb, best_clr));
return best_clr;
}
}
chtype TerminalColor::Theme(::Theme::Tag token, uint32_t flags) {
auto key = std::make_pair(token, flags);
auto it = cache_.find(key);
if (it != cache_.end()) return it->second;
chtype c = Lookup(theme_->ThemeToken(token, flags));
cache_.insert(std::make_pair(key, c));
return c;
}
chtype TerminalColor::Lookup(CharFmt fmt) {
auto it = cfcache_.find(fmt);
if (it != cfcache_.end()) return it->second;
int fg = ColorToIndex(fmt.foreground);
int bg = ColorToIndex(fmt.background);
auto pit = pair_cache_.find(std::make_pair(fg, bg));
if (pit == pair_cache_.end()) {
int n = next_pair_++;
Log() << "PAIR[" << n << "]: " << fg << "," << bg;
Log() << init_pair(n, fg, bg);
pit = pair_cache_.insert(std::make_pair(std::make_pair(fg, bg), n)).first;
}
chtype highlight_or = 0;
switch (fmt.highlight) {
case Highlight::UNSET:
case Highlight::NONE:
break;
case Highlight::UNDERLINE:
highlight_or = A_UNDERLINE;
break;
case Highlight::BOLD:
highlight_or = A_BOLD;
break;
case Highlight::ITALIC:
#ifdef A_ITALIC
highlight_or = A_ITALIC;
#endif
break;
case Highlight::STRIKE:
highlight_or = A_BLINK;
break;
}
return cfcache_
.insert(std::make_pair(fmt, COLOR_PAIR(pit->second) | highlight_or))
.first->second;
}