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 IMP080300 //GG Add protection on FindColorMapIndex() |
20 | |
21 | #define IMP060400 //GG Take in account the Hue of the color |
22 | // for computing the nearest color index. |
23 | |
24 | #include <Aspect_GenericColorMap.ixx> |
25 | #include <Aspect_ColorMapEntry.hxx> |
26 | |
27 | Aspect_GenericColorMap::Aspect_GenericColorMap ():Aspect_ColorMap (Aspect_TOC_Generic) { |
28 | Aspect_ColorMapEntry theDefaultEntry; |
29 | |
30 | AddEntry(theDefaultEntry); |
31 | } |
32 | |
33 | void Aspect_GenericColorMap::AddEntry (const Aspect_ColorMapEntry& AnEntry) { |
34 | Standard_Integer index = AnEntry.Index(); |
35 | |
36 | if ( myDataMap.IsBound( index ) ) { |
37 | Standard_Integer i = myDataMap( index ) ; |
38 | mydata.SetValue( i , AnEntry ) ; |
39 | } else { |
40 | mydata.Append( AnEntry ) ; |
41 | myDataMap.Bind( index , mydata.Length() ) ; |
42 | } |
43 | } |
44 | |
45 | Standard_Integer Aspect_GenericColorMap::AddEntry (const Quantity_Color &aColor) { |
46 | Aspect_ColorMapEntry theEntry ; |
47 | Standard_Integer i,maxindex = 0 ; |
48 | |
49 | for( i=1 ; i<=mydata.Length() ; i++ ) { |
50 | theEntry = mydata.Value(i) ; |
51 | maxindex = Max(maxindex,theEntry.Index()) ; |
52 | if( theEntry.Color() == aColor ) return theEntry.Index() ; |
53 | } |
54 | |
55 | maxindex++ ; |
56 | theEntry.SetValue(maxindex,aColor) ; |
57 | mydata.Append( theEntry ) ; |
58 | myDataMap.Bind( maxindex , mydata.Length() ) ; |
59 | return maxindex ; |
60 | } |
61 | |
62 | void Aspect_GenericColorMap::RemoveEntry (const Standard_Integer index) { |
63 | |
64 | mydata.Remove( index ) ; |
65 | } |
66 | |
67 | const Aspect_ColorMapEntry& Aspect_GenericColorMap::NearestEntry( |
68 | const Quantity_Color& color ) const |
69 | |
70 | { |
71 | return( Entry( NearestColorMapIndex( color ) ) ) ; |
72 | } |
73 | |
74 | Standard_Integer Aspect_GenericColorMap::NearestColorMapIndex( |
75 | const Quantity_Color& color ) const |
76 | |
77 | { |
78 | |
79 | Standard_Real dist ; |
80 | struct { |
81 | Standard_Real dist ; |
82 | Standard_Integer index ; |
83 | } nearest; |
84 | |
85 | nearest.dist = 0. ; |
86 | nearest.index = 0 ; |
87 | |
88 | #ifdef IMP060400 |
89 | Standard_Integer ehue,hue = (color.Hue() < 0.) ? -1 : |
90 | Standard_Integer(color.Hue())/60; |
91 | #endif |
92 | |
93 | Quantity_Color ecolor; |
94 | |
95 | if ( Size() == 0 ) |
96 | Aspect_BadAccess::Raise ("NearestColorMapIndex() ColorMap is empty."); |
97 | |
98 | for ( Standard_Integer i = 1 ; i <= Size() ; i++ ) { |
99 | if ( Entry(i).IsAllocated() ) { |
100 | ecolor = Entry(i).Color(); |
101 | dist = color.SquareDistance( ecolor ) ; |
102 | #ifdef IMP060400 |
103 | ehue = (ecolor.Hue() < 0.) ? -1 : Standard_Integer(ecolor.Hue())/60; |
104 | if ( (nearest.index == 0) || |
105 | ((dist < nearest.dist) && (hue == ehue)) ) { |
106 | #else |
107 | if ( nearest.index == 0 || dist < nearest.dist ) { |
108 | #endif |
109 | nearest.index = i ; |
110 | nearest.dist = dist ; |
111 | #ifdef IMP060400 |
112 | if( dist == 0.0 ) break; |
113 | #endif |
114 | } |
115 | } |
116 | } |
117 | |
118 | if ( nearest.index == 0 ) |
119 | Aspect_BadAccess::Raise ("NearestEntryIndex() ColorMap is empty."); |
120 | |
121 | return( nearest.index ) ; |
122 | } |
123 | |
124 | const Aspect_ColorMapEntry& Aspect_GenericColorMap::FindEntry( |
125 | const Standard_Integer ColorEntryIndex ) const |
126 | |
127 | { |
128 | return( Entry( FindColorMapIndex( ColorEntryIndex ) ) ) ; |
129 | } |
130 | |
131 | Standard_Integer Aspect_GenericColorMap::FindColorMapIndex( |
132 | const Standard_Integer ColorEntryIndex ) const |
133 | { |
134 | Standard_Integer index = 0; |
135 | #ifdef IMP080300 |
136 | if( myDataMap.IsBound( ColorEntryIndex ) ) |
137 | index = myDataMap.Find( ColorEntryIndex ); |
138 | #else |
139 | index = myDataMap ( ColorEntryIndex ); |
140 | #endif |
141 | return index; |
142 | } |