]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Add tools/hex/hex-color-average.c
authorJohan Malm <jgm323@gmail.com>
Tue, 21 Jul 2020 20:07:47 +0000 (21:07 +0100)
committerJohan Malm <jgm323@gmail.com>
Tue, 21 Jul 2020 20:07:47 +0000 (21:07 +0100)
tools/hex/.gitignore [new file with mode: 0644]
tools/hex/Makefile [new file with mode: 0644]
tools/hex/hex-color-average.c [new file with mode: 0644]

diff --git a/tools/hex/.gitignore b/tools/hex/.gitignore
new file mode 100644 (file)
index 0000000..ccf9afa
--- /dev/null
@@ -0,0 +1,2 @@
+hex-color-average
+hex-color-average.o
diff --git a/tools/hex/Makefile b/tools/hex/Makefile
new file mode 100644 (file)
index 0000000..e62fee0
--- /dev/null
@@ -0,0 +1,4 @@
+hex-color-average: hex-color-average.o
+
+clean:
+       rm -f hex-color-average
diff --git a/tools/hex/hex-color-average.c b/tools/hex/hex-color-average.c
new file mode 100644 (file)
index 0000000..f615040
--- /dev/null
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int hexval(int c)
+{
+       int ret = -1;
+       switch (c) {
+       case '0'...'9':
+               ret = c - '0';
+               break;
+       case 'a'...'f':
+               ret = c - 'a' + 10;
+               break;
+       case 'A'...'F':
+               ret = c - 'A' + 10;
+               break;
+       }
+       return ret;
+}
+
+int hex2dec(const char *hexstring)
+{
+       int value = 0, pos = 0, hex;
+       while ((hex = hexval(hexstring[pos++])) != -1)
+               value = (value << 4) + hex;
+       return value;
+}
+
+void usage(const char *command)
+{
+       printf("Usage: %s <rrggbb> <rrggbb>\n", command);
+       exit(1);
+}
+int main(int argc, char **argv)
+{
+       double col[6] = { 0 };
+
+       if (argc < 3)
+               usage(argv[0]);
+
+       for (int j = 1; j < argc; j++) {
+               int len = strlen(argv[j]);
+               for (int i = 0; i < len / 2; i++) {
+                       char buf[3] = { 0 };
+                       buf[0] = argv[j][i * 2];
+                       buf[1] = argv[j][i * 2 + 1];
+                       col[(j - 1) * 3 + i] = hex2dec(buf) / 255.0;
+               }
+       }
+       printf("[%s] { %.2f, %.2f, %.2f }\n", argv[1], col[0], col[1], col[2]);
+       printf("[%s] { %.2f, %.2f, %.2f }\n", argv[2], col[3], col[4], col[5]);
+       printf("[ mean ] { %.2f, %.2f, %.2f }\n",
+              (col[0] + col[3]) / 2.0,
+              (col[1] + col[4]) / 2.0,
+              (col[2] + col[5]) / 2.0);
+}
+