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.
19 #include <Xw_Extension.h>
21 /* ifdef then trace on */
23 #define TRACE_GET_COLOR_PIXEL
27 STATUS Xw_get_color_pixel(acolormap,r,g,b,pixel,isapproximate):
28 XW_EXT_COLORMAP *acolormap Colormap extension structure
29 float r,g,b ; Red,Green,Blue color value 0. >= x <= 1.
30 unsigned long *pixel ; Return Color pixel depending of Visual Class
33 Gets the nearest color pixel from R,G,B Color values.
35 Returns ERROR if Colormap is not defined properly
36 Returns SUCCESS if Successful
41 #ifdef TEST /* Optim. no more necessary with XQueryColors */
43 static int indice = 0;
44 static int indice_max = 0;
45 static XW_EXT_COLORMAP *qcolormap = NULL;
46 static float tab_ind[MAX_COLRS][4];
50 XW_STATUS Xw_get_color_pixel(void* acolormap,
51 float r,float g,float b,unsigned long* pixel,int *isapproximate)
53 XW_STATUS Xw_get_color_pixel(acolormap,r,g,b,pixel,isapproximate)
56 unsigned long *pixel ;
58 #endif /*XW_PROTOTYPE*/
60 XW_EXT_COLORMAP *pcolormap = (XW_EXT_COLORMAP*)acolormap ;
61 XW_STATUS status = XW_ERROR ;
62 //XColor colors[MAXCOLOR] ;
63 //int color_indexs[MAXCOLOR] ;
69 /*ERROR*Bad EXT_COLORMAP Address*/
70 Xw_set_error( 42,"Xw_get_color_pixel",pcolormap ) ;
74 *isapproximate = False;
75 if( _CCLASS == TrueColor ) {
76 unsigned long mask = _CVISUAL->map_entries-1 ;
77 unsigned long red = (unsigned long) (r * mask) ;
78 unsigned long green = (unsigned long) (g * mask) ;
79 unsigned long blue = (unsigned long) (b * mask) ;
81 mask = _CVISUAL->red_mask;
82 while ( !(mask & 0x01) ) { mask >>= 1; red <<= 1; }
83 mask = _CVISUAL->green_mask;
84 while ( !(mask & 0x01) ) { mask >>= 1; green <<= 1; }
85 mask = _CVISUAL->blue_mask;
86 while ( !(mask & 0x01) ) { mask >>= 1; blue <<= 1; }
87 *pixel = red|green|blue ;
89 } else if( pcolormap->mapping == Xw_TOM_COLORCUBE ) {
91 if( (_CGINFO.red_mult > 0) && (fabs(r-g) < 0.01) && (fabs(r-b) < 0.01) ) {
92 curind = (int)(0.5+r*_CGINFO.red_max)*_CGINFO.red_mult;
93 if( _CINFO.red_max > 0 ) curind += (_CINFO.red_max+1)*(_CINFO.green_max+1)*(_CINFO.blue_max+1);
94 } else if( _CINFO.red_mult > 0 ) {
95 curind = ((int)(0.5+r*_CINFO.red_max))*_CINFO.red_mult+
96 ((int)(0.5+g*_CINFO.green_max))*_CINFO.green_mult+
97 ((int)(0.5+b*_CINFO.blue_max))*_CINFO.blue_mult;
98 } else if( _CGINFO.red_mult > 0 ) {
100 curind = (int)(0.5+l*_CGINFO.red_max)*_CGINFO.red_mult;
102 *pixel = _CINFO.base_pixel + curind ;
103 if( pcolormap->pixels[curind] == *pixel ) status = XW_SUCCESS ;
109 if (pcolormap != qcolormap) {
110 for (i=0; i<MAX_COLRS; i++) tab_ind[i][0] = -1.;
111 qcolormap = pcolormap;
116 for (i=0; i<indice_max; i++) {
117 if (tab_ind[i][0] >= 0.) {
118 if ((tab_ind[i][1] == r) &&
119 (tab_ind[i][2] == g) &&
120 (tab_ind[i][3] == b)) {
121 *pixel = (int) tab_ind[i][0];
130 status = Xw_alloc_color(pcolormap,r,g,b,pixel,isapproximate) ;
134 tab_ind[indice][0] = *pixel ;
135 tab_ind[indice][1] = r ;
136 tab_ind[indice][2] = g ;
137 tab_ind[indice][3] = b ;
139 * Une fois que le tableau est plein, il faut le
140 * parcourir completement, donc on ne touche plus
144 if( indice < MAX_COLRS ) {
145 indice_max = max(indice_max,indice);
147 indice = 0 ; /* on repart a 0 */
148 #ifdef TRACE_GET_COLOR_PIXEL
149 if( Xw_get_trace() > 1 ) {
150 printf(" ************************\n") ;
151 printf(" Xw_get_color_pixel, full array\n") ;
152 printf(" ************************\n") ;
162 #ifdef TRACE_GET_COLOR_PIXEL
163 if( Xw_get_trace() > 3 ) {
164 printf(" %d = Xw_get_color_pixel(%lx,%f,%f,%f,%lx,%d)\n",
165 status,(long ) pcolormap,r,g,b,*pixel,*isapproximate) ;