This repository was archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcor.c
234 lines (204 loc) · 5.82 KB
/
cor.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CFG_MAX_LINE_LENGTH BUFSIZ
/* Unsigned char (8 bits) */
typedef unsigned char u8;
typedef enum {
ERROR_FOPEN = 1,
ERROR_INVALID_OPT = 2,
ERROR_LINE_LENGTH = 4,
ERROR_FGETS = 8
} exit_status;
/* Hexadecimal triplet color types.
* The enum constants use the length as values (e.g. "#0f0" -> length 4). */
typedef enum {
HEX_NONE = 0,
HEX_RGB = 4, /* Ex: #0f0 */
HEX_RGBA = 5, /* Ex: #00f7 */
HEX_RRGGBB = 7, /* Ex: #225577 */
HEX_RRGGBBAA = 9 /* Ex: #deadbeef */
} hex_color_type;
/* Red, Green and Blue color */
typedef struct {
u8 r, g, b;
} Rgb;
/* Inserts values into RGB color struct if a hex triplet is
* found in the string. Returns the type of the hex triplet. */
hex_color_type get_hex_color(const char *s, Rgb *clr) {
hex_color_type t = HEX_NONE;
char rgb_str[3][3];
if (!isxdigit((u8)s[0]) || !isxdigit((u8)s[1]) || !isxdigit((u8)s[2]))
return HEX_NONE;
else if (!isalnum((u8)s[3]))
t = HEX_RGB;
else if (!isxdigit((u8)s[3]))
return HEX_NONE;
else if (!isalnum((u8)s[4]))
t = HEX_RGBA;
else if (!isxdigit((u8)s[4]) || !isxdigit((u8)s[5]))
t = HEX_NONE;
else if (!isalnum((u8)s[6]))
t = HEX_RRGGBB;
else if (!isxdigit((u8)s[6]) || !isxdigit((u8)s[7]))
return HEX_NONE;
else if (!isalnum((u8)s[8]))
t = HEX_RRGGBBAA;
else
return HEX_NONE;
if (t == HEX_RGB || t == HEX_RGBA) {
rgb_str[0][0] = s[0];
rgb_str[0][1] = s[0];
rgb_str[0][2] = '\0';
rgb_str[1][0] = s[1];
rgb_str[1][1] = s[1];
rgb_str[1][2] = '\0';
rgb_str[2][0] = s[2];
rgb_str[2][1] = s[2];
rgb_str[2][2] = '\0';
} else if (t == HEX_RRGGBB || t == HEX_RRGGBBAA) {
rgb_str[0][0] = s[0];
rgb_str[0][1] = s[1];
rgb_str[0][2] = '\0';
rgb_str[1][0] = s[2];
rgb_str[1][1] = s[3];
rgb_str[1][2] = '\0';
rgb_str[2][0] = s[4];
rgb_str[2][1] = s[5];
rgb_str[2][2] = '\0';
}
clr->r = strtol(rgb_str[0], NULL, 16);
clr->g = strtol(rgb_str[1], NULL, 16);
clr->b = strtol(rgb_str[2], NULL, 16);
return t;
}
/* Returns whether a RGB color is bright based on HSP
* (https://alienryderflex.com/hsp.html) */
int is_color_bright(Rgb clr) {
return (clr.r * clr.r * .299f) + (clr.g * clr.g * .587f) +
(clr.b * clr.b * .114f) >
127.5f * 127.5f;
}
/* Outputs the hex triplet with a colored background using "true color"
* terminal escape codes. Returns the number of chars outputted. */
int print_hex_color(const char *s, hex_color_type t, Rgb bg) {
if (is_color_bright(bg))
printf("\033[38;2;0;0;0m\033[48;2;%d;%d;%dm", bg.r, bg.g, bg.b);
else
printf("\033[38;2;255;255;255m\033[48;2;%d;%d;%dm", bg.r, bg.g, bg.b);
printf("#%.*s", t - 1, s);
fputs("\033[0m", stdout);
return t;
}
/* Outputs the file contents with colored hex triplets (e.g. #f00 with a
* red background). If 'extract' is non-zero only display the colors
* instead of the whole file. Returns non-zero in case of error/warning. */
exit_status cor(FILE *fp, int extract) {
char line[CFG_MAX_LINE_LENGTH + 2];
Rgb clr;
int i;
hex_color_type t;
exit_status sts = 0;
line[sizeof(line) - 2] = '\0';
clearerr(fp);
while (fgets(line, sizeof(line), fp)) {
if (ferror(fp)) {
perror("cor: fgets");
return sts |= ERROR_FGETS;
}
if (line[sizeof(line) - 2] != '\0') {
fprintf(
stderr,
"cor: WARNING: line is as long or longer than the buffer capacity "
"(%d)\n",
CFG_MAX_LINE_LENGTH);
sts |= ERROR_LINE_LENGTH;
line[sizeof(line) - 2] = '\0';
}
if (line[0] == '#' && (t = get_hex_color(line + 1, &clr))) {
i = print_hex_color(line + 1, t, clr);
if (extract)
putchar('\n');
} else {
if (!extract)
putchar(line[0]);
i = 1;
}
while (line[i]) {
if (!isalnum((u8)line[i - 1]) && line[i] == '#' &&
(t = get_hex_color(line + i + 1, &clr))) {
i += print_hex_color(line + i + 1, t, clr);
if (extract)
putchar('\n');
continue;
}
if (!extract)
putchar(line[i]);
++i;
}
}
return sts;
}
void test_colors(void) {
int i, j, n;
/* 0-9 */
for (n = 0; n < 10; ++n)
printf("\033[%dm %3d\033[m", n, n);
putchar('\n');
/* 10 is default font and 11-19 is alternative font (not widely supported) */
/* 20-108 */
for (i = 2; i < 11; ++i) {
for (j = 0; j < 10; ++j) {
n = 10 * i + j;
if (n > 107)
break;
printf("\033[%dm %3d\033[m", n, n);
}
putchar('\n');
}
puts("\033[38;2;255;100;0mThis is orange if true color is "
"supported.\033[0m");
}
int main(int argc, char **argv) {
FILE *fp;
int i, file_given = 0, use_opts = 1, extract = 0, show_filename = 0;
exit_status sts = 0;
for (i = 1; i < argc; ++i) {
if (use_opts && argv[i][0] == '-') {
if (argv[i][1] == '\0') {
sts |= cor(stdin, extract);
continue;
} else if (argv[i][1] == '-' && argv[i][2] == '\0') {
use_opts = 0;
} else if (strcmp(argv[i] + 2, "extract") == 0) {
extract = 1;
} else if (strcmp(argv[i] + 2, "show-filename") == 0) {
show_filename = 1;
} else if (strcmp(argv[i] + 2, "test") == 0) {
test_colors();
return sts;
} else {
puts("Usage: cor [--extract | --show-filename | --test] [FILE...]");
if (strcmp(argv[i] + 2, "help") != 0)
sts |= ERROR_INVALID_OPT;
return sts;
}
continue;
}
file_given = 1;
if (!(fp = fopen(argv[i], "r"))) {
fputs("cor: ", stderr);
perror(argv[i]);
sts |= ERROR_FOPEN;
continue;
}
if (show_filename)
printf("%s:\n", argv[i]);
sts |= cor(fp, extract);
fclose(fp);
}
if (!file_given)
return cor(stdin, extract);
return sts;
}