2be89c61f37c18638ab70c8483b90efa76b59070
[occt.git] / src / MeshVS / MeshVS_TwoColors.cxx
1 // Created on: 2003-10-08
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <MeshVS_TwoColors.hxx>
17
18
19 //================================================================
20 // Function : HashCode
21 // Purpose  :
22 //================================================================
23 Standard_Integer HashCode ( const MeshVS_TwoColors&       theKey,
24                             const Standard_Integer theUpper)
25 {
26 #define MESHPRS_HASH_BYTE(val) { \
27     aHash += (val);              \
28     aHash += (aHash << 10);      \
29     aHash ^= (aHash >> 6);       \
30   }
31   Standard_Integer aHash = 0;
32   MESHPRS_HASH_BYTE (theKey.r1)
33   MESHPRS_HASH_BYTE (theKey.g1)
34   MESHPRS_HASH_BYTE (theKey.b1)
35   MESHPRS_HASH_BYTE (theKey.r2)
36   MESHPRS_HASH_BYTE (theKey.g2)
37   MESHPRS_HASH_BYTE (theKey.b2)
38   aHash += (aHash << 3);
39   aHash ^= (aHash >> 11);
40   aHash += (aHash << 15);
41   return (( aHash & 0x7fffffff ) % theUpper) + 1;
42 #undef MESHPRS_HASH_BYTE
43 }
44
45 //================================================================
46 // Function : IsEqual
47 // Purpose  :
48 //================================================================
49 Standard_Boolean IsEqual (const MeshVS_TwoColors& K1,
50                           const MeshVS_TwoColors& K2)
51 {
52   return (((K1.r1 * 256 + K1.g1) * 256 + K1.b1) ==
53           ((K2.r1 * 256 + K2.g1) * 256 + K2.b1) &&
54           ((K1.r2 * 256 + K1.g2) * 256 + K1.b2) ==
55           ((K2.r2 * 256 + K2.g2) * 256 + K2.b2));
56 }
57
58 //================================================================
59 // Function : operator ==
60 // Purpose  :
61 //================================================================
62 Standard_Boolean operator== ( const MeshVS_TwoColors& K1,
63                               const MeshVS_TwoColors& K2  )
64 {
65   return IsEqual ( K1, K2 );
66 }
67
68 //================================================================
69 // Function : BindTwoColors
70 // Purpose  :
71 //================================================================
72 MeshVS_TwoColors BindTwoColors ( const Quantity_Color& theCol1, const Quantity_Color& theCol2 )
73 {
74   MeshVS_TwoColors aRes;
75
76   aRes.r1 = unsigned ( theCol1.Red()   * 255.0 );
77   aRes.g1 = unsigned ( theCol1.Green() * 255.0 );
78   aRes.b1 = unsigned ( theCol1.Blue()  * 255.0 );
79   aRes.r2 = unsigned ( theCol2.Red()   * 255.0 );
80   aRes.g2 = unsigned ( theCol2.Green() * 255.0 );
81   aRes.b2 = unsigned ( theCol2.Blue()  * 255.0 );
82
83   return aRes;
84 }
85
86 //================================================================
87 // Function : ExtractColor
88 // Purpose  :
89 //================================================================
90 Quantity_Color ExtractColor ( MeshVS_TwoColors& theTwoColors, const Standard_Integer Index )
91 {
92   Quantity_Color aRes;
93   Standard_Real max = 255.0;
94
95   if ( Index == 1 )
96     aRes.SetValues ( Standard_Real (theTwoColors.r1) / max,
97                      Standard_Real (theTwoColors.g1) / max,
98                      Standard_Real (theTwoColors.b1) / max, Quantity_TOC_RGB );
99   else if (Index == 2)
100     aRes.SetValues ( Standard_Real (theTwoColors.r2) / max,
101                      Standard_Real (theTwoColors.g2) / max,
102                      Standard_Real (theTwoColors.b2) / max, Quantity_TOC_RGB );
103
104   return aRes;
105 }
106
107 //================================================================
108 // Function : ExtractColors
109 // Purpose  :
110 //================================================================
111 void ExtractColors ( MeshVS_TwoColors& theTwoColors, Quantity_Color& theCol1, Quantity_Color& theCol2 )
112 {
113   Standard_Real max = 255.0;
114   theCol1.SetValues ( Standard_Real (theTwoColors.r1) / max,
115                       Standard_Real (theTwoColors.g1) / max,
116                       Standard_Real (theTwoColors.b1) / max, Quantity_TOC_RGB );
117   theCol2.SetValues ( Standard_Real (theTwoColors.r2) / max,
118                       Standard_Real (theTwoColors.g2) / max,
119                       Standard_Real (theTwoColors.b2) / max, Quantity_TOC_RGB );
120 }