From bfacf5a78ddf09baa32c8d6c23d2dfb89d59c15d Mon Sep 17 00:00:00 2001 From: rodri Date: Wed, 1 Jul 2020 21:44:44 +0000 Subject: add better brush color management. fix issues with procrfork. replace procexecl with execl. fclamp is now part of libgeometry so get rid of it. --- color.c | 24 ++++++++++++++++++++++++ dat.h | 7 +++++++ fns.h | 5 ++++- main.c | 17 +++++++++-------- mkfile | 1 + utils.c | 6 ------ 6 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 color.c diff --git a/color.c b/color.c new file mode 100644 index 0000000..ce69fe6 --- /dev/null +++ b/color.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include "dat.h" +#include "fns.h" + +Color* +newcolor(ulong col) +{ + Color *c; + + c = emalloc(sizeof(Color)); + c->v = col; + c->i = eallocimage(display, Rect(0,0,1,1), screen->chan, 1, col); + return c; +} + +void +rmcolor(Color *c) +{ + freeimage(c->i); + free(c); +} diff --git a/dat.h b/dat.h index f08a329..61e000f 100644 --- a/dat.h +++ b/dat.h @@ -9,11 +9,18 @@ enum { MAXZOOM = 8 }; +typedef struct Color Color; typedef struct Layer Layer; typedef struct Canvas Canvas; typedef struct HUD HUD; typedef struct HUDWidget HUDWidget; +struct Color +{ + ulong v; + Image *i; +}; + struct Layer { RFrame; diff --git a/fns.h b/fns.h index 5152daa..01e7f4b 100644 --- a/fns.h +++ b/fns.h @@ -13,7 +13,10 @@ Layer *getlayer(Canvas*, char*); Layer *newlayer(char*, Rectangle, ulong); void rmlayer(Layer*); +/* color */ +Color *newcolor(ulong); +void rmcolor(Color*); + /* utils */ int clamp(int, int, int); -double fclamp(double, double, double); int alphachan(ulong); diff --git a/main.c b/main.c index 647c3b0..c5f06a6 100644 --- a/main.c +++ b/main.c @@ -16,7 +16,7 @@ enum { RFrame worldrf; Image *background; Canvas *curcanvas; -Image *brushcolor; +Color *brushcolor; Image *pal[NCOLOR]; int zoom = 1; @@ -235,7 +235,7 @@ pickerproc(void *arg) snprint(mntdesc, sizeof mntdesc, "-pid %d -dx %d -dy %d", getpid(), 384, 320); newwindow(mntdesc); - procexecl(nil, "/bin/picker", "picker", "-e", nil); + execl("/bin/picker", "picker", "-e", nil); threadexits("procexecl: %r"); } @@ -323,9 +323,9 @@ rmb(Mousectl *mc, Keyboardctl *kc) case SETCOLOR: if(pipe(pfd) < 0) sysfatal("pipe: %r"); - procrfork(pickerproc, pfd, 4096, RFFDG|RFNAMEG); + procrfork(pickerproc, pfd, 4096, RFFDG|RFNAMEG|RFNOTEG); close(pfd[0]); - fprint(pfd[1], "0 %08ux\n", 0x000000ff); + fprint(pfd[1], "0 %08ulx\n", brushcolor->v); n = read(pfd[1], buf, sizeof(buf)-1); close(pfd[1]); if(n < 0) @@ -334,7 +334,8 @@ rmb(Mousectl *mc, Keyboardctl *kc) s = buf; while(*s && *s++ != '\t') ; - brushcolor = eallocimage(display, Rect(0,0,1,1), screen->chan, 1, strtoul(s, nil, 16)); + rmcolor(brushcolor); + brushcolor = newcolor(strtoul(s, nil, 16)); break; case SAVE: if(curcanvas == nil) @@ -401,7 +402,7 @@ lmb(Mousectl *mc, Keyboardctl *) mpos = rframexform(mpos, *curcanvas); p = Pt(mpos.x,mpos.y); - draw(curcanvas->curlayer->image, rectaddpt(r, p), brushcolor, nil, ZP); + draw(curcanvas->curlayer->image, rectaddpt(r, p), brushcolor->i, nil, ZP); redraw(); for(;;){ oldp = p; @@ -415,7 +416,7 @@ lmb(Mousectl *mc, Keyboardctl *) p = Pt(mpos.x,mpos.y); if(eqpt(p, oldp)) continue; - line(curcanvas->curlayer->image, oldp, p, Endsquare, Endsquare, 0, brushcolor, ZP); + line(curcanvas->curlayer->image, oldp, p, Endsquare, Endsquare, 0, brushcolor->i, ZP); redraw(); } } @@ -499,7 +500,7 @@ threadmain(int argc, char *argv[]) worldrf.by = Vec2(0,1); initpalette(); background = mkcheckerboard(4, 4); - brushcolor = pal[PCBlack]; + brushcolor = newcolor(DBlack); display->locking = 1; unlockdisplay(display); diff --git a/mkfile b/mkfile index 4072486..791d30d 100644 --- a/mkfile +++ b/mkfile @@ -6,6 +6,7 @@ OFILES=\ utils.$O\ hud.$O\ alloc.$O\ + color.$O\ layer.$O\ canvas.$O\ main.$O\ diff --git a/utils.c b/utils.c index 4813da5..e3b5737 100644 --- a/utils.c +++ b/utils.c @@ -8,12 +8,6 @@ clamp(int n, int min, int max) return n < min? min: n > max? max: n; } -double -fclamp(double n, double min, double max) -{ - return n < min? min: n > max? max: n; -} - int alphachan(ulong chan) { -- cgit v1.2.3