0023414: Remove deprecated classes Xw_PixMap and WNT_PixMap
[occt.git] / src / Xw / Xw_def_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_COLOR
24 #endif
25
26 /*
27    XW_STATUS Xw_def_color(acolormap,index,r,g,b):
28    XW_EXT_COLORMAP *acolormap
29    int index ;          Color index 0 >= x < MAXCOLOR
30    float r,g,b ;        Red,Green,Blue color value 0. >= x <= 1.
31
32         Update Color Extended colormap index with the specified R,G,B values .
33
34         Returns ERROR if BadColor Index
35         Returns SUCCESS if Successful      
36
37 */
38
39 #ifdef XW_PROTOTYPE
40 XW_STATUS Xw_def_color (void* acolormap,
41                         int index,float r,float g,float b)
42 #else
43 XW_STATUS Xw_def_color (acolormap,index,r,g,b)
44 void *acolormap;
45 int index ;
46 float r,g,b ;
47 #endif /*XW_PROTOTYPE*/
48 {
49 XW_EXT_COLORMAP *pcolormap = (XW_EXT_COLORMAP*)acolormap ;
50 XColor color ;
51 int cclass,isapproximate;
52 unsigned long pixel;
53 XW_STATUS status = XW_SUCCESS;
54
55        if ( !Xw_isdefine_colorindex(pcolormap,index) ) {
56             /*ERROR*Bad Color Index*/
57             Xw_set_error(1,"Xw_def_color",&index) ;
58             return (XW_ERROR) ;
59         }
60
61         if( pcolormap->mapping == Xw_TOM_READONLY ) {
62           cclass = StaticColor;
63         } else {
64           cclass = _CCLASS;
65         }
66
67         switch (cclass) {
68             
69             case TrueColor :
70                 Xw_get_color_pixel(pcolormap,r,g,b,&pixel,&isapproximate) ;
71                 break ;
72
73             case PseudoColor :
74                 if( pcolormap->mapping == Xw_TOM_COLORCUBE ) {
75                   int kindex;
76                   if( (_CGINFO.red_mult > 0) && (fabs(r-g) < 0.01) && (fabs(r-b) < 0.01) ) {
77                     kindex = (int)(0.5+r*_CGINFO.red_max)*_CGINFO.red_mult;
78                     if( _CINFO.red_max > 0 ) 
79                                 kindex += (_CINFO.red_max+1)*(_CINFO.green_max+1)*(_CINFO.blue_max+1);
80                   } else if( _CINFO.red_mult > 0 ) {
81                     kindex = ((int)(0.5+r*_CINFO.red_max))*_CINFO.red_mult+
82                             ((int)(0.5+g*_CINFO.green_max))*_CINFO.green_mult+
83                             ((int)(0.5+b*_CINFO.blue_max))*_CINFO.blue_mult;
84                   } else if( _CGINFO.red_mult > 0 ) {
85                     float l = (r+g+b)/3.;
86                     kindex = (int)(0.5+l*_CGINFO.red_max)*_CGINFO.red_mult;
87                   } else {
88                     kindex = 0;
89                   }
90                   pixel = _CINFO.base_pixel + kindex;
91                 } else {
92                     color.pixel = pixel = pcolormap->pixels[index] ;
93                     color.red   = (unsigned short) (r * 0xFFFF) ;
94                     color.green = (unsigned short) (g * 0xFFFF) ;
95                     color.blue  = (unsigned short) (b * 0xFFFF) ;
96                     color.flags = DoRed|DoGreen|DoBlue ;
97                     XStoreColor(_CDISPLAY,_CINFO.colormap,&color) ;
98                 }
99                 break ;
100
101             case StaticColor :
102                 status = Xw_alloc_color(pcolormap,r,g,b,&pixel,&isapproximate) ;
103                 break ;
104         }
105
106         if( status ) {
107           pcolormap->define[index] = USERCOLOR ;
108           pcolormap->pixels[index] = pixel ;
109         }
110
111 #ifdef TRACE_DEF_COLOR
112 if( Xw_get_trace() ) {
113     printf(" %d = Xw_def_color(%lx,%d,%f,%f,%f)\n",status,(long ) pcolormap,index,r,g,b) ;
114 }
115 #endif
116     return (status);
117 }