aboutsummaryrefslogtreecommitdiffstats
path: root/conv-colour.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-06-19 22:30:19 +0200
committerMattias Andrée <maandree@kth.se>2017-06-19 22:30:19 +0200
commitf6ed0b0a6bf2e207b5755913a9749d8af6b061f2 (patch)
treed77da5f4f11ed8d76b9a7859163f32821624f2ee /conv-colour.c
parentm (diff)
downloadlibcolour-f6ed0b0a6bf2e207b5755913a9749d8af6b061f2.tar.gz
libcolour-f6ed0b0a6bf2e207b5755913a9749d8af6b061f2.tar.bz2
libcolour-f6ed0b0a6bf2e207b5755913a9749d8af6b061f2.tar.xz
Add a utility (not installed) that converts colours
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--conv-colour.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/conv-colour.c b/conv-colour.c
new file mode 100644
index 0000000..d1356db
--- /dev/null
+++ b/conv-colour.c
@@ -0,0 +1,127 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+#include "libcolour.h"
+
+
+#define TYPE_COLOUR libcolour_colour_llf_t
+#define TYPE_PRINTF ".10Lf"
+#define TYPE_STRTOD strtold
+
+
+const char *argv0;
+
+
+static void
+get_colour_space(char ***argvp, TYPE_COLOUR *cs)
+{
+#define argv (*argvp)
+
+ if (!strcasecmp(*argv, "srgb")) {
+ argv++;
+ cs->model = LIBCOLOUR_SRGB;
+ if (!*argv || !**argv) exit(2);
+ cs->srgb.with_transfer = !!strchr("yYtT", **argv);
+ argv++;
+ } else if (!strcasecmp(*argv, "xyy") || !strcasecmp(*argv, "ciexyy")) {
+ argv++;
+ cs->model = LIBCOLOUR_CIEXYY;
+ } else if (!strcasecmp(*argv, "xyz") || !strcasecmp(*argv, "ciexyz")) {
+ argv++;
+ cs->model = LIBCOLOUR_CIEXYZ;
+ } else if (!strcasecmp(*argv, "lab") || !strcasecmp(*argv, "cielab")) {
+ argv++;
+ cs->model = LIBCOLOUR_CIELAB;
+ } else if (!strcasecmp(*argv, "yiq")) {
+ argv++;
+ cs->model = LIBCOLOUR_YIQ;
+ } else if (!strcasecmp(*argv, "ydbdr")) {
+ argv++;
+ cs->model = LIBCOLOUR_YDBDR;
+ } else if (!strcasecmp(*argv, "yuv")) {
+ argv++;
+ cs->model = LIBCOLOUR_YUV;
+ } else if (!strcasecmp(*argv, "ypbpr")) {
+ argv++;
+ cs->model = LIBCOLOUR_YPBPR;
+ } else if (!strcasecmp(*argv, "ycgco")) {
+ argv++;
+ cs->model = LIBCOLOUR_YCGCO;
+ } else if (!strcasecmp(*argv, "cie1960ucs") || !strcasecmp(*argv, "cieucs") || !strcasecmp(*argv, "ucs")) {
+ argv++;
+ cs->model = LIBCOLOUR_CIE1960UCS;
+ } else if (!strcasecmp(*argv, "cieuvw") || !strcasecmp(*argv, "uvw")) {
+ argv++;
+ cs->model = LIBCOLOUR_CIEUVW;
+ if (!*argv) exit(2);
+ cs->cieuvw.u0 = TYPE_STRTOD(*argv++, NULL);
+ if (!*argv) exit(2);
+ cs->cieuvw.v0 = TYPE_STRTOD(*argv++, NULL);
+ } else if (!strcasecmp(*argv, "cieluv") || !strcasecmp(*argv, "luv")) {
+ argv++;
+ cs->model = LIBCOLOUR_CIELUV;
+ cs->cieluv.white.model = LIBCOLOUR_CIEXYZ;
+ if (!*argv) exit(2);
+ cs->cieluv.white.X = TYPE_STRTOD(*argv++, NULL);
+ if (!*argv) exit(2);
+ cs->cieluv.white.Y = TYPE_STRTOD(*argv++, NULL);
+ if (!*argv) exit(2);
+ cs->cieluv.white.Z = TYPE_STRTOD(*argv++, NULL);
+ } else if (!strcasecmp(*argv, "cielchuv") || !strcasecmp(*argv, "lchuv")) {
+ argv++;
+ cs->model = LIBCOLOUR_CIELCHUV;
+ cs->cielchuv.white.model = LIBCOLOUR_CIEXYZ;
+ if (!*argv) exit(2);
+ cs->cielchuv.white.X = TYPE_STRTOD(*argv++, NULL);
+ if (!*argv) exit(2);
+ cs->cielchuv.white.Y = TYPE_STRTOD(*argv++, NULL);
+ if (!*argv) exit(2);
+ cs->cielchuv.white.Z = TYPE_STRTOD(*argv++, NULL);
+ if (!*argv) exit(2);
+ cs->cielchuv.one_revolution = TYPE_STRTOD(*argv++, NULL);
+ } else if (!strcasecmp(*argv, "yes")) {
+ argv++;
+ cs->model = LIBCOLOUR_YES;
+ }
+
+#undef argv
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ TYPE_COLOUR from, to;
+
+ if (!*argv) return 2;
+ argv0 = *argv++;
+
+ if (!*argv) return 2;
+ get_colour_space(&argv, &from);
+
+ if (!*argv) return 2;
+ from.srgb.R = TYPE_STRTOD(*argv++, NULL);
+ if (!*argv) return 2;
+ from.srgb.G = TYPE_STRTOD(*argv++, NULL);
+ if (!*argv) return 2;
+ from.srgb.B = TYPE_STRTOD(*argv++, NULL);
+
+ if (!*argv) return 2;
+ get_colour_space(&argv, &to);
+
+ if (*argv) return 2;
+
+ if (libcolour_convert(&from, &to)) return 1;
+
+ printf("%"TYPE_PRINTF" %"TYPE_PRINTF" %"TYPE_PRINTF"\n", to.srgb.R, to.srgb.G, to.srgb.B);
+
+ fflush(stdout);
+ if (ferror(stdout))
+ return 2;
+ return 0;
+
+ (void) argc;
+}