colors

extract colors from pictures
git clone git://git.2f30.org/colors
Log | Files | Refs | README | LICENSE

commit 94f621d9eed6b057281df9ec2ef927b80f01a42e
parent 95c32852f2c9c922084ad75a0b05b6fecd226fbe
Author: lostd <lostd@2f30.org>
Date:   Fri,  5 Jun 2015 16:22:25 +0300

Basic hue clustering, only the 6 basic hues supported for now

Diffstat:
Mcolors.1 | 3+++
Mcolors.c | 29++++++++++++++++++++++++++++-
2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/colors.1 b/colors.1 @@ -8,6 +8,7 @@ .Nm colors .Op Fl e .Op Fl r +.Op Fl h .Op Fl n Ar clusters .Ar file .Sh DESCRIPTION @@ -20,6 +21,8 @@ from pictures. Print empty clusters as well. .It Fl r Select initial clusters randomly. +.It Fl h +Select initial clusters from the hue domain. .It Fl n Ar clusters .El Set the number of clusters. It defaults to 8. diff --git a/colors.c b/colors.c @@ -11,6 +11,8 @@ #include "colors.h" #include "queue.h" +#define LEN(x) (sizeof (x) / sizeof *(x)) + struct point { int x, y, z; struct cluster *c; @@ -30,6 +32,7 @@ TAILQ_HEAD(points, point) points; size_t npoints; int eflag; int rflag; +int hflag; int distance(struct point *p1, struct point *p2) @@ -97,6 +100,23 @@ initcluster_rand(struct cluster *c, int unused) c->center = *p; } +struct point huetab[] = { + { .x = 0xff, .y = 0x00, .z = 0x00 }, /* red */ + { .x = 0xff, .y = 0x00, .z = 0xff }, /* purple */ + { .x = 0x00, .y = 0x00, .z = 0xff }, /* blue */ + { .x = 0x00, .y = 0xff, .z = 0xff }, /* cyan */ + { .x = 0x00, .y = 0xff, .z = 0x00 }, /* green */ + { .x = 0xff, .y = 0xff, .z = 0x00 }, /* yellow */ +}; + +void +initcluster_hue(struct cluster *c, int i) +{ + TAILQ_INIT(&c->members); + c->nmembers = 0; + c->center = huetab[i]; +} + void (*initcluster)(struct cluster *c, int i) = initcluster_fixed; void @@ -208,7 +228,7 @@ printclusters(void) void usage(void) { - fprintf(stderr, "usage: %s [-er] [-n clusters] file\n", argv0); + fprintf(stderr, "usage: %s [-erh] [-n clusters] file\n", argv0); exit(1); } @@ -224,6 +244,9 @@ main(int argc, char *argv[]) case 'r': rflag = 1; break; + case 'h': + hflag = 1; + break; case 'n': errno = 0; nclusters = strtol(EARGF(usage()), &e, 10); @@ -241,6 +264,10 @@ main(int argc, char *argv[]) srand(time(NULL)); initcluster = initcluster_rand; } + if (hflag) { + nclusters = LEN(huetab); + initcluster = initcluster_hue; + } TAILQ_INIT(&points); parseimg(argv[0], fillpoints); initclusters(clusters, nclusters);