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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#include "richtext.h"
static int matchIRCColourString(const char *str, int pos, int *result) {
// First, try to match a single digit.
if (str[pos] >= '0' && str[pos] <= '9') {
int first = str[pos] - '0';
if (str[pos + 1] >= '0' && str[pos + 1] <= '9') {
int second = str[pos + 1] - '0';
int attempt = (first * 10) + second;
if (attempt < 0 || attempt > 15) {
// Invalid number, give up here
return pos;
} else {
// Valid number!
*result = attempt;
return pos + 2;
}
} else {
// Second character was not a digit, so
// let's take the colour we *did* parse as is
*result = first;
return pos + 1;
}
} else {
// First character was not a digit, so pass entirely.
return pos;
}
}
static int matchIRCColourPair(const char *str, int pos, int *whichFG, int *whichBG) {
// str[pos] is the first character following the
// \x03 colour code.
*whichFG = -1;
*whichBG = -1;
pos = matchIRCColourString(str, pos, whichFG);
if (*whichFG != -1) {
if (str[pos] == ',') {
pos = matchIRCColourString(str, pos + 1, whichBG);
}
}
return pos;
}
void RichTextBuilder::appendIRC(const char *str) {
// We're up for fun here!
const int colLayer = COL_LEVEL_IRC;
bool b = false, i = false, u = false;
int activeFG = COL_DEFAULT_FG, activeBG = COL_DEFAULT_BG;
int pos = 0;
int len = strlen(str);
while (pos < len) {
char ch = str[pos];
if (ch < 0 || ch == 7 || ch == 10 || ch >= 0x20) {
// Pass this character through unfiltered.
writeU8(ch);
pos++;
continue;
}
// IRC control code, but what?
if (ch == 2) {
b = !b;
if (b)
bold();
else
endBold();
} else if (ch == 0x1D) {
i = !i;
if (i)
italic();
else
endItalic();
} else if (ch == 0x1F) {
u = !u;
if (u)
underline();
else
endUnderline();
} else if (ch == 0xF) {
// Reset all formatting
if (b) {
endBold();
b = false;
}
if (i) {
endItalic();
i = false;
}
if (u) {
endUnderline();
u = false;
}
if (activeFG != COL_DEFAULT_FG) {
endForeground(colLayer);
activeFG = COL_DEFAULT_FG;
}
if (activeBG != COL_DEFAULT_BG) {
endBackground(colLayer);
activeBG = COL_DEFAULT_BG;
}
} else if (ch == 0x16) {
// Reverse
int swap = activeBG;
activeBG = activeFG;
activeFG = swap;
if (activeFG == COL_DEFAULT_FG)
endForeground(colLayer);
else
foreground(colLayer, activeFG);
if (activeBG == COL_DEFAULT_BG)
endBackground(colLayer);
else
background(colLayer, activeBG);
} else if (ch == 3) {
// Colours!
int whichFG = -1, whichBG = -1;
pos = matchIRCColourPair(str, pos + 1, &whichFG, &whichBG);
if (whichFG == -1) {
if (activeFG != COL_DEFAULT_FG) {
activeFG = COL_DEFAULT_FG;
endForeground(colLayer);
}
} else {
activeFG = whichFG;
foreground(colLayer, whichFG);
}
if (whichBG == -1) {
if (whichFG == -1 && activeBG != COL_DEFAULT_BG) {
activeBG = COL_DEFAULT_BG;
endBackground(colLayer);
}
} else {
activeBG = whichBG;
background(colLayer, whichBG);
}
continue;
}
pos++;
}
// Clean up any leftover state.
if (b)
endBold();
if (i)
endItalic();
if (u)
endUnderline();
if (activeFG != COL_DEFAULT_FG)
endForeground(colLayer);
if (activeBG != COL_DEFAULT_BG)
endBackground(colLayer);
}
const char *RichTextBuilder::c_str() {
if (size() == 0 || data()[size() - 1] != 0)
writeU8(0);
return data();
}
inline static float hslValue(float n1, float n2, float hue) {
if (hue > 6.0f)
hue -= 6.0f;
else if (hue < 0.0f)
hue += 6.0f;
if (hue < 1.0f)
return n1 + (n2 - n1) * hue;
else if (hue < 3.0f)
return n2;
else if (hue < 4.0f)
return n1 + (n2 - n1) * (4.0f - hue);
else
return n1;
}
void calculateHSL(float h, float s, float l, uint8_t *r, uint8_t *g, uint8_t *b) {
// make it RGB
if (s == 0) {
*r = *g = *b = l*255.0f;
} else {
float m1, m2;
if (l <= 0.5f)
m2 = l * (1.0f + s);
else
m2 = l + s - l * s;
m1 = 2.0f * s - m2;
*r = hslValue(m1, m2, h * 6.0f + 2.0) * 255.0f;
*g = hslValue(m1, m2, h * 6.0f) * 255.0f;
*b = hslValue(m1, m2, h * 6.0f - 2.0) * 255.0f;
}
}
void RichTextBuilder::appendNick(const char *nick) {
uint32_t hash = 0;
const char *c = nick;
while (*c)
hash = (hash * 17) + *(c++);
int hueBase = ((hash & 0xFF0000) >> 16);
int satBase = ((hash & 0xFF00) >> 8);
int lumBase = (hash & 0xFF);
uint8_t r, g, b;
calculateHSL(
hueBase / 255.0f,
(0.50f + (satBase / 765.0f)),
(0.40f + (lumBase / 1020.0f)),
&r, &g, &b);
foreground(COL_LEVEL_NICK, r, g, b);
append(nick);
endForeground(COL_LEVEL_NICK);
}
|