1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #define PRO9517 /*GG_280897
19 // Ameliorer le critere de comparaison de 2 couleurs
20 // sinon ROUGE = VERT = BLEU !!!
23 #include <Xw_Extension.h>
25 /* ifdef then trace on */
27 #define TRACE_GET_COLOR_INDEX
31 STATUS Xw_get_color_index(acolormap,r,g,b,index):
32 XW_EXT_COLORMAP *acolormap Colormap extension structure
33 float r,g,b ; Red,Green,Blue color value 0. >= x <= 1.
34 int *index ; Return Color index 0 >= x < MAXCOLOR
36 Gets the nearest color index from R,G,B Color values.
38 Returns ERROR if BadColor Index or color is not defined
39 Returns SUCCESS if Successful
44 #ifdef TEST /* Optim. No more necessary with XQueryColors() */
46 static int indice = 0;
47 static int indice_max = 0;
48 static XW_EXT_COLORMAP *qcolormap = NULL;
49 static float tab_ind[MAX_COLRS][4];
53 XW_STATUS Xw_get_color_index(void* acolormap,float r,float g,float b,int* index)
55 XW_STATUS Xw_get_color_index(acolormap,r,g,b,index)
59 #endif /*XW_PROTOTYPE*/
61 XW_EXT_COLORMAP *pcolormap = (XW_EXT_COLORMAP*)acolormap ;
62 XW_STATUS status = XW_ERROR ;
63 XColor colors[MAXCOLOR] ;
64 int color_indexs[MAXCOLOR] ;
66 int curind = -1,freeind = -1,isapproximate ;
68 int drmin,dgmin,dbmin;
70 float curdist,newdist ;
75 /*ERROR*Bad EXT_COLORMAP Address*/
76 Xw_set_error( 42,"Xw_get_color_index",pcolormap ) ;
80 if( _CCLASS != TrueColor ) {
81 if( pcolormap->mapping == Xw_TOM_COLORCUBE ) {
82 if( (_CGINFO.red_mult > 0) && (fabs(r-g) < 0.01) && (fabs(r-b) < 0.01) ) {
83 curind = (int)(0.5+r*_CGINFO.red_max)*_CGINFO.red_mult;
84 if( _CINFO.red_max > 0 ) curind += (_CINFO.red_max+1)*(_CINFO.green_max+1)*(_CINFO.blue_max+1);
85 } else if( _CINFO.red_mult > 0 ) {
86 curind = ((int)(0.5+r*_CINFO.red_max))*_CINFO.red_mult+
87 ((int)(0.5+g*_CINFO.green_max))*_CINFO.green_mult+
88 ((int)(0.5+b*_CINFO.blue_max))*_CINFO.blue_mult;
89 } else if( _CGINFO.red_mult > 0 ) {
91 curind = (int)(0.5+l*_CGINFO.red_max)*_CGINFO.red_mult;
95 if( pcolormap->pixels[curind] == curind +_CINFO.base_pixel )
103 if (pcolormap != qcolormap) {
104 for (i=0; i<MAX_COLRS; i++) tab_ind[i][0] = -1.;
105 qcolormap = pcolormap;
110 for (i=0; i<indice_max; i++) {
111 if ( tab_ind[i][0] >= 0. ) {
112 if ((tab_ind[i][1] == r) &&
113 (tab_ind[i][2] == g) &&
114 (tab_ind[i][3] == b)) {
115 *index = curind = (int) tab_ind[i][0];
117 #ifdef TRACE_GET_COLOR_INDEX
118 if( Xw_get_trace() > 3 ) {
119 printf(" %d = Xw_get_color_index(%x,%f,%f,%f,=%d/0x%x)\n",
120 status,pcolormap,r,g,b,curind,pcolormap->pixels[curind]) ;
130 drmin = dgmin = dbmin = 65536;
137 for( i=n=0 ; i<pcolormap->maxcolor ; i++ ) {
138 if( Xw_isdefine_color( pcolormap,i ) == XW_SUCCESS ) {
139 if( n < pcolormap->maxhcolor ) {
140 colors[n].pixel = pcolormap->pixels[i];
144 for( j=0 ; j<n ; j++ ) {
145 if( colors[j].pixel == pcolormap->pixels[i] ) break;
148 /* Too many entrys in the virtual colormap */
149 Xw_set_error(129,"Xw_get_color_index",pcolormap ) ;
153 if( freeind < 0 ) freeind = i ;
157 if( _CCLASS != TrueColor ) {
159 XQueryColors(_CDISPLAY,_CINFO.colormap,colors,n) ;
162 unsigned long rred = (long unsigned int )( r * 0xFFFF);
163 unsigned long rgreen = (long unsigned int )( g * 0xFFFF);
164 unsigned long rblue = (long unsigned int )( b * 0xFFFF);
166 for( i=0 ; i<n ; i++ ){
167 //#if defined(_AIX) || defined(__hpux)
168 int aLocalInt = rred - colors[i].red ; dr = abs( aLocalInt ) >> 8;
169 aLocalInt = rgreen - colors[i].green ; dg = abs( aLocalInt ) >> 8;
170 aLocalInt = rblue - colors[i].blue ; db = abs( aLocalInt ) >> 8;
172 // dr = abs( rred - colors[i].red ) >> 8;
173 // dg = abs( rgreen - colors[i].green ) >> 8;
174 // db = abs( rblue - colors[i].blue ) >> 8;
176 if( (dr <= drmin) && (dg <= dgmin) && (db <= dbmin) ) {
177 drmin = dr; dgmin = dg; dbmin = db;
178 curind = color_indexs[i];
179 if( !dr && !dg && !db ) break;
184 for( i=0 ; ((i<n) && (newdist != 0.0)) ; i++ ){
185 dr = (float)colors[i].red/0xFFFF - r ;
186 dg = (float)colors[i].green/0xFFFF - g ;
187 db = (float)colors[i].blue/0xFFFF - b ;
188 newdist = dr*dr + dg*dg + db*db ;
189 if( newdist < curdist ) {
190 curind = color_indexs[i] ;
198 // unsigned long mask = _CVISUAL->map_entries-1 ;
199 unsigned long red,green,blue,rmask,gmask,bmask ;
202 unsigned long rred = (long unsigned int )( r * (1 << _CVISUAL->bits_per_rgb) - 1);
203 unsigned long rgreen = (long unsigned int )( g * (1 << _CVISUAL->bits_per_rgb) - 1);
204 unsigned long rblue = (long unsigned int )( b * (1 << _CVISUAL->bits_per_rgb) - 1);
206 for( i=0 ; i<n ; i++ ){
207 rmask = _CVISUAL->red_mask;
208 red = colors[i].pixel & rmask ;
209 while ( !(rmask & 0x01) ) { rmask >>= 1; red >>= 1; }
211 gmask = _CVISUAL->green_mask;
212 green = colors[i].pixel & gmask ;
213 while ( !(gmask & 0x01) ) { gmask >>= 1; green >>= 1; }
215 bmask = _CVISUAL->blue_mask;
216 blue = colors[i].pixel & bmask ;
217 while ( !(bmask & 0x01) ) { bmask >>= 1; blue >>= 1; }
219 //#if defined(_AIX) || defined(__hpux)
220 int aLocalInt = rred - red ; dr = abs(aLocalInt);
221 aLocalInt = rgreen - green ; dg = abs(aLocalInt);
222 aLocalInt = rblue - blue ; db = abs(aLocalInt);
224 // dr = abs( rred - red );
225 // dg = abs( rgreen - green );
226 // db = abs( rblue - blue );
228 if( (dr <= drmin) && (dg <= dgmin) && (db <= dbmin) ) {
229 drmin = dr; dgmin = dg; dbmin = db;
230 curind = color_indexs[i];
231 if( !dr && !dg && !db ) break;
236 for( i=0 ; ((i<n) && (newdist != 0.0)) ; i++ ){
237 rmask = _CVISUAL->red_mask;
238 red = colors[i].pixel & rmask ;
239 while ( !(rmask & 0x01) ) { rmask >>= 1; red >>= 1; }
241 gmask = _CVISUAL->green_mask;
242 green = colors[i].pixel & gmask ;
243 while ( !(gmask & 0x01) ) { gmask >>= 1; green >>= 1; }
245 bmask = _CVISUAL->blue_mask;
246 blue = colors[i].pixel & bmask ;
247 while ( !(bmask & 0x01) ) { bmask >>= 1; blue >>= 1; }
249 dr = (float)red/mask - r ;
250 dg = (float)green/mask - g ;
251 db = (float)blue/mask - b ;
252 newdist = dr*dr + dg*dg + db*db ;
254 if( newdist < curdist ) {
255 curind = color_indexs[i] ;
264 if( drmin > 0 || dgmin > 0 || dbmin > 0 ) {
266 if( curdist > 0.0 ) {
270 if( Xw_alloc_color(pcolormap,r,g,b,&pixel,&isapproximate) ) {
271 for( i=0 ; i<pcolormap->maxcolor ; i++ ) {
272 if( pixel == pcolormap->pixels[i] ) break;
274 if( i < pcolormap->maxcolor ) {
279 if( Xw_def_color(pcolormap,freeind,r,g,b) ) {
286 status = XW_SUCCESS ;
288 tab_ind[indice][0] = curind ;
289 tab_ind[indice][1] = r ;
290 tab_ind[indice][2] = g ;
291 tab_ind[indice][3] = b ;
293 * Une fois que le tableau est plein, il faut le
294 * parcourir completement, donc on ne touche plus
298 if( indice < MAX_COLRS ) {
299 indice_max = max(indice_max,indice);
301 indice = 0 ; /* on repart a 0 */
302 #ifdef TRACE_GET_COLOR_INDEX
303 if( Xw_get_trace() > 1 ) {
304 printf(" ************************\n") ;
305 printf(" Xw_get_color_index, full array\n") ;
306 printf(" ************************\n") ;
317 #ifdef TRACE_GET_COLOR_INDEX
318 if( Xw_get_trace() > 3 ) {
319 printf(" %d = Xw_get_color_index(%lx,%f,%f,%f,#%d/0x%lx)\n",
320 status,(long ) pcolormap,r,g,b,curind,(long ) pcolormap->pixels[curind]) ;