summaryrefslogtreecommitdiff
path: root/src/Aspect/Aspect_GenericColorMap.cxx
blob: 6ae4d99b786bce2207cd27d4cd3bd821c2f6d8dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#define IMP080300	//GG Add protection on FindColorMapIndex()

#define IMP060400	//GG Take in account the Hue of the color
//			for computing the nearest color index.

#include <Aspect_GenericColorMap.ixx>
#include <Aspect_ColorMapEntry.hxx>

Aspect_GenericColorMap::Aspect_GenericColorMap ():Aspect_ColorMap (Aspect_TOC_Generic) {
Aspect_ColorMapEntry theDefaultEntry;

    AddEntry(theDefaultEntry);
}

void Aspect_GenericColorMap::AddEntry (const Aspect_ColorMapEntry& AnEntry) {
Standard_Integer index = AnEntry.Index();

    if ( myDataMap.IsBound( index ) ) {
        Standard_Integer i = myDataMap( index ) ;
        mydata.SetValue( i , AnEntry ) ;
    } else {
        mydata.Append( AnEntry ) ;
        myDataMap.Bind( index , mydata.Length() ) ; 
    }
}

Standard_Integer Aspect_GenericColorMap::AddEntry (const Quantity_Color &aColor) {
Aspect_ColorMapEntry theEntry ;
Standard_Integer i,maxindex = 0 ;

    for( i=1 ; i<=mydata.Length() ; i++ ) {
	theEntry = mydata.Value(i) ;
	maxindex = Max(maxindex,theEntry.Index()) ;
	if( theEntry.Color() == aColor ) return theEntry.Index() ;
    }

    maxindex++ ;
    theEntry.SetValue(maxindex,aColor) ;
    mydata.Append( theEntry ) ;
    myDataMap.Bind( maxindex , mydata.Length() ) ; 
    return maxindex ;
}

void Aspect_GenericColorMap::RemoveEntry (const Standard_Integer index) {

   mydata.Remove( index ) ;
}

const Aspect_ColorMapEntry& Aspect_GenericColorMap::NearestEntry( 
				const Quantity_Color&  color ) const

{ 
  return( Entry( NearestColorMapIndex( color ) ) ) ;
}

Standard_Integer Aspect_GenericColorMap::NearestColorMapIndex( 
				const Quantity_Color&  color ) const

{ 

  Standard_Real    dist ;
  struct {
	Standard_Real    dist ;
	Standard_Integer index ;
  } nearest;

  nearest.dist  = 0. ;
  nearest.index = 0 ;

#ifdef IMP060400
  Standard_Integer ehue,hue = (color.Hue() < 0.) ? -1 :
				Standard_Integer(color.Hue())/60;
#endif

  Quantity_Color ecolor;

  if ( Size() == 0 )
  	Aspect_BadAccess::Raise ("NearestColorMapIndex() ColorMap is empty.");

  for ( Standard_Integer i = 1 ; i <= Size() ; i++ ) {
    if ( Entry(i).IsAllocated() ) {
	ecolor = Entry(i).Color();
	dist = color.SquareDistance( ecolor ) ;
#ifdef IMP060400
	ehue = (ecolor.Hue() < 0.) ? -1 : Standard_Integer(ecolor.Hue())/60;
	if ( (nearest.index == 0) || 
		((dist < nearest.dist) && (hue == ehue)) ) {
#else
	if ( nearest.index == 0 || dist < nearest.dist ) {
#endif
		nearest.index = i ;
		nearest.dist  = dist ;
#ifdef IMP060400
		if( dist == 0.0 ) break;
#endif
	}
    }
  }

  if ( nearest.index == 0 )
  	Aspect_BadAccess::Raise ("NearestEntryIndex() ColorMap is empty.");

  return( nearest.index ) ;
}

const Aspect_ColorMapEntry& Aspect_GenericColorMap::FindEntry( 
		const Standard_Integer ColorEntryIndex ) const 

{ 
  return( Entry( FindColorMapIndex( ColorEntryIndex ) ) ) ;
}

Standard_Integer Aspect_GenericColorMap::FindColorMapIndex( 
		const Standard_Integer ColorEntryIndex ) const 
{
Standard_Integer index = 0;
#ifdef IMP080300
  if( myDataMap.IsBound( ColorEntryIndex ) ) 
	index = myDataMap.Find( ColorEntryIndex );
#else
  index = myDataMap ( ColorEntryIndex );
#endif
  return index;
}