shithub: picker

Download patch

ref: cab743dec18069d730108f8f8295185cd801a968
parent: f213193d7f4df8554d455dc6d9c38dba0352b099
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sat Mar 14 18:39:48 EDT 2020

produce not-so-romantic gradients in rgb mode

--- a/picker.c
+++ b/picker.c
@@ -19,7 +19,7 @@
 	Numchan,
 
 	Offset = 6,
-	Sliderheight = 32,
+	Sliderheight = 48,
 };
 
 typedef struct Color Color;
@@ -36,6 +36,7 @@
 struct Mode {
 	char *name;
 	char opt;
+	char single;
 	void (*torgb)(double *v, double *rgb);
 	void (*fromrgb)(double *rgb, double *v);
 	double max[4];
@@ -68,17 +69,17 @@
 static void
 _torgb(double *v, double *rgb)
 {
-	v[0] = rgb[0];
-	v[1] = rgb[1];
-	v[2] = rgb[2];
+	rgb[0] = v[0];
+	rgb[1] = v[1];
+	rgb[2] = v[2];
 }
 
 static void
 _fromrgb(double *rgb, double *v)
 {
-	rgb[0] = v[0];
-	rgb[1] = v[1];
-	rgb[2] = v[2];
+	v[0] = rgb[0];
+	v[1] = rgb[1];
+	v[2] = rgb[2];
 }
 
 static Mode modes[] = {
@@ -85,6 +86,7 @@
 	{
 		.name = "HSLuv",
 		.opt = 's',
+		.single = 0,
 		.torgb = _hsluv2rgb,
 		.fromrgb = _rgb2hsluv,
 		.max = {360.0, 100.0, 100.0, 1.0},
@@ -92,6 +94,7 @@
 	{
 		.name = "HPLuv",
 		.opt = 'l',
+		.single = 0,
 		.torgb = _hpluv2rgb,
 		.fromrgb = _rgb2hpluv,
 		.max = {360.0, 100.0, 100.0, 1.0},
@@ -99,6 +102,7 @@
 	{
 		.name = "RGB",
 		.opt = 'r',
+		.single = 1,
 		.torgb = _torgb,
 		.fromrgb = _fromrgb,
 		.max = {1.0, 1.0, 1.0, 1.0},
@@ -105,6 +109,9 @@
 	},
 };
 
+static char *menu2i[nelem(modes)+1];
+static Menu menu2 = { .item = menu2i };
+
 static Color *colors;
 static int ncolors, curcolor, nchan;
 static Rectangle srects[3];
@@ -111,12 +118,18 @@
 static Mode *mode;
 static Image *bg;
 
+static ulong
+rgba2u(double *rgba)
+{
+	return D2C(rgba[0])<<24 | D2C(rgba[1])<<16 | D2C(rgba[2])<<8 | D2C(rgba[3]);
+}
+
 static Image *
 slider(int si, int w)
 {
 	static Image *s, *sliders[4];
 	static u8int *b, *buf[4];
-	double c[4], rgb[3], dt;
+	double c[4], rgba[4], dt;
 	Rectangle rect;
 	int i, n, mi;
 	ulong u;
@@ -133,12 +146,18 @@
 	b = buf[si];
 
 	memmove(c, colors[curcolor].v, sizeof(c));
+	if (mode->single) {
+		memset(c, 0, 3*sizeof(double));
+		c[si] = colors[curcolor].v[si];
+	}
 	dt = mode->max[si] / w;
 	mi = c[si] / dt;
 	c[si] = 0.0;
 	for (i = n = 0; i < w; i++, n += 4) {
-		mode->torgb(c, rgb);
-		u = setalpha(D2C(rgb[0])<<24 | D2C(rgb[1])<<16 | D2C(rgb[2])<<8, D2C(c[3]));
+		mode->torgb(c, rgba);
+		rgba[3] = c[3];
+		u = rgba2u(rgba);
+		u = setalpha(u, u&0xff);
 		b[n] = 0xff;
 		if (mi-2 == i)
 			memset(b+n+1, 0, 3);
@@ -337,6 +356,9 @@
 		mode->fromrgb(colors[i].rgba, colors[i].v);
 	}
 
+	for (i = 0; i < nelem(modes); i++)
+		menu2i[i] = modes[i].name;
+
 	if (initdraw(nil, nil, "picker") < 0)
 		sysfatal("initdraw: %r");
 	if ((kctl = initkeyboard(nil)) == nil)
@@ -387,8 +409,10 @@
 						ulong u;
 
 						c->v[i] = MIN(mode->max[i], (double)(m.xy.x - r.min.x) * mode->max[i]/(double)(Dx(r)-1));
+						c->rgba[3] = c->v[3];
+changed:
 						mode->torgb(c->v, c->rgba);
-						u = D2C(c->rgba[0])<<24 | D2C(c->rgba[1])<<16 | D2C(c->rgba[2])<<8 | D2C(c->v[3]);
+						u = rgba2u(c->rgba);
 						if (c->u != u) {
 							c->u = u;
 							if (nchan < 4)
@@ -397,8 +421,8 @@
 								j = sprint(buf, "%d %08lux\n", curcolor, u);
 							if (write(1, buf, j) != j)
 								goto end;
-							redraw();
 						}
+						redraw();
 						goto next;
 					}
 				}
@@ -408,6 +432,14 @@
 						redraw();
 						goto next;
 					}
+				}
+			} else if (m.buttons == 2) {
+				if ((i = menuhit(2, mctl, &menu2, nil)) >= 0 && i < nelem(modes)) {
+					mode = &modes[i];
+					mode->fromrgb(c->rgba, c->v);
+					for (i = 0; i < 3; i++)
+						c->v[i] = MAX(0.0, MIN(mode->max[i], c->v[i]));
+					goto changed;
 				}
 			}
 			break;