0023426: Tool to compare two runs of tests on the same station
[occt.git] / src / Xw / Xw_def_background_color.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
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.
7 //
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.
10 //
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.
17
18
19 #include <Xw_Extension.h>
20
21 /* ifdef then trace on */
22 #ifdef TRACE
23 #define TRACE_DEF_BACKGROUND_COLOR
24 #endif
25
26 /*
27    XW_STATUS Xw_def_background_color(awindow,r,g,b):
28    XW_EXT_WINDOW *awindow
29    float r,g,b ;        Red,Green,Blue color value 0. >= x <= 1.
30
31         Update Background Color index with the specified R,G,B values .
32
33         Returns ERROR if Extended Window or Colormap is wrong
34         Returns SUCCESS if Successful      
35
36 */
37
38 #ifdef XW_PROTOTYPE
39 XW_STATUS Xw_def_background_color (void* awindow,
40                                         float r,float g,float b)
41 #else
42 XW_STATUS Xw_def_background_color (awindow,r,g,b)
43 void *awindow;
44 float r,g,b ;
45 #endif /*XW_PROTOTYPE*/
46 {
47 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*)awindow ;
48 XW_EXT_COLORMAP *pcolormap ;
49 unsigned long backpixel,highpixel ;
50 XColor color ;
51 int i,index,cclass,isapproximate ;
52 XW_STATUS status = XW_SUCCESS;
53
54         if ( !Xw_isdefine_window(pwindow) ) {
55             /*ERROR*Bad EXT_WINDOW Address*/
56             Xw_set_error(24,"Xw_def_background_color",pwindow) ;
57             return (XW_ERROR) ;
58         }
59
60         if ( !Xw_isdefine_colormap(_COLORMAP) ) {
61             /*ERROR*Bad EXT_COLORMAP Address*/
62             Xw_set_error(42,"Xw_def_background_color",_COLORMAP) ;
63             return (XW_ERROR) ;
64         }
65
66         pcolormap = _COLORMAP ;
67
68         index = pwindow->backindex ;
69
70         if( index >= 0 ) {
71           if( pcolormap->define[index] != BACKCOLOR ) {
72             for( i=0 ; i<pcolormap->maxcolor ; i++ ) {
73                 if( pcolormap->define[i] == FREECOLOR ) break;
74             }
75
76             if( i < pcolormap->maxcolor ) {
77                 index = i ;
78                 pcolormap->define[i] = BACKCOLOR;
79             } 
80           }
81
82           if( pcolormap->mapping == Xw_TOM_READONLY ) { 
83             cclass = StaticColor; 
84           } else { 
85             cclass = _CCLASS; 
86           }
87
88           switch ( cclass ) {
89             
90             case TrueColor :
91                 Xw_get_color_pixel(pcolormap,r,g,b,&backpixel,&isapproximate) ;
92                 break ;
93
94             case PseudoColor :
95                 if( pcolormap->mapping == Xw_TOM_COLORCUBE ) {
96                   if( (_CGINFO.red_mult > 0) && (fabs(r-g) < 0.01) && (fabs(r-b) < 0.01) ) {
97                     index = (int)(0.5+r*_CGINFO.red_max)*_CGINFO.red_mult;
98                     if( _CINFO.red_max > 0 ) 
99                                 index += (_CINFO.red_max+1)*(_CINFO.green_max+1)*(_CINFO.blue_max+1);
100                   } else if( _CINFO.red_mult > 0 ) { 
101                     index = ((int)(0.5+r*_CINFO.red_max))*_CINFO.red_mult+
102                             ((int)(0.5+g*_CINFO.green_max))*_CINFO.green_mult+
103                             ((int)(0.5+b*_CINFO.blue_max))*_CINFO.blue_mult;
104                   } else if( _CGINFO.red_mult > 0 ) {
105                     float l = (r+g+b)/3.; 
106                     index = (int)(0.5+l*_CGINFO.red_max)*_CGINFO.red_mult;
107                   } else {
108                     index = 0;
109                   } 
110                   backpixel = _CINFO.base_pixel = index;
111                 } else if( pcolormap->define[index] == BACKCOLOR ) {
112                     color.pixel = backpixel = pcolormap->pixels[index] ;
113                     color.red   = (unsigned short) (r * 0xFFFF) ;
114                     color.green = (unsigned short) (g * 0xFFFF) ;
115                     color.blue  = (unsigned short) (b * 0xFFFF) ;
116                     color.flags = DoRed|DoGreen|DoBlue ;
117                     XStoreColor(_DISPLAY,_CINFO.colormap,&color) ;
118                 }
119                 break ;
120
121             case StaticColor :
122                 status = Xw_alloc_color(pcolormap,r,g,b,&backpixel,&isapproximate) ;
123                 break ;
124           }
125
126           if( status ) {
127             pwindow->backindex = index ;
128             pcolormap->pixels[index] = backpixel ;
129           }
130         } else {
131           backpixel = pcolormap->backpixel ;    /* Transparent window */
132         }
133
134         if( status ) {
135           highpixel = pcolormap->highpixel ^ backpixel ;
136           XSetWindowBackground (_DISPLAY, _WINDOW, backpixel) ;
137           XSetBackground(_DISPLAY, pwindow->qgwind.gc, backpixel) ;
138           XSetBackground(_DISPLAY, pwindow->qgwind.gccopy, backpixel) ;
139           XSetBackground(_DISPLAY, pwindow->qgwind.gcclear, backpixel) ;
140           XSetBackground(_DISPLAY, pwindow->qgwind.gchigh, backpixel) ;
141           XSetForeground(_DISPLAY, pwindow->qgwind.gcclear, backpixel) ;
142           XSetForeground(_DISPLAY, pwindow->qgwind.gchigh, highpixel) ;
143           for( i=0 ; i<MAXQG ; i++ ) {
144             XSetBackground(_DISPLAY, pwindow->qgline[i].gc, backpixel);
145             XSetBackground(_DISPLAY, pwindow->qgpoly[i].gc, backpixel);
146             XSetBackground(_DISPLAY, pwindow->qgtext[i].gc, backpixel);
147             XSetBackground(_DISPLAY, pwindow->qgmark[i].gc, backpixel);
148           }
149         }
150
151 #ifdef TRACE_DEF_BACKGROUND_COLOR
152 if( Xw_get_trace() ) {
153     printf(" %d = Xw_def_background_color(%lx,%f,%f,%f)\n",status,(long ) pwindow,r,g,b) ;
154 }
155 #endif
156     return (status);
157 }