17 p = emalloc(sizeof(*p));
29 for(i = 0; i < p->ncol; i++)
36 readpal(Pal *p, Biobuf *bp)
43 if(tline(bp, &s, args, nelem(args)) != 2)
45 if(strcmp(args[0], "pal") != 0)
47 nc = strtol(args[1], &sp, 0);
48 if(*sp != 0 || nc < 0)
53 p->cols = emalloc(nc * sizeof(*p->cols));
54 p->ims = emalloc(nc * sizeof(*p->ims));
55 for(i = 0; i < nc; i++){
56 if(tline(bp, &s, args, nelem(args)) != 1)
58 c = strtol(args[0], &sp, 0);
59 if(*sp != 0 || c < 0 || c > 0xffffff)
65 for(i = 0; i < nc; i++)
66 p->ims[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, p->cols[i] << 8 | 0xff);
67 p->id = getident(bp->fid);
72 werrstr("invalid format");
77 writepal(Pal *p, char *f)
84 bp = Bopen(f, OWRITE);
90 rc = Bprint(bp, "pal %d\n", p->ncol);
93 for(i = 0; i < p->ncol; i++){
94 rc = Bprint(bp, "%#.6x\n", p->cols[i]);
103 cmdprint("%s: #%d\n", f, n);
112 findpal(char *sf, char *fn, int op)
123 s = emalloc(strlen(sf) + strlen(fn) + 2);
137 if(i.type == (uint)-1){
141 for(f = flist.next; f != &flist; f = f->next)
142 if(f->type == PAL && identcmp(&f->id, &i) == 0){
152 bp = emalloc(sizeof(*bp));
153 Binit(bp, fd, OREAD);
155 if(readpal(p, bp) < 0){
174 for(f = flist.next; f != &flist; f = f->next)
175 if(f->type == SPR && ((Spr *) f)->pal == p)
180 palsize(Pal *p, int sz)
186 p->cols = realloc(p->cols, sz * sizeof(*p->cols));
187 p->ims = realloc(p->ims, sz * sizeof(*p->ims));
189 memset(p->cols + p->ncol, 0, sz);
190 for(i = p->ncol; i < sz; i++)
191 p->ims[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0);
206 if(w->type != PAL || w->f == nil)
207 sysfatal("paldraw: phase error");
209 n = Dx(w->inner) / w->zoom;
210 draw(w->im, w->inner, w->tab->cols[BACK], nil, ZP);
211 for(i = 0; i < p->ncol; i++){
212 r.min = addpt(w->inner.min, mulpt(Pt(i%n, i/n), w->zoom));
213 r.max.x = r.min.x + w->zoom;
214 r.max.y = r.min.y + w->zoom;
215 draw(w->im, r, p->ims[i], nil, ZP);
217 border(w->im, r, SELSIZ, display->white, ZP);
222 palset(Pal *p, int s, u32int c)
224 if(s < 0 || s >= p->ncol || p->cols[s] == c)
227 freeimage(p->ims[s]);
228 p->ims[s] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, c << 8 | 0xff);
242 palzerox(Win *w, Win *v)
248 palclick(Win *w, Mousectl *mc)
254 if(!ptinrect(mc->xy, w->inner))
257 sysfatal("palclick: phase error");
259 n = Dx(w->inner) / w->zoom;
260 pt = subpt(mc->xy, w->inner.min);
261 if(pt.x >= n * w->zoom)
263 i = pt.x / w->zoom + pt.y / w->zoom * n;