1 #define PRO9517 /*GG_280897
2 // Ameliorer le critere de comparaison de 2 couleurs
3 // sinon ROUGE = VERT = BLEU !!!
6 #include <Xw_Extension.h>
8 /* ifdef then trace on */
10 #define TRACE_GET_COLOR_INDEX
14 STATUS Xw_get_color_index(acolormap,r,g,b,index):
15 XW_EXT_COLORMAP *acolormap Colormap extension structure
16 float r,g,b ; Red,Green,Blue color value 0. >= x <= 1.
17 int *index ; Return Color index 0 >= x < MAXCOLOR
19 Gets the nearest color index from R,G,B Color values.
21 Returns ERROR if BadColor Index or color is not defined
22 Returns SUCCESS if Successful
27 #ifdef TEST /* Optim. No more necessary with XQueryColors() */
29 static int indice = 0;
30 static int indice_max = 0;
31 static XW_EXT_COLORMAP *qcolormap = NULL;
32 static float tab_ind[MAX_COLRS][4];
36 XW_STATUS Xw_get_color_index(void* acolormap,float r,float g,float b,int* index)
38 XW_STATUS Xw_get_color_index(acolormap,r,g,b,index)
42 #endif /*XW_PROTOTYPE*/
44 XW_EXT_COLORMAP *pcolormap = (XW_EXT_COLORMAP*)acolormap ;
45 XW_STATUS status = XW_ERROR ;
46 XColor colors[MAXCOLOR] ;
47 int color_indexs[MAXCOLOR] ;
49 int curind = -1,freeind = -1,isapproximate ;
51 int drmin,dgmin,dbmin;
53 float curdist,newdist ;
58 /*ERROR*Bad EXT_COLORMAP Address*/
59 Xw_set_error( 42,"Xw_get_color_index",pcolormap ) ;
63 if( _CCLASS != TrueColor ) {
64 if( pcolormap->mapping == Xw_TOM_COLORCUBE ) {
65 if( (_CGINFO.red_mult > 0) && (fabs(r-g) < 0.01) && (fabs(r-b) < 0.01) ) {
66 curind = (int)(0.5+r*_CGINFO.red_max)*_CGINFO.red_mult;
67 if( _CINFO.red_max > 0 ) curind += (_CINFO.red_max+1)*(_CINFO.green_max+1)*(_CINFO.blue_max+1);
68 } else if( _CINFO.red_mult > 0 ) {
69 curind = ((int)(0.5+r*_CINFO.red_max))*_CINFO.red_mult+
70 ((int)(0.5+g*_CINFO.green_max))*_CINFO.green_mult+
71 ((int)(0.5+b*_CINFO.blue_max))*_CINFO.blue_mult;
72 } else if( _CGINFO.red_mult > 0 ) {
74 curind = (int)(0.5+l*_CGINFO.red_max)*_CGINFO.red_mult;
78 if( pcolormap->pixels[curind] == curind +_CINFO.base_pixel )
86 if (pcolormap != qcolormap) {
87 for (i=0; i<MAX_COLRS; i++) tab_ind[i][0] = -1.;
88 qcolormap = pcolormap;
93 for (i=0; i<indice_max; i++) {
94 if ( tab_ind[i][0] >= 0. ) {
95 if ((tab_ind[i][1] == r) &&
96 (tab_ind[i][2] == g) &&
97 (tab_ind[i][3] == b)) {
98 *index = curind = (int) tab_ind[i][0];
100 #ifdef TRACE_GET_COLOR_INDEX
101 if( Xw_get_trace() > 3 ) {
102 printf(" %d = Xw_get_color_index(%x,%f,%f,%f,=%d/0x%x)\n",
103 status,pcolormap,r,g,b,curind,pcolormap->pixels[curind]) ;
113 drmin = dgmin = dbmin = 65536;
120 for( i=n=0 ; i<pcolormap->maxcolor ; i++ ) {
121 if( Xw_isdefine_color( pcolormap,i ) == XW_SUCCESS ) {
122 if( n < pcolormap->maxhcolor ) {
123 colors[n].pixel = pcolormap->pixels[i];
127 for( j=0 ; j<n ; j++ ) {
128 if( colors[j].pixel == pcolormap->pixels[i] ) break;
131 /* Too many entrys in the virtual colormap */
132 Xw_set_error(129,"Xw_get_color_index",pcolormap ) ;
136 if( freeind < 0 ) freeind = i ;
140 if( _CCLASS != TrueColor ) {
142 XQueryColors(_CDISPLAY,_CINFO.colormap,colors,n) ;
145 unsigned long rred = (long unsigned int )( r * 0xFFFF);
146 unsigned long rgreen = (long unsigned int )( g * 0xFFFF);
147 unsigned long rblue = (long unsigned int )( b * 0xFFFF);
149 for( i=0 ; i<n ; i++ ){
150 //#if defined(_AIX) || defined(__hpux)
151 int aLocalInt = rred - colors[i].red ; dr = abs( aLocalInt ) >> 8;
152 aLocalInt = rgreen - colors[i].green ; dg = abs( aLocalInt ) >> 8;
153 aLocalInt = rblue - colors[i].blue ; db = abs( aLocalInt ) >> 8;
155 // dr = abs( rred - colors[i].red ) >> 8;
156 // dg = abs( rgreen - colors[i].green ) >> 8;
157 // db = abs( rblue - colors[i].blue ) >> 8;
159 if( (dr <= drmin) && (dg <= dgmin) && (db <= dbmin) ) {
160 drmin = dr; dgmin = dg; dbmin = db;
161 curind = color_indexs[i];
162 if( !dr && !dg && !db ) break;
167 for( i=0 ; ((i<n) && (newdist != 0.0)) ; i++ ){
168 dr = (float)colors[i].red/0xFFFF - r ;
169 dg = (float)colors[i].green/0xFFFF - g ;
170 db = (float)colors[i].blue/0xFFFF - b ;
171 newdist = dr*dr + dg*dg + db*db ;
172 if( newdist < curdist ) {
173 curind = color_indexs[i] ;
181 // unsigned long mask = _CVISUAL->map_entries-1 ;
182 unsigned long red,green,blue,rmask,gmask,bmask ;
185 unsigned long rred = (long unsigned int )( r * (1 << _CVISUAL->bits_per_rgb) - 1);
186 unsigned long rgreen = (long unsigned int )( g * (1 << _CVISUAL->bits_per_rgb) - 1);
187 unsigned long rblue = (long unsigned int )( b * (1 << _CVISUAL->bits_per_rgb) - 1);
189 for( i=0 ; i<n ; i++ ){
190 rmask = _CVISUAL->red_mask;
191 red = colors[i].pixel & rmask ;
192 while ( !(rmask & 0x01) ) { rmask >>= 1; red >>= 1; }
194 gmask = _CVISUAL->green_mask;
195 green = colors[i].pixel & gmask ;
196 while ( !(gmask & 0x01) ) { gmask >>= 1; green >>= 1; }
198 bmask = _CVISUAL->blue_mask;
199 blue = colors[i].pixel & bmask ;
200 while ( !(bmask & 0x01) ) { bmask >>= 1; blue >>= 1; }
202 //#if defined(_AIX) || defined(__hpux)
203 int aLocalInt = rred - red ; dr = abs(aLocalInt);
204 aLocalInt = rgreen - green ; dg = abs(aLocalInt);
205 aLocalInt = rblue - blue ; db = abs(aLocalInt);
207 // dr = abs( rred - red );
208 // dg = abs( rgreen - green );
209 // db = abs( rblue - blue );
211 if( (dr <= drmin) && (dg <= dgmin) && (db <= dbmin) ) {
212 drmin = dr; dgmin = dg; dbmin = db;
213 curind = color_indexs[i];
214 if( !dr && !dg && !db ) break;
219 for( i=0 ; ((i<n) && (newdist != 0.0)) ; i++ ){
220 rmask = _CVISUAL->red_mask;
221 red = colors[i].pixel & rmask ;
222 while ( !(rmask & 0x01) ) { rmask >>= 1; red >>= 1; }
224 gmask = _CVISUAL->green_mask;
225 green = colors[i].pixel & gmask ;
226 while ( !(gmask & 0x01) ) { gmask >>= 1; green >>= 1; }
228 bmask = _CVISUAL->blue_mask;
229 blue = colors[i].pixel & bmask ;
230 while ( !(bmask & 0x01) ) { bmask >>= 1; blue >>= 1; }
232 dr = (float)red/mask - r ;
233 dg = (float)green/mask - g ;
234 db = (float)blue/mask - b ;
235 newdist = dr*dr + dg*dg + db*db ;
237 if( newdist < curdist ) {
238 curind = color_indexs[i] ;
247 if( drmin > 0 || dgmin > 0 || dbmin > 0 ) {
249 if( curdist > 0.0 ) {
253 if( Xw_alloc_color(pcolormap,r,g,b,&pixel,&isapproximate) ) {
254 for( i=0 ; i<pcolormap->maxcolor ; i++ ) {
255 if( pixel == pcolormap->pixels[i] ) break;
257 if( i < pcolormap->maxcolor ) {
262 if( Xw_def_color(pcolormap,freeind,r,g,b) ) {
269 status = XW_SUCCESS ;
271 tab_ind[indice][0] = curind ;
272 tab_ind[indice][1] = r ;
273 tab_ind[indice][2] = g ;
274 tab_ind[indice][3] = b ;
276 * Une fois que le tableau est plein, il faut le
277 * parcourir completement, donc on ne touche plus
281 if( indice < MAX_COLRS ) {
282 indice_max = max(indice_max,indice);
284 indice = 0 ; /* on repart a 0 */
285 #ifdef TRACE_GET_COLOR_INDEX
286 if( Xw_get_trace() > 1 ) {
287 printf(" ************************\n") ;
288 printf(" Xw_get_color_index, full array\n") ;
289 printf(" ************************\n") ;
300 #ifdef TRACE_GET_COLOR_INDEX
301 if( Xw_get_trace() > 3 ) {
302 printf(" %d = Xw_get_color_index(%lx,%f,%f,%f,#%d/0x%lx)\n",
303 status,(long ) pcolormap,r,g,b,curind,(long ) pcolormap->pixels[curind]) ;