-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.c
604 lines (567 loc) · 18.3 KB
/
colors.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#ifndef __COLORS_C__
#define __COLORS_C__ "Desmon.hak.anon"
#include "colors.h"
// genera un valor entre 0 y 255
unsigned int jenkins_hash(
unsigned int value,
unsigned int n1, unsigned int n2, unsigned int n3,
unsigned int n4, unsigned int n5, unsigned int n6)
{
value = (value + 0x7ed55d16) + (value << n1);
value = (value ^ 0xc761c23c) ^ (value >> n2);
value = (value + 0x165667b1) + (value << n3);
value = (value + 0xd3a2646c) ^ (value << n4);
value = (value + 0xfd7046c5) + (value << n5);
value = (value ^ 0xb55a4f09) ^ (value >> n6);
return value % 256;
}
// combina los valores de un array
void shuffle_array(int array[], int size)
{
srand(time(NULL));
for (int i = size - 1; i > 0; --i)
{
int j = rand() % (i + 1);
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// genera tres valores aparit de 1 usando 6 desplazamientos
void generate_three_values(
unsigned int x,
unsigned int *value1,
unsigned int *value2,
unsigned int *value3,
unsigned int n1, unsigned int n2, unsigned int n3,
unsigned int n4, unsigned int n5, unsigned int n6)
{
// si es 0 o menor o igual que 255
if ((x == 0) || (x >= 255))
{
printf("El numero debe estar en el rango de 0 a 255.\n");
return;
}
// Aplicar la función de dispersión hash a los valores iniciales
*value1 = jenkins_hash(x, n1, n2, n3, n4, n5, n6);
*value2 = jenkins_hash(*value1, n1, n2, n3, n4, n5, n6);
*value3 = jenkins_hash(*value2, n1, n2, n3, n4, n5, n6);
}
void resetColorTerminal()
{
#ifdef _WIN32
/*setConsoleBackgroundColor(BACKGROUND_BLACK);
setConsoleForegroundColor(FOREGROUND_WHITE);*/
// setConsoleColor(FOREGROUND_MASK, 0);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_MASK);
/*HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
WORD originalAttrs = csbi.wAttributes;
WORD foregroundAttrs = originalAttrs & 0x0F; // Máscara para obtener solo los bits del color del texto
WORD backgroundAttrs = originalAttrs & 0xF0; // Máscara para obtener solo los bits del color de fondo
WORD resetAttrs = foregroundAttrs | (backgroundAttrs << 4); // Combinar los atributos de texto y fondo
SetConsoleTextAttribute(hConsole, resetAttrs);*/
#else
printf(BACKGROUND_COLOR_RESET_ANSI);
#endif
}
void __attribute__((destructor)) _RESET_COLOR__()
{
// setConsoleColor(C_WHITE, C_BLACK);
// SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
resetColorTerminal();
}
#ifdef _WIN32
void resetConsoleForegroundColor()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
WORD attributes = consoleInfo.wAttributes;
attributes &= 0xFFF0; // Eliminar el color de la letra actual
attributes |= FOREGROUND_WHITE;
SetConsoleTextAttribute(hConsole, attributes);
}
void setConsoleForegroundColor(WORD foregroundColor)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
WORD attributes = consoleInfo.wAttributes;
attributes &= 0xFFF0; // Eliminar el color de la letra actual
attributes |= foregroundColor;
SetConsoleTextAttribute(hConsole, attributes);
}
#else
void resetConsoleForegroundColor()
{
resetColorTerminal();
}
void setConsoleForegroundColor(ConsoleColor foregroundColor)
{
printf("\033[%dm", 30 + foregroundColor);
}
#endif
#ifdef _WIN32
void resetConsoleBackgroundColor()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
WORD originalAttrs = csbi.wAttributes;
WORD backgroundAttrs = originalAttrs & 0xF0;
SetConsoleTextAttribute(hConsole, backgroundAttrs);
}
void setConsoleBackgroundColor(WORD backgroundColor)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
WORD attributes = consoleInfo.wAttributes;
attributes &= 0x000F; // Limpiar los bits de color de fondo existentes
attributes |= backgroundColor;
SetConsoleTextAttribute(hConsole, attributes);
}
#else
void resetConsoleBackgroundColor()
{
resetColorTerminal();
}
void setConsoleBackgroundColor(ConsoleColor backgroundColor)
{
printf("\033[0;%dm", 40 + backgroundColor);
}
#endif
#ifdef _WIN32
void __attribute__((constructor)) _ACTIVATE_COLORS_ANSI_WIN__()
{
// Habilitar el soporte de colores ANSI en la consola de Windows
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
if (GetConsoleMode(hOut, &dwMode))
{
if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
{
// Habilitar los colores ANSI en la consola de Windows
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
}
}
}
#endif
#ifdef _WIN32
void setConsoleColor(WORD foreground, WORD background)
{
// HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// SetConsoleTextAttribute(hConsole, (int)foreground | ((int)background << 4));
setConsoleForegroundColor(foreground);
setConsoleBackgroundColor(background);
}
#else
void setConsoleColor(ConsoleColor foreground, ConsoleColor background)
{
setConsoleForegroundColor(foreground);
setConsoleBackgroundColor(background);
}
#endif
void printf_color(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf_color(format, args);
va_end(args);
}
void vprintf_color(const char *format, va_list args)
{
va_list args_copy;
va_copy(args_copy, args);
size_t size = (vsnprintf(NULL, 0, format, args_copy) + 1) * sizeof(char);
va_end(args_copy);
char *formatted_buffer = (char *)malloc(size);
vsprintf(formatted_buffer, format, args);
const char *p = formatted_buffer;
bool in_color_code = false;
char color_code[30];
int color_code_index = 0;
while (*p != '\0')
{
if (in_color_code)
{
if (*p == '}')
{
color_code[color_code_index] = '\0';
// Token de color encontrado, procesarlo aquí
if (strncmp(color_code, "FG:red", 6) == 0)
{
// Cambiar a color rojo
LETTER_RED;
}
else if (strncmp(color_code, "FG:reset", 8) == 0)
{
// Restablecer color de primer plano
resetConsoleForegroundColor();
}
else if (strncmp(color_code, "BG:reset", 8) == 0)
{
// Restablecer color de fondo
resetConsoleBackgroundColor();
}
else if (strncmp(color_code, "FG:lred", 8) == 0)
{
LETTER_LIGHTRED_EX;
}
else if (strncmp(color_code, "FG:lblack", 8) == 0)
{
LETTER_LIGHTBLACK_EX;
}
else if (strncmp(color_code, "FG:lgreen", 8) == 0)
{
LETTER_LIGHTGREEN_EX;
}
else if (strncmp(color_code, "FG:lyellow", 8) == 0)
{
LETTER_LIGHTYELLOW_EX;
}
else if (strncmp(color_code, "FG:lblue", 8) == 0)
{
LETTER_LIGHTBLUE_EX;
}
else if (strncmp(color_code, "FG:lpurple", 8) == 0)
{
LETTER_LIGHTMAGENTA_EX;
}
else if (strncmp(color_code, "FG:lcyan", 8) == 0)
{
LETTER_LIGHTCYAN_EX;
}
else if (strncmp(color_code, "FG:lwhite", 8) == 0)
{
LETTER_LIGHTWHITE_EX;
}
else if (strncmp(color_code, "FG:green", 8) == 0)
{
// Cambiar a color verde
LETTER_GREEN;
}
else if (strncmp(color_code, "FG:blue", 7) == 0)
{
// Cambiar a color azul
LETTER_BLUE;
}
else if (strncmp(color_code, "FG:black", 8) == 0)
{
// Cambiar a color negro
LETTER_BLACK;
}
else if (strncmp(color_code, "FG:yellow", 9) == 0)
{
// Cambiar a color amarillo
LETTER_YELLOW;
}
else if (strncmp(color_code, "FG:purple", 9) == 0)
{
// Cambiar a color magenta
LETTER_MAGENTA;
}
else if (strncmp(color_code, "FG:cyan", 7) == 0)
{
// Cambiar a color cian
LETTER_CYAN;
}
else if (strncmp(color_code, "FG:white", 8) == 0)
{
// Cambiar a color blanco
LETTER_WHITE;
}
else if (strncmp(color_code, "BG:black", 8) == 0)
{
// Cambiar a fondo negro
BACKGROUND_COLOR_BLACK;
}
else if (strncmp(color_code, "BG:red", 6) == 0)
{
// Cambiar a fondo rojo
BACKGROUND_COLOR_RED;
}
else if (strncmp(color_code, "BG:green", 8) == 0)
{
// Cambiar a fondo verde
BACKGROUND_COLOR_GREEN;
}
else if (strncmp(color_code, "BG:yellow", 9) == 0)
{
// Cambiar a fondo amarillo
BACKGROUND_COLOR_YELLOW;
}
else if (strncmp(color_code, "BG:purple", 9) == 0)
{
// Cambiar a fondo magenta
BACKGROUND_COLOR_MAGENTA;
}
else if (strncmp(color_code, "BG:cyan", 7) == 0)
{
// Cambiar a fondo cian
BACKGROUND_COLOR_CYAN;
}
else if (strncmp(color_code, "BG:white", 8) == 0)
{
// Cambiar a fondo blanco
BACKGROUND_COLOR_WHITE;
}
else if (strncmp(color_code, "BG:blue", 7) == 0)
{
// Cambiar a fondo azul
BACKGROUND_COLOR_BLUE;
}
else if (strncmp(color_code, "FG:", 3) == 0)
{
// Comprobar si es un color personalizado FG:r;g;b
unsigned char red, green, blue;
if (sscanf(color_code, "FG:%hhu;%hhu;%hhu", &red, &green, &blue) == 3)
{
// Cambiar a color personalizado
foreground_color_custom_(red, green, blue);
}
}
else if (strncmp(color_code, "BG:", 3) == 0)
{
// Comprobar si es un color personalizado FG:r;g;b
unsigned char red, green, blue;
if (sscanf(color_code, "BG:%hhu;%hhu;%hhu", &red, &green, &blue) == 3)
{
// Cambiar a color personalizado
background_color_custom_(red, green, blue);
}
}
else if (strncmp(color_code, "i64:", 4) == 0)
{
sizes_num num;
if (sscanf(color_code, "i64:%"PRIu64, &num.i64))
{
print_sizes_num(num, 64);
}
}
else if (strncmp(color_code, "i32:", 4) == 0)
{
sizes_num num;
if (sscanf(color_code, "i32:%"SCNu32, &num.i32))
{
print_sizes_num(num, 32);
}
}
else if (strncmp(color_code, "i16:", 4) == 0)
{
sizes_num num;
if (sscanf(color_code, "i16:%hu", &num.i16))
{
print_sizes_num(num, 16);
}
}
else if (strncmp(color_code, "i8:", 3) == 0)
{
sizes_num num;
if (sscanf(color_code, "i8:%hhu", &num.i8))
{
print_sizes_num(num, 8);
printf("-%d-\n", num.i8);
}
else
{
print_sizes_num((sizes_num){.i8 = 0}, 8);
}
// printf("%d\n", num.i8);
}
else
{
printf("%s: identificador invalido", color_code);
}
in_color_code = false;
color_code_index = 0;
}
else
{
color_code[color_code_index] = *p;
color_code_index++;
}
}
else
{
if (*p == '#')
{
p++;
if (*p == '{')
{
in_color_code = true;
color_code_index = 0;
}
else
{
putchar('#');
putchar(*p);
fflush(stdout);
}
}
else
{
if (*p == '\n')
{
resetColorTerminal();
}
putchar(*p);
fflush(stdout);
}
}
p++;
}
// Restablecer colores
resetColorTerminal();
free(formatted_buffer);
}
void clear_line()
{
printf(CLEAR_LINE);
}
void clear_display()
{
printf(CLEAR_DISPLAY);
}
void set_title(const char *title)
{
printf(SET_TITLE("%s"), title);
}
void pos(const unsigned char x, const unsigned char y, const char *data)
{
printf(POS("%s", "%d", "%d"), x, y, data);
}
void back(const char *data, const unsigned char number)
{
printf(BACK("%s", "%d"), number, data);
}
void forward(const char *data, const unsigned char number)
{
printf(FORWARD("%s", "%d"), number, data);
}
void down(const char *data, const unsigned char number)
{
printf(DOWN("%s", "%d"), number, data);
}
void up(const char *data, const unsigned char number)
{
printf(UP("%s", "%d"), number, data);
}
#ifndef __DISABLE_COLORS_FORE_BACK_GROUND__
static inline void foreground_color_custom_RGB(RGB_C color)
{
foreground_color_custom_(color.r, color.g, color.b);
}
static void foreground_color_custom_(const unsigned char red, const unsigned char green, const unsigned char blue)
{
printf(FOREGROUND_COLOR_CUSTOM_RGB("%d", "%d", "%d"), red, green, blue);
}
static inline void background_color_custom_RGB(RGB_C color)
{
background_color_custom_(color.red, color.green, color.blue);
}
static void background_color_custom_(const unsigned char red, const unsigned char green, const unsigned char blue)
{
printf(BACKGROUND_COLOR_CUSTOM_RGB("%d", "%d", "%d"), red, green, blue);
}
#else
static inline void background_color_custom_RGB(RGB_C color)
{
return; // no comptible para win7
}
static void background_color_custom_(const unsigned char red, const unsigned char green, const unsigned char blue)
{
return; // no comptible para win7
}
static inline void foreground_color_custom_RGB(RGB_C color)
{
return; // no comptible para win7
}
static void foreground_color_custom_(const unsigned char red, const unsigned char green, const unsigned char blue)
{
return; // no comptible para win7
}
#endif
static inline void back_fore_color_custom_RGB(RGB_C colorBackGround, RGB_C colorForeGround)
{
back_fore_color_custom_(
colorBackGround.r,
colorBackGround.g,
colorBackGround.b,
colorForeGround.r,
colorForeGround.g,
colorForeGround.b);
}
static void back_fore_color_custom_(unsigned char redB, unsigned char greenB,
unsigned char blueB, unsigned char redF,
unsigned char greenF, unsigned char blueF)
{
foreground_color_custom_(redF, greenF, blueF);
background_color_custom_(redB, greenB, blueB);
}
void ANSI_fore_color(ANSIColors color)
{
printf(ANSI_COLOR_FOREGROUNG("%d"), color);
}
void ANSI_back_color(ANSIColors color)
{
printf(ANSI_COLOR_BACKGROUNG("%d"), color);
}
void print_sizes_num(sizes_num byte, size_t size_word)
{
switch (size_word)
{
case 8:
for (int i = 7; i >= 0; i--)
{
unsigned char mask = 1U << i;
unsigned char bit = byte.i8 & mask ? '1' : '0';
putchar(bit);
}
break;
case 16:
for (int i = 15; i >= 0; i--)
{
unsigned short mask = 1U << i;
unsigned char bit = byte.i16 & mask ? '1' : '0';
putchar(bit);
}
break;
case 32:
for (int i = 31; i >= 0; i--)
{
unsigned long mask = 1UL << i;
unsigned char bit = byte.i32 & mask ? '1' : '0';
putchar(bit);
}
break;
case 64:
for (int i = 63; i >= 0; i--)
{
unsigned long long mask = 1ULL << i;
unsigned char bit = byte.i64 & mask ? '1' : '0';
putchar(bit);
}
break;
default:
puts("print_sizes_num: size_word desconocido");
break;
}
}
void print_bin(const void *data, size_t size)
{
const unsigned char *bytePtr = (const unsigned char *)data;
for (size_t byte = 0; byte < size; byte++)
{
for (size_t i = 8; i > 0; i--)
{
unsigned char mask = 1U << (i - 1);
unsigned char bit = bytePtr[byte] & mask ? '1' : '0';
putchar(bit);
}
}
}
#endif