shithub: picker

Download patch

ref: 72a65f4596a41a569ef02852c9ab638d6329a481
parent: ffb97abccf0d8a01cf7db41c495103585d810699
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Aug 10 05:58:05 EDT 2020

add "revert" and "revert all" to menu

--- a/picker.c
+++ b/picker.c
@@ -33,6 +33,7 @@
 	Image *i;
 	double v[4];
 	double rgba[4];
+	double rgba0[4]; /* that's for reverting back to the original */
 	Rectangle r;
 	u32int u;
 	int nchan;
@@ -115,7 +116,7 @@
 	},
 };
 
-static char *menu2i[nelem(spaces)+4];
+static char *menu2i[nelem(spaces)+6];
 static Menu menu2 = { .item = menu2i };
 
 static Color *colors, *color, *last;
@@ -409,6 +410,7 @@
 				free(c->id);
 				new->prev = c->prev;
 				new->next = c->next;
+				memmove(new->rgba0, c->rgba0, sizeof(new->rgba0));
 				memmove(c, new, sizeof(*c));
 				break;
 			}
@@ -416,6 +418,7 @@
 
 		if (c == nil) {
 			c = new;
+			memmove(c->rgba0, c->rgba, sizeof(c->rgba));
 			if (last != nil)
 				last->next = new;
 			new->prev = last;
@@ -487,7 +490,7 @@
 		[Cloaded] = { nil, nil, CHANRCV },
 		{ nil, nil, CHANEND },
 	};
-	int i, oldbuttons, slider;
+	int i, oldbuttons, slider, menusnarf, menusnarfall, menurevert, menurevertall, menuexit;
 	ulong u;
 	char buf[16];
 
@@ -523,9 +526,11 @@
 
 	for (i = 0; i < nelem(spaces); i++)
 		menu2i[i] = spaces[i].name;
-	menu2i[i++] = "snarf";
-	menu2i[i++] = "snarf all";
-	menu2i[i] = "exit";
+	menu2i[menusnarf = i++] = "snarf";
+	menu2i[menusnarfall = i++] = "snarf all";
+	menu2i[menurevert = i++] = "revert";
+	menu2i[menurevertall = i++] = "revert all";
+	menu2i[menuexit = i] = "exit";
 
 	if (initdraw(nil, nil, "picker") < 0)
 		sysfatal("initdraw: %r");
@@ -610,7 +615,7 @@
 					}
 				}
 			} else if (m.buttons == 4 && (i = menuhit(3, mctl, &menu2, nil)) >= 0) {
-				if (i == nelem(spaces)+2)
+				if (i == menuexit)
 					goto end;
 				if (i < nelem(spaces)) {
 					space = &spaces[i];
@@ -618,15 +623,34 @@
 					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 == nelem(spaces))
-						write(f, hex, strlen(hex));
-					else
-						dump(f);
-					close(f);
+				} else if (i == menusnarf || i == menusnarfall) {
+					int f;
+					if ((f = open("/dev/snarf", OWRITE)) >= 0) {
+						if (i == menusnarf)
+							write(f, hex, strlen(hex));
+						else
+							dump(f);
+						close(f);
+					}
+				} else if (i == menurevert) {
+					space->fromrgb(c->rgba0, c->v);
+					goto changed;
+				} else if (i == menurevertall) {
+					for (c = colors; c != nil; c = c->next) {
+						memmove(c->rgba, c->rgba0, sizeof(c->rgba));
+						space->fromrgb(c->rgba, c->v);
+						space->torgb(c->v, c->rgba);
+						u = rgba2u(c->rgba);
+						if (c->u != u) {
+							c->u = u;
+							if (!once)
+								printcolor(1, c);
+							freeimage(c->i);
+							c->i = nil;
+						}
+					}
+					redraw();
+					break;
 				}
 			} else if (m.buttons == 2) {
 				strcpy(buf, hex);