4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
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
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_showcol_c
= "$Id$";
50 static bool ColCallBack(struct t_x11
*x11
,XEvent
*event
, Window w
, void *data
)
60 switch (event
->type
) {
65 printf("%6d%6d%6d\t\t%-20s\n",r
,g
,b
,wd
->text
);
71 static bool BCallBack(struct t_x11
*x11
,XEvent
*event
, Window w
, void *data
)
76 switch (event
->type
) {
78 XSetForeground(x11
->disp
,x11
->gc
,sc
->col
[sc
->ncol
-1].xc
.pixel
);
79 TextInWin(x11
,&sc
->but
,sc
->but
.text
,eXCenter
,eYCenter
);
82 sc
->but
.width
=event
->xconfigure
.width
;
83 sc
->but
.height
=event
->xconfigure
.height
;
86 LightBorder(x11
->disp
,sc
->but
.self
,sc
->col
[sc
->ncol
-1].xc
.pixel
);
89 LightBorder(x11
->disp
,sc
->but
.self
,sc
->col
[0].xc
.pixel
);
92 x11
->UnRegisterCallback(x11
,sc
->wd
.self
);
93 x11
->UnRegisterCallback(x11
,sc
->but
.self
);
99 static bool scCallBack(struct t_x11
*x11
,XEvent
*event
, Window w
, void *data
)
103 int nx
,ny
,X
,Y
,Dx
,Dy
,th
;
106 th
=XTextHeight(x11
->font
)+6;
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)) {
114 printf("Disp: %x, But: %x, Font: %x x: %d, th: %d\n",
115 x11
->disp
,sc
->but
.self
,x11
->font
,(int)x
,th
);
117 XResizeWindow(x11
->disp
,sc
->but
.self
,(int)x
-4,th
-4);
120 while (nx
*ny
< sc
->ncol
)
124 for(i
=0; (i
<sc
->ncol
); i
++) {
129 XMoveWindow(x11
->disp
,sc
->col
[i
].wd
.self
,X
,th
+Y
);
130 XResizeWindow(x11
->disp
,sc
->col
[i
].wd
.self
,Dx
,Dy
);
138 static int col_comp(const void *c1
,const void *c2
)
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
);
151 if ((dr
=(x1
->red
-x2
->red
)) != 0)
153 if ((dg
=(x1
->green
-x2
->green
)) != 0)
155 return x1
->blue
-x2
->blue
;
161 static void read_col(t_x11
*x11
,t_sc
*sc
,char *rgb
)
164 char buf
[STRLEN
],name
[STRLEN
],*dummy
;
170 if ((fp
=fopen(rgb
,"r"))==NULL
) {
176 if (fscanf(fp
,"%d%d%d",&r
,&g
,&b
)==3) {
177 dummy
=fgets2(buf
,STRLEN
-1,fp
);
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
;
188 printf("color %3d: %s\n",sc
->ncol
,buf
);
190 InitWin(&(sc
->col
[sc
->ncol
-1].wd
),0,0,1,1,0,name
);
198 qsort(sc
->col
,sc
->ncol
,sizeof(sc
->col
[0]),col_comp
);
199 /* Now filter out doubles */
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
)) {
212 static t_sc
*init_sc(t_x11
*x11
,Window Parent
,char *rgb
)
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
|
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
);
248 void main(int argc
, char *argv
[])
254 x11
=GetX11(&argc
,argv
);
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
);