0023712: Remove dependency on Aspect_GraphicDevice from Aspect_Window
[occt.git] / src / Xw / Xw_ColorMap.cxx
CommitLineData
b311480e 1// Copyright (c) 1995-1999 Matra Datavision
2// Copyright (c) 1999-2012 OPEN CASCADE SAS
3//
4// The content of this file is subject to the Open CASCADE Technology Public
5// License Version 6.5 (the "License"). You may not use the content of this file
6// except in compliance with the License. Please obtain a copy of the License
7// at http://www.opencascade.org and read it completely before using this file.
8//
9// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11//
12// The Original Code and all software distributed under the License is
13// distributed on an "AS IS" basis, without warranty of any kind, and the
14// Initial Developer hereby disclaims all such warranties, including without
15// limitation, any warranties of merchantability, fitness for a particular
16// purpose or non-infringement. Please see the License for the specific terms
17// and conditions governing the rights and limitations under the License.
18
7fd59977 19#define PRO7349 //GG_11/04/97
20// Modification de la methode PixelOfColor
21// qui renvoie maintenant un numero de pixel negatif
22// lorsqu'il correspond a une couleur approximee.
23
24#define PRO10676 //GG_151297
25// Permettre de reserver un index de couleur modifiable
26// dans la colormap.
27
28#include <Xw_ColorMap.ixx>
29#include <Quantity_Color.hxx>
30#include <Standard_ShortReal.hxx>
dc3fe572 31#include <Standard_PCharacter.hxx>
7fd59977 32
33// Routines C a declarer en extern
34//extern "C" {
35#include <Xw_Cextern.hxx>
36//}
37
38//-Static data definitions
39
40static XW_STATUS status ;
41
42void Xw_ColorMap::PrintError() {
43Standard_CString ErrorMessag ;
44Standard_Integer ErrorNumber ;
45Standard_Integer ErrorGravity ;
46
47 status = XW_SUCCESS ;
48 ErrorMessag = Xw_get_error(&ErrorNumber,&ErrorGravity) ;
49 if( ErrorGravity > 2 ) Aspect_ColorMapDefinitionError::Raise (ErrorMessag) ;
50 else Xw_print_error() ;
51}
52
53Xw_ColorMap::Xw_ColorMap () {
54}
55
56Xw_ColorMap::Xw_ColorMap (const Standard_CString Connexion, const Xw_TypeOfVisual Visual, const Xw_TypeOfMapping Mapping, const Standard_Integer Ncolors, const Standard_Boolean UseDefault) {
57Aspect_Handle BaseMap = 0 ;
58
59 MyExtendedDisplay = Xw_open_display((Standard_PCharacter)Connexion) ;
60
61 if( !MyExtendedDisplay ) PrintError() ;
62
63 if( UseDefault ) {
64 Aspect_Handle *display,root ;
65 Xw_TypeOfVisual vclass ;
66 int depth ;
67
68 status = Xw_get_display_info (MyExtendedDisplay,
69 &display,&root,&BaseMap,&vclass,&depth);
70 if( Visual != vclass ) BaseMap = 0;
71 }
72
73 MyExtendedColorMap = Xw_def_colormap(MyExtendedDisplay,
74 Visual,Ncolors,BaseMap,Mapping) ;
75
76 if( !Xw_isdefine_colormap(MyExtendedColorMap) ) PrintError() ;
77
78 MyExtendedOverlayColorMap = Xw_def_colormap(MyExtendedDisplay,
79 Xw_TOV_OVERLAY,0,0,Xw_TOM_READONLY) ;
80
81 SetHighlightColor(Quantity_NOC_WHITESMOKE) ;
82
83 MyMapping = Mapping ;
84}
85
86void Xw_ColorMap::SetEntry (const Standard_Integer Index,const Standard_Real Red, const Standard_Real Green, const Standard_Real Blue) {
87
88
89 switch ( MyMapping ) {
90
91 case Xw_TOM_HARDRAMP:
92 case Xw_TOM_SIMPLERAMP:
93 case Xw_TOM_BESTRAMP:
94 status = Xw_def_color(MyExtendedColorMap,
95 (int)Index,(float)Red,(float)Green,(float)Blue) ;
96
97 if( !status ) PrintError() ;
98 break ;
99
100 case Xw_TOM_COLORCUBE:
101 case Xw_TOM_READONLY:
102 Aspect_BadAccess::Raise ("Try to write in a READ ONLY colormap");
103 }
104
105}
106
107void Xw_ColorMap::SetEntry (const Aspect_ColorMapEntry& Entry) {
108Standard_Real r,g,b ;
109
110 (Entry.Color()).Values(r,g,b,Quantity_TOC_RGB) ;
111
112 SetEntry(Entry.Index(),r,g,b) ;
113
114}
115
116void Xw_ColorMap::SetEntries (const Handle(Aspect_ColorMap)& Colormap) {
117Standard_Integer size = Colormap->Size() ;
118Standard_Integer i ;
119
120 for( i=1 ; i<=size ; i++ ) {
121 SetEntry(Colormap->Entry(i)) ;
122 }
123}
124
125void Xw_ColorMap::SetHighlightColor ( const Quantity_Color& aColor) {
126Standard_Real r,g,b ;
127
128 aColor.Values(r,g,b,Quantity_TOC_RGB) ;
129
130 status = Xw_def_highlight_color(MyExtendedColorMap,
131 (float)r,(float)g,(float)b) ;
132 if( !status ) PrintError() ;
133
134 if( MyExtendedOverlayColorMap ) {
135 status = Xw_def_highlight_color(MyExtendedOverlayColorMap,
136 (float)r,(float)g,(float)b) ;
137 if( !status ) PrintError() ;
138 }
139}
140
141void Xw_ColorMap::Destroy() {
142
143 if( MyExtendedColorMap ) {
144 Xw_close_colormap(MyExtendedColorMap) ;
145 MyExtendedColorMap = NULL ;
146 }
147
148 if( MyExtendedOverlayColorMap ) {
149 Xw_close_colormap(MyExtendedOverlayColorMap) ;
150 MyExtendedOverlayColorMap = NULL ;
151 }
152}
153
154Quantity_Color Xw_ColorMap::HighlightColor ( ) const {
155float r,g,b ;
156
157 status = Xw_get_highlight_color (MyExtendedColorMap,&r,&g,&b);
158
159 Standard_Real Red = r;
160 Standard_Real Green = g;
161 Standard_Real Blue = b;
162
163 return Quantity_Color(Red,Green,Blue,Quantity_TOC_RGB) ;
164}
165
166Standard_Integer Xw_ColorMap::HighlightPixel ( ) const {
167unsigned long pixel ;
168
169 status = Xw_get_highlight_pixel(MyExtendedColorMap,&pixel) ;
170
171 if( !status ) PrintError() ;
172
173 return (Standard_Integer(pixel)) ;
174}
175
176Standard_Integer Xw_ColorMap::PixelOfColor ( const Quantity_Color &aColor ) const {
177Standard_Real Red,Green,Blue ;
178unsigned long pixel ;
179Standard_Integer isapproximate;
180
181 aColor.Values(Red,Green,Blue,Quantity_TOC_RGB) ;
182
183#ifdef PRO7349
184 status = Xw_get_color_pixel(MyExtendedColorMap,
185 (float)Red,(float)Green,(float)Blue,&pixel,&isapproximate) ;
186 if( !status ) PrintError() ;
187
188 if( isapproximate ) return (-Standard_Integer(pixel));
189
190 else return (Standard_Integer(pixel)) ;
191#else
192 status = Xw_get_color_pixel(MyExtendedColorMap,
193 (float)Red,(float)Green,(float)Blue,&pixel) ;
194
195 if( !status ) PrintError() ;
196
197 return (Standard_Integer(pixel)) ;
198#endif
199}
200
201#ifdef PRO10676
202Standard_Integer Xw_ColorMap::AllocatesPixelOfColor ( ) const {
203 unsigned long pixel;
204 if( Xw_alloc_pixel(MyExtendedColorMap,&pixel) )
205 return Standard_Integer(pixel);
206 else {
207 Xw_print_error();
208 return -1;
209 }
210}
211
212void Xw_ColorMap::FreePixelOfColor ( const Standard_Integer aPixel ) const {
213 unsigned long pixel = aPixel;
214 Xw_free_pixel(MyExtendedColorMap,pixel);
215}
216
217Standard_Boolean Xw_ColorMap::SetColorOfPixel ( const Standard_Integer aPixel,
218 const Quantity_Color &aColor ) const {
219Standard_Real Red,Green,Blue ;
220 aColor.Values(Red,Green,Blue,Quantity_TOC_RGB) ;
221 Standard_ShortReal r = Red,g = Green,b = Blue;
222 unsigned long pixel = aPixel;
223 return Xw_set_pixel(MyExtendedColorMap,pixel,r,g,b);
224}
225#endif
226
227Standard_Integer Xw_ColorMap::Entry ( const Standard_Integer Index, Standard_Real &Red, Standard_Real &Green, Standard_Real &Blue ) const {
228unsigned long pixel = 0 ;
229float red,green,blue ;
230
231 status = Xw_get_color(MyExtendedColorMap,Index,
232 &red,&green,&blue,&pixel) ;
233
234 if( !status ) PrintError() ;
235
236 Red = red; Green = green; Blue = blue;
237
238 return (Standard_Integer(pixel)) ;
239}
240
241Xw_TypeOfVisual Xw_ColorMap::VisualClass( ) const {
242Xw_TypeOfVisual vclass ;
243
244 vclass = Xw_get_colormap_visual(MyExtendedColorMap) ;
245
246 if( vclass == Xw_TOV_DEFAULT ) PrintError() ;
247
248 return (vclass) ;
249}
250
251Xw_TypeOfVisual Xw_ColorMap::OverlayVisualClass( ) const {
252Xw_TypeOfVisual vclass = Xw_TOV_DEFAULT;
253
254 if( MyExtendedOverlayColorMap ) {
255 vclass = Xw_get_colormap_visual(MyExtendedOverlayColorMap) ;
256 }
257
258 return (vclass) ;
259}
260
261Standard_Integer Xw_ColorMap::VisualID( ) const {
262Aspect_Handle *VisualInfo ;
263Xw_TypeOfVisual VisualClass ;
264int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid = 0 ;
265
266 status = Xw_get_colormap_info(MyExtendedColorMap,&VisualInfo,
267 &VisualClass,&visualid,&MaxColor,&BasePixel,
268 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
269
270 if( !status ) Xw_print_error();
271
272 return (visualid) ;
273}
274
275Standard_Integer Xw_ColorMap::OverlayVisualID( ) const {
276Aspect_Handle *VisualInfo ;
277Xw_TypeOfVisual VisualClass ;
278int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid = 0;
279
280 if( MyExtendedOverlayColorMap ) {
281 status = Xw_get_colormap_info(MyExtendedOverlayColorMap,&VisualInfo,
282 &VisualClass,&visualid,&MaxColor,&BasePixel,
283 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
284
285 if( !status ) Xw_print_error() ;
286 }
287 return (visualid) ;
288}
289
290Standard_Integer Xw_ColorMap::MaxColors( ) const {
291Aspect_Handle *VisualInfo ;
292Xw_TypeOfVisual VisualClass ;
293int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid ;
294
295 status = Xw_get_colormap_info(MyExtendedColorMap,&VisualInfo,
296 &VisualClass,&visualid,&MaxColor,&BasePixel,
297 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
298
299 if( status ) {
300 if( VisualClass == Xw_TOV_TRUECOLOR ) return MaxColor ;
301 else return MaxUserColor;
302 } else {
303 Xw_print_error() ;
304 return 0 ;
305 }
306}
307
308Standard_Integer Xw_ColorMap::MaxOverlayColors( ) const {
309Aspect_Handle *VisualInfo ;
310Xw_TypeOfVisual VisualClass ;
311int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid ;
312
313 status = XW_ERROR;
314 if( MyExtendedOverlayColorMap ) {
315 status = Xw_get_colormap_info(MyExtendedOverlayColorMap,&VisualInfo,
316 &VisualClass,&visualid,&MaxColor,&BasePixel,
317 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
318 if( !status ) Xw_print_error() ;
319 }
320
321 return (status) ? MaxUserColor : 0;
322}
323
324Standard_Address Xw_ColorMap::XVisual( ) const {
325Aspect_Handle *VisualInfo ;
326Xw_TypeOfVisual VisualClass ;
327int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid ;
328
329 status = Xw_get_colormap_info(MyExtendedColorMap,&VisualInfo,
330 &VisualClass,&visualid,&MaxColor,&BasePixel,
331 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
332
333 if( !status ) PrintError() ;
334
335 return (VisualInfo) ;
336}
337
338Standard_Address Xw_ColorMap::XOverlayVisual( ) const {
339Aspect_Handle *VisualInfo = NULL ;
340Xw_TypeOfVisual VisualClass ;
341int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid ;
342
343 status = XW_ERROR;
344 if( MyExtendedOverlayColorMap ) {
345 status = Xw_get_colormap_info(MyExtendedOverlayColorMap,&VisualInfo,
346 &VisualClass,&visualid,&MaxColor,&BasePixel,
347 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
348 if( !status ) Xw_print_error() ;
349 }
350
351 return (status) ? VisualInfo : NULL ;
352}
353
354Aspect_Handle Xw_ColorMap::XColorMap( ) const {
355Aspect_Handle colormap ;
356
357 colormap = Xw_get_colormap_xid(MyExtendedColorMap) ;
358
359 if( !colormap ) PrintError() ;
360
361 return (colormap) ;
362}
363
364Aspect_Handle Xw_ColorMap::XOverlayColorMap( ) const {
365Aspect_Handle colormap = 0;
366
367 if( MyExtendedOverlayColorMap ) {
368 colormap = Xw_get_colormap_xid(MyExtendedOverlayColorMap) ;
369 }
370
371 return (colormap);
372}
373
374Standard_Address Xw_ColorMap::ExtendedColorMap () const {
375
376 return (MyExtendedColorMap);
377
378}
379
380Standard_Address Xw_ColorMap::ExtendedOverlayColorMap () const {
381
382 return (MyExtendedOverlayColorMap);
383
384}
385
386Standard_Boolean Xw_ColorMap::XColorCube ( Aspect_Handle &ColormapID,
387 Standard_Integer &VisualID,
388 Standard_Integer &BasePixel,
389 Standard_Integer &RedMax,
390 Standard_Integer &RedMult,
391 Standard_Integer &GreenMax,
392 Standard_Integer &GreenMult,
393 Standard_Integer &BlueMax,
394 Standard_Integer &BlueMult ) const
395{
396
397 status = Xw_get_colormap_colorcube(MyExtendedColorMap,
398 &ColormapID,&VisualID,&BasePixel,
399 &RedMax,&RedMult,&GreenMax,&GreenMult,&BlueMax,&BlueMult);
400
401 return status;
402}
403
404
405Standard_Boolean Xw_ColorMap::XGrayRamp ( Aspect_Handle &ColormapID,
406 Standard_Integer &VisualID,
407 Standard_Integer &BasePixel,
408 Standard_Integer &GrayMax,
409 Standard_Integer &GrayMult) const
410{
411
412 status = Xw_get_colormap_grayramp(MyExtendedColorMap,
413 &ColormapID,&VisualID,&BasePixel,&GrayMax,&GrayMult);
414
415 return status;
416}
417