0024624: Lost word in license statement in source files
[occt.git] / src / Aspect / Aspect_GenericColorMap.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #define IMP080300       //GG Add protection on FindColorMapIndex()
16
17 #define IMP060400       //GG Take in account the Hue of the color
18 //                      for computing the nearest color index.
19
20 #include <Aspect_GenericColorMap.ixx>
21 #include <Aspect_ColorMapEntry.hxx>
22
23 Aspect_GenericColorMap::Aspect_GenericColorMap ():Aspect_ColorMap (Aspect_TOC_Generic) {
24 Aspect_ColorMapEntry theDefaultEntry;
25
26     AddEntry(theDefaultEntry);
27 }
28
29 void Aspect_GenericColorMap::AddEntry (const Aspect_ColorMapEntry& AnEntry) {
30 Standard_Integer index = AnEntry.Index();
31
32     if ( myDataMap.IsBound( index ) ) {
33         Standard_Integer i = myDataMap( index ) ;
34         mydata.SetValue( i , AnEntry ) ;
35     } else {
36         mydata.Append( AnEntry ) ;
37         myDataMap.Bind( index , mydata.Length() ) ; 
38     }
39 }
40
41 Standard_Integer Aspect_GenericColorMap::AddEntry (const Quantity_Color &aColor) {
42 Aspect_ColorMapEntry theEntry ;
43 Standard_Integer i,maxindex = 0 ;
44
45     for( i=1 ; i<=mydata.Length() ; i++ ) {
46         theEntry = mydata.Value(i) ;
47         maxindex = Max(maxindex,theEntry.Index()) ;
48         if( theEntry.Color() == aColor ) return theEntry.Index() ;
49     }
50
51     maxindex++ ;
52     theEntry.SetValue(maxindex,aColor) ;
53     mydata.Append( theEntry ) ;
54     myDataMap.Bind( maxindex , mydata.Length() ) ; 
55     return maxindex ;
56 }
57
58 void Aspect_GenericColorMap::RemoveEntry (const Standard_Integer index) {
59
60    mydata.Remove( index ) ;
61 }
62
63 const Aspect_ColorMapEntry& Aspect_GenericColorMap::NearestEntry( 
64                                 const Quantity_Color&  color ) const
65
66
67   return( Entry( NearestColorMapIndex( color ) ) ) ;
68 }
69
70 Standard_Integer Aspect_GenericColorMap::NearestColorMapIndex( 
71                                 const Quantity_Color&  color ) const
72
73
74
75   Standard_Real    dist ;
76   struct {
77         Standard_Real    dist ;
78         Standard_Integer index ;
79   } nearest;
80
81   nearest.dist  = 0. ;
82   nearest.index = 0 ;
83
84 #ifdef IMP060400
85   Standard_Integer ehue,hue = (color.Hue() < 0.) ? -1 :
86                                 Standard_Integer(color.Hue())/60;
87 #endif
88
89   Quantity_Color ecolor;
90
91   if ( Size() == 0 )
92         Aspect_BadAccess::Raise ("NearestColorMapIndex() ColorMap is empty.");
93
94   for ( Standard_Integer i = 1 ; i <= Size() ; i++ ) {
95     if ( Entry(i).IsAllocated() ) {
96         ecolor = Entry(i).Color();
97         dist = color.SquareDistance( ecolor ) ;
98 #ifdef IMP060400
99         ehue = (ecolor.Hue() < 0.) ? -1 : Standard_Integer(ecolor.Hue())/60;
100         if ( (nearest.index == 0) || 
101                 ((dist < nearest.dist) && (hue == ehue)) ) {
102 #else
103         if ( nearest.index == 0 || dist < nearest.dist ) {
104 #endif
105                 nearest.index = i ;
106                 nearest.dist  = dist ;
107 #ifdef IMP060400
108                 if( dist == 0.0 ) break;
109 #endif
110         }
111     }
112   }
113
114   if ( nearest.index == 0 )
115         Aspect_BadAccess::Raise ("NearestEntryIndex() ColorMap is empty.");
116
117   return( nearest.index ) ;
118 }
119
120 const Aspect_ColorMapEntry& Aspect_GenericColorMap::FindEntry( 
121                 const Standard_Integer ColorEntryIndex ) const 
122
123
124   return( Entry( FindColorMapIndex( ColorEntryIndex ) ) ) ;
125 }
126
127 Standard_Integer Aspect_GenericColorMap::FindColorMapIndex( 
128                 const Standard_Integer ColorEntryIndex ) const 
129 {
130 Standard_Integer index = 0;
131 #ifdef IMP080300
132   if( myDataMap.IsBound( ColorEntryIndex ) ) 
133         index = myDataMap.Find( ColorEntryIndex );
134 #else
135   index = myDataMap ( ColorEntryIndex );
136 #endif
137   return index;
138