~amavect/pal

9d90c574cee37b7ca440a75e513019dac3c5b735 — amavect 4 years ago 12f948d master
add stdin color input
2 files changed, 69 insertions(+), 12 deletions(-)

M pal.c
M pal.man
M pal.c => pal.c +68 -11
@@ 52,6 52,34 @@ error(char *s)
	threadexitsall(s);
}

ulong
strtocolor(char *s)
{
	uchar alpha, chan;
	ulong c;
	int i;
	
	if(s[10] != '\0')
		return DNotacolor;
	if(s[0] != '0' || s[1] != 'x')
		return DNotacolor;
	for(i = 2; i < 10; i++){
		if(!(
			s[i] >= '0' && s[i] <= '9' || 
			s[i] >= 'a' && s[i] <= 'f' ||
			s[i] >= 'A' && s[i] <= 'F'))
			return DNotacolor;
	}
	c = strtoul(s, nil, 0);
	alpha = c & 0xFF;
	for(i = 8; i <= 24; i+=8){
		chan = c >> i & 0xFF;
		if(chan > alpha)
			return DNotacolor;
	}
	return c;
}

Image *
eallocimage(Rectangle r, ulong chan, int repl, ulong col)
{


@@ 70,9 98,15 @@ dogetwindow(void)
}

void
fsendcolor(int fd)
{
	fprint(fd, "0x%08ulX\n", colors[selcol]);
}

void
sendcolor(void)
{
	print("0x%08ulX\n", colors[selcol]);
	fsendcolor(1);
	if(!pflag)
		threadexitsall(nil);
}


@@ 171,7 205,9 @@ readpalfile(char *s)
		if(a[10] != '\n')
			error("invalid palette");
		a[10] = 0;
		c = strtoul(a, nil, 0);
		c = strtocolor(a);
		if(c == DNotacolor)
			error("invalid color in palette");
		addcolor(c, ncolors);
	}
}


@@ 193,8 229,9 @@ pickerproc(void *aux)
			threadexitsall(nil);
		}
		s[10] = 0;
		col = strtoul(s, nil, 0);
		send(c, &col);
		col = strtocolor(s);
		if(col != DNotacolor)
			send(c, &col);
	}
}



@@ 286,25 323,35 @@ initkbd(void)
}

void
stdinproc(void *)
stdinproc(void *aux)
{
	char in[11];
	Channel *c;
	char in[11]; /* 0xRRGGBBAA\0 or 0xRRGGBBAA\n */
	ulong col;
	long n;
	
	c = aux;
	threadsetname("stdinproc");
	
	for(;;){
		n = read(0, in, sizeof(in));
		if(n <= 0)
			break;
		in[10] = 0;
		col = strtocolor(in);
		if(col != DNotacolor)
			send(c, &col);
	}
	threadexitsall(nil);
}

void
Channel *
initstdin(void)
{
	proccreate(stdinproc, nil, 0x2000);
	Channel *c;
	c = chancreate(sizeof(ulong), 16);
	proccreate(stdinproc, c, 0x2000);
	return c;
}

void


@@ 324,6 371,7 @@ threadmain(int argc, char **argv)
	ulong tcol;
	Channel *pc;
	Channel *kbdc;
	Channel *stdinc;
	
	ARGBEGIN{
	case 'c':


@@ 357,18 405,19 @@ threadmain(int argc, char **argv)
	draw(tile, Rect(0,0,DX/4,DY/4), display->black, nil, ZP);
	draw(tile, Rect(DX/4,DY/4,DX/2,DY/2), display->black, nil, ZP);
	
	initstdin();
	stdinc = initstdin();
	kbdc = initkbd();
	mctl = initmouse(nil, screen);
	if(mctl == nil)
		error("no mouse");
	
	
	enum{MOUSE, RESIZE, KEYS, PICKER};
	enum{MOUSE, RESIZE, KEYS, STDIN, PICKER};
	Alt alts[] = {
		{mctl->c, &m, CHANRCV},
		{mctl->resizec, nil, CHANRCV},
		{kbdc, &r, CHANRCV},
		{stdinc, &col, CHANRCV},
		{nil, nil, CHANEND},
		{nil, nil, CHANEND}
	};


@@ 395,7 444,7 @@ threadmain(int argc, char **argv)
					if(iflag)
						sendcolor();
					if(cflag)
						fprint(pfd, "0x%08ulX\n", colors[selcol]);
						fsendcolor(pfd);
					redraw();
				}
				if(tcol >= ncolors)


@@ 431,6 480,14 @@ threadmain(int argc, char **argv)
			if(r == Kdel)
				threadexitsall(nil);
			break;
		case STDIN:
			if(ncolors > 0){
				colors[selcol] = col;
				redraw();
				if(cflag)
					fsendcolor(pfd);
			}
			break;
		case PICKER:
			if(ncolors > 0){
				colors[selcol] = col;

M pal.man => pal.man +1 -1
@@ 21,7 21,7 @@ Passing a file with a hex color on each line will initialize
.I pal
with those colors, instead.
.PP
Hex colors from stdin are currently ignored. However,
Hex colors from stdin will replace the selected color.
.I Pal
will close when stdin closes.
.PP