7fd59977 |
1 | |
2 | #include <Xw_Extension.h> |
3 | |
4 | /* ifdef then trace on */ |
5 | #ifdef TRACE |
6 | #define TRACE_DEF_COLOR |
7 | #endif |
8 | |
9 | /* |
10 | XW_STATUS Xw_def_color(acolormap,index,r,g,b): |
11 | XW_EXT_COLORMAP *acolormap |
12 | int index ; Color index 0 >= x < MAXCOLOR |
13 | float r,g,b ; Red,Green,Blue color value 0. >= x <= 1. |
14 | |
15 | Update Color Extended colormap index with the specified R,G,B values . |
16 | |
17 | Returns ERROR if BadColor Index |
18 | Returns SUCCESS if Successful |
19 | |
20 | */ |
21 | |
22 | #ifdef XW_PROTOTYPE |
23 | XW_STATUS Xw_def_color (void* acolormap, |
24 | int index,float r,float g,float b) |
25 | #else |
26 | XW_STATUS Xw_def_color (acolormap,index,r,g,b) |
27 | void *acolormap; |
28 | int index ; |
29 | float r,g,b ; |
30 | #endif /*XW_PROTOTYPE*/ |
31 | { |
32 | XW_EXT_COLORMAP *pcolormap = (XW_EXT_COLORMAP*)acolormap ; |
33 | XColor color ; |
34 | int cclass,isapproximate; |
35 | unsigned long pixel; |
36 | XW_STATUS status = XW_SUCCESS; |
37 | |
38 | if ( !Xw_isdefine_colorindex(pcolormap,index) ) { |
39 | /*ERROR*Bad Color Index*/ |
40 | Xw_set_error(1,"Xw_def_color",&index) ; |
41 | return (XW_ERROR) ; |
42 | } |
43 | |
44 | if( pcolormap->mapping == Xw_TOM_READONLY ) { |
45 | cclass = StaticColor; |
46 | } else { |
47 | cclass = _CCLASS; |
48 | } |
49 | |
50 | switch (cclass) { |
51 | |
52 | case TrueColor : |
53 | Xw_get_color_pixel(pcolormap,r,g,b,&pixel,&isapproximate) ; |
54 | break ; |
55 | |
56 | case PseudoColor : |
57 | if( pcolormap->mapping == Xw_TOM_COLORCUBE ) { |
58 | int kindex; |
59 | if( (_CGINFO.red_mult > 0) && (fabs(r-g) < 0.01) && (fabs(r-b) < 0.01) ) { |
60 | kindex = (int)(0.5+r*_CGINFO.red_max)*_CGINFO.red_mult; |
61 | if( _CINFO.red_max > 0 ) |
62 | kindex += (_CINFO.red_max+1)*(_CINFO.green_max+1)*(_CINFO.blue_max+1); |
63 | } else if( _CINFO.red_mult > 0 ) { |
64 | kindex = ((int)(0.5+r*_CINFO.red_max))*_CINFO.red_mult+ |
65 | ((int)(0.5+g*_CINFO.green_max))*_CINFO.green_mult+ |
66 | ((int)(0.5+b*_CINFO.blue_max))*_CINFO.blue_mult; |
67 | } else if( _CGINFO.red_mult > 0 ) { |
68 | float l = (r+g+b)/3.; |
69 | kindex = (int)(0.5+l*_CGINFO.red_max)*_CGINFO.red_mult; |
70 | } else { |
71 | kindex = 0; |
72 | } |
73 | pixel = _CINFO.base_pixel + kindex; |
74 | } else { |
75 | color.pixel = pixel = pcolormap->pixels[index] ; |
76 | color.red = (unsigned short) (r * 0xFFFF) ; |
77 | color.green = (unsigned short) (g * 0xFFFF) ; |
78 | color.blue = (unsigned short) (b * 0xFFFF) ; |
79 | color.flags = DoRed|DoGreen|DoBlue ; |
80 | XStoreColor(_CDISPLAY,_CINFO.colormap,&color) ; |
81 | } |
82 | break ; |
83 | |
84 | case StaticColor : |
85 | status = Xw_alloc_color(pcolormap,r,g,b,&pixel,&isapproximate) ; |
86 | break ; |
87 | } |
88 | |
89 | if( status ) { |
90 | pcolormap->define[index] = USERCOLOR ; |
91 | pcolormap->pixels[index] = pixel ; |
92 | } |
93 | |
94 | #ifdef TRACE_DEF_COLOR |
95 | if( Xw_get_trace() ) { |
96 | printf(" %d = Xw_def_color(%lx,%d,%f,%f,%f)\n",status,(long ) pcolormap,index,r,g,b) ; |
97 | } |
98 | #endif |
99 | return (status); |
100 | } |