ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
sgr.h File Reference

ANSI SGR (Select Graphic Rendition) color sequence generation. More...

Go to the source code of this file.

Functions

void prewarm_sgr256_fg_cache (void)
 Prewarm 256-color foreground cache for benchmarks.
 
void prewarm_sgr256_cache (void)
 Prewarm 256-color foreground/background cache for benchmarks.
 
char * get_sgr256_fg_string (uint8_t fg, uint8_t *len_out)
 Get 256-color foreground ANSI sequence string.
 
char * get_sgr256_bg_string (uint8_t bg, uint8_t *len_out)
 Get 256-color background ANSI sequence string.
 
char * get_sgr256_fg_bg_string (uint8_t fg, uint8_t bg, uint8_t *len_out)
 Get 256-color foreground/background ANSI sequence string.
 
char * append_sgr_reset (char *dst)
 
char * append_sgr_truecolor_fg (char *dst, uint8_t r, uint8_t g, uint8_t b)
 
char * append_sgr_truecolor_bg (char *dst, uint8_t r, uint8_t g, uint8_t b)
 
char * append_sgr_truecolor_fg_bg (char *dst, uint8_t fr, uint8_t fg, uint8_t fb, uint8_t br, uint8_t bg, uint8_t bb)
 

Detailed Description

ANSI SGR (Select Graphic Rendition) color sequence generation.

Definition in file sgr.h.

Function Documentation

◆ append_sgr_reset()

char * append_sgr_reset ( char *  dst)
inline

Definition at line 156 of file sgr.c.

156 {
157 // "\x1b[0m"
158 static const char RESET[] = "\033[0m";
159 memcpy(dst, RESET, sizeof(RESET) - 1);
160 return dst + (sizeof(RESET) - 1);
161}

◆ append_sgr_truecolor_bg()

char * append_sgr_truecolor_bg ( char *  dst,
uint8_t  r,
uint8_t  g,
uint8_t  b 
)
inline

Definition at line 225 of file sgr.c.

225 {
226 // Constructor ensures initialization
227
228 // Direct character writes for "\033[48;2;"
229 *dst++ = '\033';
230 *dst++ = '[';
231 *dst++ = '4';
232 *dst++ = '8';
233 *dst++ = ';';
234 *dst++ = '2';
235 *dst++ = ';';
236
237 // Optimized digit copying
238 const dec3_t *rd = &g_dec3_cache.dec3_table[r];
239 if (rd->len == 1) {
240 *dst++ = rd->s[0];
241 } else if (rd->len == 2) {
242 dst[0] = rd->s[0];
243 dst[1] = rd->s[1];
244 dst += 2;
245 } else {
246 dst[0] = rd->s[0];
247 dst[1] = rd->s[1];
248 dst[2] = rd->s[2];
249 dst += 3;
250 }
251 *dst++ = ';';
252
253 const dec3_t *gd = &g_dec3_cache.dec3_table[g];
254 if (gd->len == 1) {
255 *dst++ = gd->s[0];
256 } else if (gd->len == 2) {
257 dst[0] = gd->s[0];
258 dst[1] = gd->s[1];
259 dst += 2;
260 } else {
261 dst[0] = gd->s[0];
262 dst[1] = gd->s[1];
263 dst[2] = gd->s[2];
264 dst += 3;
265 }
266 *dst++ = ';';
267
268 const dec3_t *bd = &g_dec3_cache.dec3_table[b];
269 if (bd->len == 1) {
270 *dst++ = bd->s[0];
271 } else if (bd->len == 2) {
272 dst[0] = bd->s[0];
273 dst[1] = bd->s[1];
274 dst += 2;
275 } else {
276 dst[0] = bd->s[0];
277 dst[1] = bd->s[1];
278 dst[2] = bd->s[2];
279 dst += 3;
280 }
281 *dst++ = 'm';
282 return dst;
283}
global_dec3_cache_t g_dec3_cache
Global decimal cache instance.
Decimal conversion cache structure (1-3 digits)

References global_dec3_cache_t::dec3_table, g_dec3_cache, dec3_t::len, and dec3_t::s.

◆ append_sgr_truecolor_fg()

char * append_sgr_truecolor_fg ( char *  dst,
uint8_t  r,
uint8_t  g,
uint8_t  b 
)
inline

Definition at line 164 of file sgr.c.

