shithub: picker

Download patch

ref: 29e9efba2eca61281097da7462fa4fd94ef34b35
parent: 35d6448581414f216d1295437ad3a911ea6699e4
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sat Mar 14 20:04:45 EDT 2020

a few fixes and a "snarf all" menu option

--- a/README.md
+++ b/README.md
@@ -1,4 +1,17 @@
 # picker
 
-HSLuv color picker for Plan 9.
-It's WIP so the code look horrible and doesn't make any sense right now.
+Color picker for Plan 9.  It has HSLuv, HPLuv, and RGB color space
+modes and is intended to be used mainly by external programs that need
+a way to change their color palette.
+
+## Installing
+
+```
+cd /tmp && \
+hget https://github.com/ftrvxmtrx/picker/archive/master.tar.gz | tar xz && \
+cd picker-master && mk install
+```
+
+## Documentation
+
+See the man page.
--- a/picker.c
+++ b/picker.c
@@ -109,8 +109,9 @@
 	},
 };
 
-static char *menu2i[nelem(spaces)+2] = {
+static char *menu2i[nelem(spaces)+3] = {
 	"snarf",
+	"snarf all",
 };
 static Menu menu2 = { .item = menu2i };
 
@@ -184,6 +185,12 @@
 	return s;
 }
 
+static int
+color2str(Color *c, char *s)
+{
+	return nchan > 3 ? sprint(s, "%08lux", c->u) : sprint(s, "%06lux", c->u>>8);
+}
+
 static void
 redraw(void)
 {
@@ -242,10 +249,7 @@
 	draw(screen, r, colors[curcolor].i, nil, ZP);
 
 	/* current color in hex */
-	if (nchan > 3)
-		sprint(hex, "%08lux", colors[curcolor].u);
-	else
-		sprint(hex, "%06lux", colors[curcolor].u>>8);
+	color2str(&colors[curcolor], hex);
 	r.min.x += Dx(r)/2 - stringwidth(font, hex)/2;
 	r.min.y += Dy(r)/2 - font->height/2;
 	stringbg(screen, r.min, display->white, ZP, font, hex, display->black, ZP);
@@ -351,6 +355,7 @@
 	Color *c;
 	int i, once;
 	ulong u;
+	char buf[16];
 
 	space = &spaces[0];
 	nchan = 3;
@@ -391,7 +396,7 @@
 	}
 
 	for (i = 0; i < nelem(spaces); i++)
-		menu2i[i+1] = spaces[i].name;
+		menu2i[i+2] = spaces[i].name;
 
 	if (initdraw(nil, nil, "picker") < 0)
 		sysfatal("initdraw: %r");
@@ -461,26 +466,33 @@
 						goto next;
 					}
 				}
-			} else if (m.buttons == 2) {
-				if ((i = menuhit(2, mctl, &menu2, nil)) >= 0) {
+			} else if (m.buttons == 2 && (i = menuhit(2, mctl, &menu2, nil)) >= 0) {
+				if (i >= 2) {
+					space = &spaces[i-2];
+					space->fromrgb(c->rgba, c->v);
+					for (i = 0; i < 3; i++)
+						c->v[i] = MAX(0.0, MIN(space->max[i], c->v[i]));
+					goto changed;
+				}
+
+				int f;
+				if ((f = open("/dev/snarf", OWRITE)) >= 0) {
 					if (i == 0) {
-						int f;
-						if ((f = open("/dev/snarf", OWRITE)) >= 0) {
-							write(f, hex, strlen(hex));
-							close(f);
-						}
+						write(f, hex, strlen(hex));
 					} else {
-						space = &spaces[i];
-						space->fromrgb(c->rgba, c->v);
-						for (i = 0; i < 3; i++)
-							c->v[i] = MAX(0.0, MIN(space->max[i], c->v[i]));
-						goto changed;
+						for (i = 0; i < ncolors; i++) {
+							write(f, buf, color2str(&colors[i], buf));
+							if (i != ncolors-1)
+								write(f, " ", 1);
+						}
 					}
+					close(f);
 				}
 			} else if (m.buttons == 4) {
-				if (enter(nchan < 4 ? "rgb:" : "rgba:", hex+1, sizeof(hex)-1, mctl, kctl, nil) > 0) {
+				strcpy(buf, hex);
+				if (enter(nchan < 4 ? "rgb:" : "rgba:", buf, sizeof(buf), mctl, kctl, nil) > 0) {
 					u = c->u;
-					if (str2color(hex+1, c) == 0 && c->u != u) {
+					if (str2color(buf, c) == 0 && c->u != u) {
 						c->u = ~c->u; /* just for the update to kick in */
 						goto changed;
 					}