shithub: pain

Download patch

ref: aea23b76b347d68d5c75d51065c1982acd23ee07
parent: fe07e6a3a0099fdd447919004e6b65912de30bd8
author: jmq <jmq@jmq.sh>
date: Sat Aug 23 17:46:25 EDT 2025

pain: pill: color integration

--- a/fs.c
+++ b/fs.c
@@ -4,6 +4,7 @@
 #include <thread.h>
 #include <draw.h>
 #include <9p.h>
+#include <stdio.h>
 
 #include "dat.h"
 #include "fns.h"
@@ -15,6 +16,8 @@
 
 static char * canvasread(Req *);
 static char * canvaswrite(Req *);
+static char * colorread(Req *);
+static char * colorwrite(Req *);
 static int FsPID = -1;
 
 typedef struct FileEntry {
@@ -25,7 +28,8 @@
 } FileEntry;
 
 FileEntry entries[] = {
-	{"canvas", canvasread, canvaswrite, 0666 }
+	{"canvas", canvasread, canvaswrite, 0666 },
+	{"color", colorread, colorwrite, 0666 }
 };
 static Image * CanvasReadImage = nil;
 static long CanvasReadReady = 0;
@@ -112,6 +116,8 @@
 		free(b);
 	}
 
+	freeimage(CanvasReadImage);
+
 	return nil;
 }
 
@@ -118,6 +124,56 @@
 static char *
 canvaswrite(Req *)
 {
+	return nil;
+}
+
+static char *
+colorread(Req * r)
+{
+	char b[10];
+	u32int p[4];
+	Image * br;
+	
+	br = namedimage(display, "pain.brush");
+	if (br == nil) {
+		return "failed to get brush image";
+	}
+
+	unloadimage(br, br->r, (uchar*)p, sizeof(p));
+	snprint(b, sizeof(b), "%08ux\n", p[0]);
+	readbuf(r, b, sizeof(b));
+	freeimage(br);
+	
+	return nil;
+}
+
+static char *
+colorwrite(Req *r)
+{
+	u32int c;
+	Image * i, * br;
+	
+	br = namedimage(display, "pain.brush");
+	if (br == nil) {
+		return "failed to get brush image";
+	}
+
+	if(r->ifcall.count == 0) {
+		c = DNofill;
+	} else {
+		sscanf(r->ifcall.data, "%x", &c);
+	}
+
+	i = allocimage(display, br->r, RGBA32, 0, c);
+	if (i == nil) {
+		freeimage(br);
+		return "failed to allocate image";
+	}
+
+	drawop(br, br->r, i, nil, ZP, S);
+	
+	freeimage(i);
+	
 	return nil;
 }
 
--- a/main.c
+++ b/main.c
@@ -323,7 +323,7 @@
 	// Draw on the image layer
 	f = screentocanvaspt(subpt(f, viewAtCanvas.min));
 	t = screentocanvaspt(subpt(t, viewAtCanvas.min));
-	line(l->image, addpt(f, l->offset), addpt(t, l->offset), STROKEEND, STROKEEND, 1, BrushImage, ZP);
+	lineop(l->image, addpt(f, l->offset), addpt(t, l->offset), STROKEEND, STROKEEND, 1, BrushImage, ZP, S);
 
 	// Draw on the zoomed image
 	f = mulptf(f, Zoom);
@@ -406,6 +406,9 @@
 	if (BrushImage == nil)
 		freeimage(BrushImage);
 	BrushImage = i;
+	if (nameimage(BrushImage, "pain.brush", 1) < 0) {
+		sysfatal("nameimage: %r");
+	}
 }
 
 void
@@ -500,6 +503,7 @@
 	applyzoom();
 	setdefaultbindings();
 	centercanvas();
+	setbrushcolor(DBlack);
 
 	einit(Emouse | Ekeyboard);
 	etimer(0, UPDATEPERIOD);
--- a/pill/pill.c
+++ b/pill/pill.c
@@ -19,7 +19,7 @@
 #define COLORPICKERRADIUS .25
 #define INNERBORDERRADIUS	(COLORPICKERPADDING + COLORPICKERRADIUS)
 #define RECTANGLE(w, h) ((Rectangle){(Point){(0),(0)}, (Point){(w),(h)}})
-#define COLORINDICATORSIZE 0.15
+#define COLORINDICATORSIZE 0.1
 #define VALUEINCREASE 0.05
 #define UPDATEDELAY 100
 
@@ -340,10 +340,9 @@
 		sysfatal("allocimage: %r");
 	}
 	
-	r = RECTANGLE( ((float)Dx(screen->r)) * COLORINDICATORSIZE,
-		((float)Dx(screen->r)) * COLORINDICATORSIZE);
-	r.min = screen->r.min;
-	r.max = addpt(r.max, r.min);
+	r.max = r.min = screen->r.max;
+	r.min.x -= ((float)Dx(screen->r)) * COLORINDICATORSIZE;
+	r.min.y -=((float)Dx(screen->r)) * COLORINDICATORSIZE;
 	draw(screen, r, c, nil, ZP);
 	freeimage(c);
 }
@@ -484,13 +483,13 @@
 	if (mouse.buttons & 0x16) {
 		v -= VALUEINCREASE;
 	}
+	if (v > 1.0) {
+		v = 1.0;
+	}
+	if (v < 0.0) {
+		v = 0.0;
+	}
 	if (v != Value) {
-		if (v > 1.0) {
-			v = 1.0;
-		}
-		if (v < 0.0) {
-			v = 0.0;
-		}
 		Value = v;
 		UpdateGradient = 1;
 		ShouldDraw = 1;
--