164 {
165 // Constructor ensures initialization
166
167 // Direct character writes (compiler will optimize to word operations)
168 *dst++ = '\033';
169 *dst++ = '[';
170 *dst++ = '3';
171 *dst++ = '8';
172 *dst++ = ';';
173 *dst++ = '2';
174 *dst++ = ';';
175
176 // Fast digit copying for 1-3 digit numbers (avoid memcpy overhead)
177 const dec3_t *rd = &g_dec3_cache.dec3_table[r];
178 if (rd->len == 1) {
179 *dst++ = rd->s[0];
180 } else if (rd->len == 2) {
181 dst[0] = rd->s[0];
182 dst[1] = rd->s[1];
183 dst += 2;
184 } else {
185 dst[0] = rd->s[0];
186 dst[1] = rd->s[1];
187 dst[2] = rd->s[2];
188 dst += 3;
189 }
190 *dst++ = ';';
191
192 const dec3_t *gd = &g_dec3_cache.dec3_table[g];
193 if (gd->len == 1) {
194 *dst++ = gd->s[0];
195 } else if (gd->len == 2) {
196 dst[0] = gd->s[0];
197 dst[1] = gd->s[1];
198 dst += 2;
199 } else {
200 dst[0] = gd->s[0];
201 dst[1] = gd->s[1];
202 dst[2] = gd->s[2];
203 dst += 3;
204 }
205 *dst++ = ';';
206
207 const dec3_t *bd = &g_dec3_cache.dec3_table[b];
208 if (bd->len == 1) {
209 *dst++ = bd->s[0];
210 } else if (bd->len == 2) {
211 dst[0] = bd->s[0];
212 dst[1] = bd->s[1];
213 dst += 2;
214 } else {
215 dst[0] = bd->s[0];
216 dst[1] = bd->s[1];
217 dst[2] = bd->s[2];
218 dst += 3;
219 }
220 *dst++ = 'm';
221 return dst;
222}

References global_dec3_cache_t::dec3_table, g_dec3_cache, dec3_t::len, and dec3_t::s.

◆ append_sgr_truecolor_fg_bg()

char * append_sgr_truecolor_fg_bg ( char *  dst,
uint8_t  fr,
uint8_t  fg,
uint8_t  fb,
uint8_t  br,
uint8_t  bg,
uint8_t  bb 
)
inline

Definition at line 286 of file sgr.c.

287 {
288 // Constructor ensures initialization
289
290 // Write "\033[38;2;" directly (7 chars)
291 *dst++ = '\033';
292 *dst++ = '[';
293 *dst++ = '3';
294 *dst++ = '8';
295 *dst++ = ';';
296 *dst++ = '2';
297 *dst++ = ';';
298
299 // Foreground RGB digits
300 const dec3_t *d = &g_dec3_cache.dec3_table[fr];
301 if (d->len == 1) {
302 *dst++ = d->s[0];
303 } else if (d->len == 2) {
304 dst[0] = d->s[0];
305 dst[1] = d->s[1];
306 dst += 2;
307 } else {
308 dst[0] = d->s[0];
309 dst[1] = d->s[1];
310 dst[2] = d->s[2];
311 dst += 3;
312 }
313 *dst++ = ';';
314
315 d = &g_dec3_cache.dec3_table[fg];
316 if (d->len == 1) {
317 *dst++ = d->s[0];
318 } else if (d->len == 2) {
319 dst[0] = d->s[0];
320 dst[1] = d->s[1];
321 dst += 2;
322 } else {
323 dst[0] = d->s[0];
324 dst[1] = d->s[1];
325 dst[2] = d->s[2];
326 dst += 3;
327 }
328 *dst++ = ';';
329
330 d = &g_dec3_cache.dec3_table[fb];
331 if (d->len == 1) {
332 *dst++ = d->s[0];
333 } else if (d->len == 2) {
334 dst[0] = d->s[0];
335 dst[1] = d->s[1];
336 dst += 2;
337 } else {
338 dst[0] = d->s[0];
339 dst[1] = d->s[1];
340 dst[2] = d->s[2];
341 dst += 3;
342 }
343
344 // Write ";48;2;" directly (6 chars)
345 *dst++ = ';';
346 *dst++ = '4';
347 *dst++ = '8';
348 *dst++ = ';';
349 *dst++ = '2';
350 *dst++ = ';';
351
352 // Background RGB digits
353 d = &g_dec3_cache.dec3_table[br];
354 if (d->len == 1) {
355 *dst++ = d->s[0];
356 } else if (d->len == 2) {
357 dst[0] = d->s[0];
358 dst[1] = d->s[1];
359 dst += 2;
360 } else {
361 dst[0] = d->s[0];
362 dst[1] = d->s[1];
363 dst[2] = d->s[2];
364 dst += 3;
365 }
366 *dst++ = ';';
367
368 d = &g_dec3_cache.dec3_table[bg];
369 if (d->len == 1) {
370 *dst++ = d->s[0];
371 } else if (d->len == 2) {
372 dst[0] = d->s[0];
373 dst[1] = d->s[1];
374 dst += 2;
375 } else {
376 dst[0] = d->s[0];
377 dst[1] = d->s[1];
378 dst[2] = d->s[2];
379 dst += 3;
380 }
381 *dst++ = ';';
382
383 d = &g_dec3_cache.dec3_table[bb];
384 if (d->len == 1) {
385 *dst++ = d->s[0];
386 } else if (d->len == 2) {
387 dst[0] = d->s[0];
388 dst[1] = d->s[1];
389 dst += 2;
390 } else {
391 dst[0] = d->s[0];
392 dst[1] = d->s[1];
393 dst[2] = d->s[2];
394 dst += 3;
395 }
396
397 *dst++ = 'm';
398 return dst;
399}

References global_dec3_cache_t::dec3_table, g_dec3_cache, dec3_t::len, and dec3_t::s.