changed reading hint
[gromacs/adressmacs.git] / src / ngmx / showcol.c
blob6c313cdd8cd91a9aafaff3d62f6c78babf980344
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_showcol_c = "$Id$";
31 #include <math.h>
32 #include <smalloc.h>
33 #include <sysstuff.h>
34 #include <string2.h>
35 #include <Xstuff.h>
36 #include "xutil.h"
38 typedef struct {
39 XColor xc;
40 t_windata wd;
41 } t_col;
43 typedef struct {
44 t_windata wd;
45 t_windata but;
46 int ncol;
47 t_col *col;
48 } t_sc;
50 static bool ColCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
52 t_col *col;
53 XColor *xc;
54 int r,g,b;
55 t_windata *wd;
57 col=(t_col *)data;
58 xc=&(col->xc);
59 wd=&(col->wd);
60 switch (event->type) {
61 case ButtonPress:
62 r=xc->red >> 8;
63 g=xc->green >> 8;
64 b=xc->blue >> 8;
65 printf("%6d%6d%6d\t\t%-20s\n",r,g,b,wd->text);
66 break;
68 return FALSE;
71 static bool BCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
73 t_sc *sc;
75 sc=(t_sc *)data;
76 switch (event->type) {
77 case Expose:
78 XSetForeground(x11->disp,x11->gc,sc->col[sc->ncol-1].xc.pixel);
79 TextInWin(x11,&sc->but,sc->but.text,eXCenter,eYCenter);
80 break;
81 case ConfigureNotify:
82 sc->but.width=event->xconfigure.width;
83 sc->but.height=event->xconfigure.height;
84 break;
85 case EnterNotify:
86 LightBorder(x11->disp,sc->but.self,sc->col[sc->ncol-1].xc.pixel);
87 break;
88 case LeaveNotify:
89 LightBorder(x11->disp,sc->but.self,sc->col[0].xc.pixel);
90 break;
91 case ButtonPress:
92 x11->UnRegisterCallback(x11,sc->wd.self);
93 x11->UnRegisterCallback(x11,sc->but.self);
94 return TRUE;
96 return FALSE;
99 static bool scCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
101 t_sc *sc;
102 int i;
103 int nx,ny,X,Y,Dx,Dy,th;
104 float x,y,dx,dy;
106 th=XTextHeight(x11->font)+6;
107 sc=(t_sc *)data;
108 switch (event->type) {
109 case ConfigureNotify:
110 x=sc->wd.width=event->xconfigure.width;
111 y=sc->wd.height=event->xconfigure.height-th;
112 if ((x >= 100) && (y >= 100)) {
113 #ifdef DEBUG
114 printf("Disp: %x, But: %x, Font: %x x: %d, th: %d\n",
115 x11->disp,sc->but.self,x11->font,(int)x,th);
116 #endif
117 XResizeWindow(x11->disp,sc->but.self,(int)x-4,th-4);
118 nx=sqrt(sc->ncol);
119 ny=sc->ncol/nx;
120 while (nx*ny < sc->ncol)
121 ny++;
122 dx=((float)x) / nx;
123 dy=((float)y) / ny;
124 for(i=0; (i<sc->ncol); i++) {
125 X=x=(i%nx) * dx;
126 Y=y=(i/nx) * dy;
127 Dx=(int)(x+dx)-X;
128 Dy=(int)(y+dy)-Y;
129 XMoveWindow(x11->disp,sc->col[i].wd.self,X,th+Y);
130 XResizeWindow(x11->disp,sc->col[i].wd.self,Dx,Dy);
133 break;
135 return FALSE;
138 static int col_comp(const void *c1,const void *c2)
140 XColor *x1,*x2;
141 int dr,dg;
142 int diff;
144 x1=&(((t_col *)c1)->xc);
145 x2=&(((t_col *)c2)->xc);
147 /* sort on intensity */
148 diff=(x1->red+x1->green+x1->blue)-(x2->red+x2->green+x2->blue);
150 if (diff==0) {
151 if ((dr=(x1->red-x2->red)) != 0)
152 return dr;
153 if ((dg=(x1->green-x2->green)) != 0)
154 return dg;
155 return x1->blue-x2->blue;
157 else
158 return diff;
161 static void read_col(t_x11 *x11,t_sc *sc,char *rgb)
163 FILE *fp;
164 char buf[STRLEN],name[STRLEN],*dummy;
165 int i,j;
166 int r,g,b;
167 XColor xc,exact;
168 t_col *cols;
170 if ((fp=fopen(rgb,"r"))==NULL) {
171 perror(rgb);
172 exit(1);
174 do {
175 dummy=NULL;
176 if (fscanf(fp,"%d%d%d",&r,&g,&b)==3) {
177 dummy=fgets2(buf,STRLEN-1,fp);
178 if (dummy) {
179 trim(buf);
180 /* Filter out colours with names of two words */
181 sscanf(buf,"%s",name);
182 /* Filter out duplicate colours (grey and gray) */
183 if (strstr(name,"gray")==0) {
184 if (XAllocNamedColor(x11->disp,x11->cmap,name,&xc,&exact)) {
185 srenew(sc->col,++sc->ncol);
186 sc->col[sc->ncol-1].xc=xc;
187 #ifdef DEBUG
188 printf("color %3d: %s\n",sc->ncol,buf);
189 #endif
190 InitWin(&(sc->col[sc->ncol-1].wd),0,0,1,1,0,name);
195 } while (dummy);
196 fclose(fp);
197 if (sc->ncol)
198 qsort(sc->col,sc->ncol,sizeof(sc->col[0]),col_comp);
199 /* Now filter out doubles */
200 cols=&(sc->col[0]);
201 for(i=1,j=0; (i<sc->ncol); i++) {
202 if ((cols[i].xc.red != cols[j].xc.red) ||
203 (cols[i].xc.green != cols[j].xc.green) ||
204 (cols[i].xc.blue != cols[j].xc.blue)) {
205 j++;
206 cols[j]=cols[i];
209 sc->ncol=j;
212 static t_sc *init_sc(t_x11 *x11,Window Parent,char *rgb)
214 t_sc *sc;
215 Window w;
216 int i;
218 snew(sc,1);
219 InitWin(&sc->wd,0,0,400,300,1,"Show Colours");
220 sc->wd.self=XCreateSimpleWindow(x11->disp,Parent,sc->wd.x,sc->wd.y,
221 sc->wd.width,sc->wd.height,
222 sc->wd.bwidth,WHITE,BLACK);
223 x11->RegisterCallback(x11,sc->wd.self,Parent,scCallBack,sc);
224 x11->SetInputMask(x11,sc->wd.self,ButtonPressMask | ExposureMask |
225 StructureNotifyMask);
226 InitWin(&sc->but,0,0,sc->wd.width-2,XTextHeight(x11->font)+2,1,"Quit");
227 sc->but.self=XCreateSimpleWindow(x11->disp,sc->wd.self,sc->but.x,sc->but.y,
228 sc->but.width,sc->but.height,
229 sc->but.bwidth,BLACK,BLACK);
230 x11->RegisterCallback(x11,sc->but.self,sc->but.self,BCallBack,sc);
231 x11->SetInputMask(x11,sc->but.self,ButtonPressMask | ExposureMask |
232 StructureNotifyMask | EnterWindowMask |
233 LeaveWindowMask);
235 read_col(x11,sc,rgb);
236 fprintf(stderr,"%d colors found\n",sc->ncol);
237 fprintf(stderr,"%6s%6s%6s\t\t%-20s\n","Red","Green","Blue","name");
238 for(i=0; (i<sc->ncol); i++) {
239 sc->col[i].wd.self=w=XCreateSimpleWindow(x11->disp,sc->wd.self,0,0,1,1,0,
240 BLACK,sc->col[i].xc.pixel);
241 x11->RegisterCallback(x11,w,sc->wd.self,ColCallBack,&(sc->col[i]));
242 x11->SetInputMask(x11,w,ButtonPressMask);
245 return sc;
248 void main(int argc, char *argv[])
250 t_x11 *x11;
251 t_sc *sc;
252 char *fn;
254 x11=GetX11(&argc,argv);
255 if (argc > 1)
256 fn=argv[1];
257 else
258 fn="/usr/lib/X11/rgb.txt";
259 sc=init_sc(x11,x11->root,fn);
260 XMapWindow(x11->disp,sc->wd.self);
261 XMapSubwindows(x11->disp,sc->wd.self);
262 x11->MainLoop(x11);
263 x11->CleanUp(x11);