summaryrefslogtreecommitdiff
path: root/colorlookupgen.c
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2012-09-27 22:29:36 +0200
committerPeter Wu <lekensteyn@gmail.com>2012-09-27 22:29:36 +0200
commit2c04b05d548987c39c8c8ba29cd7dd9ab101f093 (patch)
tree5c2e44771858805542a64c43f3d91343399a20f2 /colorlookupgen.c
parent25d7e38a339d60aa532ebd4fcfc9e710186e834a (diff)
downloadc-files-2c04b05d548987c39c8c8ba29cd7dd9ab101f093.tar.gz
xcbviewfs,colorlookupgen: implement XPM loading
Diffstat (limited to 'colorlookupgen.c')
-rw-r--r--colorlookupgen.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/colorlookupgen.c b/colorlookupgen.c
new file mode 100644
index 0000000..6770703
--- /dev/null
+++ b/colorlookupgen.c
@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <stdint.h>
+
+int main(int argc, char **argv) {
+ FILE *file = stdin;
+ char *funcname = "color_lookup";
+ char line[256];
+ char color[256];
+ unsigned int total_colors = 0;
+
+ if (argc >= 2) {
+ file = fopen(argv[1], "r");
+ if (!file) {
+ perror("Error opening rgb.txt");
+ return 1;
+ }
+ }
+
+ if (argc >= 3) {
+ funcname = argv[1];
+ }
+
+ printf("#include <string.h>\n");
+ printf("int %s(char *name, unsigned char color[3]) {\n", funcname);
+ printf(
+ " static struct {\n"
+ " char *name;\n"
+ " unsigned char color[3];\n"
+ " } colors[] = {\n");
+ while (fgets(line, sizeof(line), file) != NULL) {
+ uint8_t r, g, b;
+ int matches = sscanf(line, "%hhd %hhd %hhd %255[^\n]",
+ &r, &g, &b, color);
+ if (matches == 4) {
+ ++total_colors;
+ printf(" { \"%s\", { %d, %d, %d } },\n", color, r, g, b);
+ }
+ }
+ printf(
+ " };\n"
+ " int i = 0;\n"
+ " for (; i<%d; ++i)\n"
+ " if (!strcasecmp(colors[i].name, name)) {\n"
+ " memcpy(color, colors[i].color, 3);\n"
+ " return 1;\n"
+ " }\n"
+ " return 0;\n"
+ "}\n", total_colors);
+
+ return 0;
+}