shithub: picker

Download patch

ref: b457388fd65edc971034a534c306e269ccf604d7
parent: a08889d01cb49c0b9169181d483d2e2b7d03b195
author: Sigrid Haflínudóttir <[email protected]>
date: Mon Apr 13 08:06:13 EDT 2020

fix entering color value as hex manually

--- a/picker.c
+++ b/picker.c
@@ -324,32 +324,29 @@
 }
 
 static int
-str2color(char *s, Color *c)
+hex2color(char *h, Color *c)
 {
 	vlong v;
-	char *a[3], *e;
 	int i, n;
+	char *e;
 
-	if ((n = tokenize(s, a, nelem(a))) < 2) {
-		werrstr("columns: %d", n);
-		return -1;
-	}
-	n = strlen(a[1]);
+	n = strlen(h);
 	if (n != 6 && n != 8) {
 		werrstr("components: %d", n);
 		return -1;
 	}
 	c->nchan = n == 6 ? 3 : 4;
-	if ((v = strtoll(a[1], &e, 16)) == 0 && (e == a[1] || *e || v < 0 || (n == 6 && v > 0xffffff) || (n == 8 && v > 0xffffffff))) {
-		werrstr("color: '%s'", a[1]);
+	if ((v = strtoll(h, &e, 16)) == 0 &&
+		(e == h || *e || v < 0 || (n == 6 && v > 0xffffff) || (n == 8 && v > 0xffffffff))) {
+		werrstr("color: '%s'", h);
 		return -1;
 	}
-	c->id = strdup(a[0]);
 	if (c->nchan < 4) {
 		v <<= 8;
 		v |= 0xff;
 	}
 	c->u = v;
+
 	for (i = 0; i < 4; i++) {
 		c->rgba[i] = (double)((v>>24)&0xff) / 255.0;
 		v <<= 8;
@@ -360,6 +357,23 @@
 	return 0;
 }
 
+static int
+line2color(char *s, Color *c)
+{
+	char *a[3];
+	int n;
+
+	if ((n = tokenize(s, a, nelem(a))) < 2) {
+		werrstr("columns: %d", n);
+		return -1;
+	}
+	if (hex2color(a[1], c) != 0)
+		return -1;
+	c->id = strdup(a[0]);
+
+	return 0;
+}
+
 static void
 readcolors(Biobuf *b)
 {
@@ -372,7 +386,7 @@
 		if (new == nil)
 			new = calloc(1, sizeof(Color));
 
-		n = str2color(s, new);
+		n = line2color(s, new);
 		free(s);
 
 		if (n != 0) {
@@ -598,9 +612,9 @@
 				}
 			} else if (m.buttons == 2) {
 				strcpy(buf, hex);
-				if (enter(c->nchan < 4 ? "rgb:" : "rgba:", buf, sizeof(buf), mctl, kctl, nil) > 0) {
+				if (enter("rgb(a):", buf, sizeof(buf), mctl, kctl, nil) > 0) {
 					u = c->u;
-					if (str2color(buf, c) == 0 && c->u != u) {
+					if (hex2color(buf, c) == 0 && c->u != u) {
 						c->u = ~c->u; /* just for the update to kick in */
 						goto changed;
 					}