-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorcat.c
405 lines (366 loc) · 7.14 KB
/
colorcat.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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
ssize_t getline(char **ptr, size_t *n, FILE *stream)
{
if (!*ptr)
*ptr = malloc(100);
if (fgets(*ptr, 100, stream) == NULL)
return -1;
if (ferror(stream))
return -1;
*n = strnlen(*ptr, 100);
return *n;
}
#endif
#if __has_include(<unistd.h>)
#include <sys/time.h>
#define POSIX
#endif
#ifndef uint
#define uint uint32_t
#endif
extern char *optarg;
static bool color_256 = true;
static bool color_bold = false;
static bool each_char = true;
static bool rotate_color = true; // inside chars only works if each char is chosen
static bool random_start_color = false;
static uint color_count = 50; // N hues
static bool start_color_set = false; // Starting hue
static uint start_count = 0; // Starting hue
static uint shift_c = 1;
static void clear(int sig);
static uint hue_to_ansiNum(double hue);
static void Scolor(uint color);
static void color(uint color);
static void pchar(const char *str, uint start);
static void help(int code);
static double dmod(double x, double y);
/* Random start*/
static void init_rand(void);
static int c_rand(void);
static long long time_millis(void);
static FILE *Fdat;
#ifdef POSIX
static long long time_millis(void)
{
struct timeval te;
gettimeofday(&te, NULL);
return te.tv_sec * 1000LL + te.tv_usec / 1000;
}
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32)
static long long time_millis(void)
{
SYSTEMTIME time;
GetSystemTime(&time);
return (time.wSecond * 1000LL) + time.wMilliseconds;
}
#else
static long long time_millis(void)
{
return time(NULL);
}
#endif
static int c_rand(void)
{
if (!Fdat)
{
srand(time_millis() + rand());
return rand();
}
else
{
int rand_data;
fread(&rand_data, 1, sizeof(int), Fdat);
return rand_data;
}
}
static void init_rand(void)
{
Fdat = fopen("/dev/urandom", "r");
srand(time_millis());
}
/* Random end */
double dmod(double x, double y)
{
return x - (int)(x / y) * y;
}
static void clear(int sig)
{
puts("\033[0m");
fclose(Fdat);
exit(sig);
}
static void strip_ansi(char * str)
{
const size_t len = strlen(str);
const char chk[] = "0123456789:;<=>?";
size_t count;
while(1)
{
str = strstr(str,"\033[");
if (!str)
return;
count = strspn(str + 2, chk);
if(count == 0 || str[count + 2] != 'm') {
str++;
continue;
}
memcpy(str, str + count + 3, len - count);
}
}
static size_t get_ansi(const char *str)
{
size_t i = 1;
while(*str)
{
if(*str == '[') {
str++;
i++;
continue;
}
if(*str >= '@' && *str <= '~')
break;
i++;
str++;
}
return i;
}
static uint hue_to_ansiNum(double hue)
{
const double sat = 1.0;
const double gam = 0.99;
double p, q, t, ff;
double r, g, b;
long i;
hue = dmod(hue, 360.0);
hue /= 60.0;
i = (long)hue;
ff = hue - i;
p = gam * (1.0 - sat);
q = gam * (1.0 - (sat * ff));
t = gam * (1.0 - (sat * (1.0 - ff)));
switch (i)
{
case 0:
r = gam;
g = t;
b = p;
break;
case 1:
r = q;
g = gam;
b = p;
break;
case 2:
r = p;
g = gam;
b = t;
break;
case 3:
r = p;
g = q;
b = gam;
break;
case 4:
r = t;
g = p;
b = gam;
break;
case 5:
default:
r = gam;
g = p;
b = q;
break;
}
r *= 6;
g *= 6;
b *= 6;
return 16 + (36 * (int)r) + (6 * (int)g) + (int)b;
}
static void Scolor(uint color)
{
printf("\033[38;5;%um", color);
}
static void color(uint color)
{
if (color_256)
Scolor(hue_to_ansiNum(((color * 360.0) / (double)color_count) + start_count));
else
{
color %= 13;
color++;
if (color > 7)
printf("\033[%s%um", (color_bold)? "1;3" : "9", color - 7);
else
printf("\033[3%um", color);
}
}
static void pchar(const char *str, uint start)
{
uint curent_color = start;
int i = 0;
while (*str)
{
while(*str == '\033')
{
i = get_ansi(str);
printf("%.*s", i, str);
str += i;
if (!*str)
return;
}
if (!rotate_color)
curent_color = c_rand() % color_count;
color(curent_color);
printf("%c", *str);
str++;
curent_color++;
if (curent_color >= color_count)
curent_color = 0;
}
}
static void puts_color(char *line) {
static uint curent_color = 0;
if(!start_color_set) {
curent_color = c_rand() % color_count;
start_color_set = true;
}
strip_ansi(line);
if (random_start_color)
curent_color = c_rand() % color_count;
color(curent_color);
if (each_char)
pchar(line, curent_color);
else
printf("%s", line);
curent_color += shift_c;
if (curent_color <= 0)
curent_color = color_count - 1;
if (curent_color >= color_count)
curent_color = 0;
}
static void help(int code)
{
puts_color("+------------------------------------------------+\n");
puts_color("| -h - This message. |\n");
puts_color("| -C - Only change whole line color. |\n");
puts_color("| -r - Random line color. |\n");
puts_color("| -R - All random color. |\n");
puts_color("| -5 - Dissable 8-bit mode. |\n");
puts_color("| -B - Use bold as bright color. |\n");
puts_color("| -s - Starting color in hue 360*. (phase) |\n");
puts_color("| -f - Total shades to use. (frequency) |\n");
puts_color("| -a - Color shift amount for new lines. (angle) |\n");
puts_color("+------------------------------------------------+\n");
exit(code);
}
int main(int argc, char **argv)
{
const char var_opts[] = "asf";
FILE *fp = stdin;
int opt;
char *line = NULL;
size_t i, len = 0;
ssize_t lineSize = 0;
char opts[] = "-a";
bool file = true;
signal(SIGINT, clear);
signal(SIGTERM, clear);
signal(SIGABRT, clear);
while ((opt = getopt(argc, argv, "CrRh5Ba:s:f:")) != -1)
{
switch (opt)
{
case 'C':
each_char = false;
break;
case 'r':
random_start_color = true;
break;
case 'R':
rotate_color = false, random_start_color = true;
break;
case '5':
color_256 = false;
break;
case 'B':
color_bold = true;
break;
case 'h':
help(0);
break;
case 'f':
{
errno = 0;
color_count = strtol(optarg, NULL, 10);
if (errno)
{
printf("invalid value for -%c", opt);
help(EXIT_FAILURE);
}
break;
}
case 's':
{
errno = 0;
start_count = strtol(optarg, NULL, 10);
if (errno)
{
printf("invalid value for -%c", opt);
help(EXIT_FAILURE);
}
start_color_set = true;
break;
}
case 'a':
{
errno = 0;
shift_c = strtol(optarg, NULL, 10);
if (errno)
{
printf("invalid value for -%c", opt);
help(EXIT_FAILURE);
}
break;
}
default:
help(EXIT_FAILURE);
}
}
if (*argv[argc - 1] != '-' && argc > 1)
{
for (i = 0; i < sizeof(var_opts) - 1; i++)
{
opts[1] = var_opts[i];
if (!strcmp(argv[argc - 2], opts))
file = false;
}
if (file)
{
fp = fopen(argv[argc - 1], "r");
if (!fp)
{
fprintf(stderr, "Error opening file '%s'\n", argv[argc - 1]);
return EXIT_FAILURE;
}
}
}
init_rand();
while ((lineSize = getline(&line, &len, fp)) != -1)
puts_color(line);
free(line);
clear(0);
fclose(Fdat);
return 0;
}