Blame


1 fd465540 2004-12-27 devnull #include <u.h>
2 fd465540 2004-12-27 devnull #include <libc.h>
3 fd465540 2004-12-27 devnull #include <draw.h>
4 fd465540 2004-12-27 devnull #include <event.h>
5 fd465540 2004-12-27 devnull
6 fd465540 2004-12-27 devnull int nbit, npix;
7 fd465540 2004-12-27 devnull Image *pixel;
8 fd465540 2004-12-27 devnull Rectangle crect[256];
9 fd465540 2004-12-27 devnull
10 fd465540 2004-12-27 devnull Image *color[256];
11 fd465540 2004-12-27 devnull
12 fd465540 2004-12-27 devnull void
13 fd465540 2004-12-27 devnull eresized(int new)
14 fd465540 2004-12-27 devnull {
15 fd465540 2004-12-27 devnull int x, y, i, n, nx, ny;
16 fd465540 2004-12-27 devnull Rectangle r, b;
17 fd465540 2004-12-27 devnull
18 fd465540 2004-12-27 devnull if(new && getwindow(display, Refnone) < 0){
19 fd465540 2004-12-27 devnull fprint(2, "colors: can't reattach to window: %r\n");
20 fd465540 2004-12-27 devnull exits("resized");
21 fd465540 2004-12-27 devnull }
22 fd465540 2004-12-27 devnull if(screen->depth > 8){
23 fd465540 2004-12-27 devnull n = 256;
24 fd465540 2004-12-27 devnull nx = 16;
25 fd465540 2004-12-27 devnull }else{
26 fd465540 2004-12-27 devnull n = 1<<screen->depth;
27 fd465540 2004-12-27 devnull nx = 1<<(screen->depth/2);
28 fd465540 2004-12-27 devnull }
29 fd465540 2004-12-27 devnull
30 fd465540 2004-12-27 devnull ny = n/nx;
31 fd465540 2004-12-27 devnull draw(screen, screen->r, display->white, nil, ZP);
32 fd465540 2004-12-27 devnull r = insetrect(screen->r, 5);
33 fd465540 2004-12-27 devnull r.min.y+=20;
34 fd465540 2004-12-27 devnull b.max.y=r.min.y;
35 fd465540 2004-12-27 devnull for(i=n-1, y=0; y!=ny; y++){
36 fd465540 2004-12-27 devnull b.min.y=b.max.y;
37 fd465540 2004-12-27 devnull b.max.y=r.min.y+(r.max.y-r.min.y)*(y+1)/ny;
38 fd465540 2004-12-27 devnull b.max.x=r.min.x;
39 fd465540 2004-12-27 devnull for(x=0; x!=nx; x++, --i){
40 fd465540 2004-12-27 devnull b.min.x=b.max.x;
41 fd465540 2004-12-27 devnull b.max.x=r.min.x+(r.max.x-r.min.x)*(x+1)/nx;
42 fd465540 2004-12-27 devnull crect[i]=insetrect(b, 1);
43 fd465540 2004-12-27 devnull draw(screen, crect[i], color[i], nil, ZP);
44 fd465540 2004-12-27 devnull }
45 fd465540 2004-12-27 devnull }
46 fd465540 2004-12-27 devnull flushimage(display, 1);
47 fd465540 2004-12-27 devnull }
48 fd465540 2004-12-27 devnull
49 fd465540 2004-12-27 devnull char *buttons[] =
50 fd465540 2004-12-27 devnull {
51 fd465540 2004-12-27 devnull "exit",
52 fd465540 2004-12-27 devnull 0
53 fd465540 2004-12-27 devnull };
54 fd465540 2004-12-27 devnull
55 fd465540 2004-12-27 devnull ulong
56 fd465540 2004-12-27 devnull grey(int i)
57 fd465540 2004-12-27 devnull {
58 fd465540 2004-12-27 devnull if(i < 0)
59 fd465540 2004-12-27 devnull return grey(0);
60 fd465540 2004-12-27 devnull if(i > 255)
61 fd465540 2004-12-27 devnull return grey(255);
62 fd465540 2004-12-27 devnull return (i<<16)+(i<<8)+i;
63 fd465540 2004-12-27 devnull }
64 fd465540 2004-12-27 devnull
65 fd465540 2004-12-27 devnull Menu menu =
66 fd465540 2004-12-27 devnull {
67 fd465540 2004-12-27 devnull buttons
68 fd465540 2004-12-27 devnull };
69 fd465540 2004-12-27 devnull
70 fd465540 2004-12-27 devnull int
71 fd465540 2004-12-27 devnull dither[16] = {
72 fd465540 2004-12-27 devnull 0, 8, 2, 10,
73 fd465540 2004-12-27 devnull 12, 4, 14, 6,
74 fd465540 2004-12-27 devnull 3, 11, 1, 9,
75 fd465540 2004-12-27 devnull 15, 7, 13, 5
76 fd465540 2004-12-27 devnull };
77 fd465540 2004-12-27 devnull
78 fd465540 2004-12-27 devnull void
79 fd465540 2004-12-27 devnull main(int argc, char *argv[])
80 fd465540 2004-12-27 devnull {
81 fd465540 2004-12-27 devnull Point p;
82 fd465540 2004-12-27 devnull Mouse m;
83 fd465540 2004-12-27 devnull int i, j, k, l, n, ramp, prev;
84 fd465540 2004-12-27 devnull char buf[100];
85 fd465540 2004-12-27 devnull char *fmt;
86 fd465540 2004-12-27 devnull Image *dark;
87 fd465540 2004-12-27 devnull ulong rgb;
88 fd465540 2004-12-27 devnull
89 fd465540 2004-12-27 devnull ramp = 0;
90 fd465540 2004-12-27 devnull
91 fd465540 2004-12-27 devnull fmt = "index %3d r %3lud g %3lud b %3lud 0x%.8luX ";
92 fd465540 2004-12-27 devnull ARGBEGIN{
93 fd465540 2004-12-27 devnull default:
94 fd465540 2004-12-27 devnull goto Usage;
95 fd465540 2004-12-27 devnull case 'x':
96 fd465540 2004-12-27 devnull fmt = "index %2luX r %3luX g %3luX b %3luX 0x%.8luX ";
97 fd465540 2004-12-27 devnull break;
98 fd465540 2004-12-27 devnull case 'r':
99 fd465540 2004-12-27 devnull ramp = 1;
100 fd465540 2004-12-27 devnull break;
101 fd465540 2004-12-27 devnull }ARGEND
102 fd465540 2004-12-27 devnull
103 fd465540 2004-12-27 devnull if(argc){
104 fd465540 2004-12-27 devnull Usage:
105 fd465540 2004-12-27 devnull fprint(2, "Usage: %s [-rx]\n", argv0);
106 fd465540 2004-12-27 devnull exits("usage");
107 fd465540 2004-12-27 devnull }
108 fd465540 2004-12-27 devnull
109 c345061e 2005-01-07 devnull if(initdraw(0, 0, "colors") < 0)
110 fd465540 2004-12-27 devnull sysfatal("initdraw failed: %r");
111 fd465540 2004-12-27 devnull einit(Emouse);
112 fd465540 2004-12-27 devnull
113 fd465540 2004-12-27 devnull for(i=0; i<256; i++){
114 fd465540 2004-12-27 devnull if(ramp){
115 fd465540 2004-12-27 devnull if(screen->chan == CMAP8){
116 fd465540 2004-12-27 devnull /* dither the fine grey */
117 fd465540 2004-12-27 devnull j = i-(i%17);
118 fd465540 2004-12-27 devnull dark = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (grey(j)<<8)+0xFF);
119 fd465540 2004-12-27 devnull color[i] = allocimage(display, Rect(0,0,4,4), screen->chan, 1, (grey(j+17)<<8)+0xFF);
120 fd465540 2004-12-27 devnull for(j=0; j<16; j++){
121 fd465540 2004-12-27 devnull k = j%4;
122 fd465540 2004-12-27 devnull l = j/4;
123 fd465540 2004-12-27 devnull if(dither[j] > (i%17))
124 fd465540 2004-12-27 devnull draw(color[i], Rect(k, l, k+1, l+1), dark, nil, ZP);
125 fd465540 2004-12-27 devnull }
126 fd465540 2004-12-27 devnull freeimage(dark);
127 fd465540 2004-12-27 devnull }else
128 fd465540 2004-12-27 devnull color[i] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (grey(i)<<8)+0xFF);
129 fd465540 2004-12-27 devnull }else
130 fd465540 2004-12-27 devnull color[i] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (cmap2rgb(i)<<8)+0xFF);
131 fd465540 2004-12-27 devnull if(color[i] == nil)
132 fd465540 2004-12-27 devnull sysfatal("can't allocate image: %r");
133 fd465540 2004-12-27 devnull }
134 fd465540 2004-12-27 devnull eresized(0);
135 fd465540 2004-12-27 devnull prev = -1;
136 fd465540 2004-12-27 devnull for(;;){
137 ccff3cd0 2005-03-18 devnull flushimage(display, 1);
138 fd465540 2004-12-27 devnull m = emouse();
139 fd465540 2004-12-27 devnull switch(m.buttons){
140 fd465540 2004-12-27 devnull case 1:
141 fd465540 2004-12-27 devnull while(m.buttons){
142 fd465540 2004-12-27 devnull if(screen->depth > 8)
143 fd465540 2004-12-27 devnull n = 256;
144 fd465540 2004-12-27 devnull else
145 fd465540 2004-12-27 devnull n = 1<<screen->depth;
146 fd465540 2004-12-27 devnull for(i=0; i!=n; i++)
147 fd465540 2004-12-27 devnull if(i!=prev && ptinrect(m.xy, crect[i])){
148 fd465540 2004-12-27 devnull if(ramp)
149 fd465540 2004-12-27 devnull rgb = grey(i);
150 fd465540 2004-12-27 devnull else
151 fd465540 2004-12-27 devnull rgb = cmap2rgb(i);
152 fd465540 2004-12-27 devnull sprint(buf, fmt,
153 fd465540 2004-12-27 devnull i,
154 fd465540 2004-12-27 devnull (rgb>>16)&0xFF,
155 fd465540 2004-12-27 devnull (rgb>>8)&0xFF,
156 fd465540 2004-12-27 devnull rgb&0xFF,
157 fd465540 2004-12-27 devnull (rgb<<8) | 0xFF);
158 fd465540 2004-12-27 devnull p = addpt(screen->r.min, Pt(2,2));
159 fd465540 2004-12-27 devnull draw(screen, Rpt(p, addpt(p, stringsize(font, buf))), display->white, nil, p);
160 fd465540 2004-12-27 devnull string(screen, p, display->black, ZP, font, buf);
161 fd465540 2004-12-27 devnull prev=i;
162 fd465540 2004-12-27 devnull break;
163 fd465540 2004-12-27 devnull }
164 fd465540 2004-12-27 devnull m = emouse();
165 fd465540 2004-12-27 devnull }
166 fd465540 2004-12-27 devnull break;
167 fd465540 2004-12-27 devnull
168 fd465540 2004-12-27 devnull case 4:
169 fd465540 2004-12-27 devnull switch(emenuhit(3, &m, &menu)){
170 fd465540 2004-12-27 devnull case 0:
171 fd465540 2004-12-27 devnull exits(0);
172 fd465540 2004-12-27 devnull }
173 fd465540 2004-12-27 devnull }
174 fd465540 2004-12-27 devnull }
175 fd465540 2004-12-27 devnull }