decdee7d |
1 | // Created on: 2016-12-13 |
2 | // Copyright (c) 2016 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 | #ifndef _Quantity_ColorHasher_HeaderFile |
16 | #define _Quantity_ColorHasher_HeaderFile |
17 | |
18 | #include <Quantity_Color.hxx> |
19 | |
20 | //! Hasher of Quantity_Color. |
21 | struct Quantity_ColorHasher |
22 | { |
2b2be3fb |
23 | //! Returns hash code for the given RGB color, in the range [1, theUpperBound] |
24 | //! @param theColor the RGB color object which hash code is to be computed |
25 | //! @param theUpperBound the upper bound of the range a computing range must be within |
26 | //! @return a computed hash code, in the range [1, theUpperBound] |
decdee7d |
27 | static Standard_Integer HashCode (const Quantity_Color& theColor, |
2b2be3fb |
28 | const Standard_Integer theUpperBound) |
decdee7d |
29 | { |
30 | Standard_Integer aRed = Standard_Integer (255 * theColor.Red()); |
31 | Standard_Integer aGreen = Standard_Integer (255 * theColor.Green()); |
32 | Standard_Integer aBlue = Standard_Integer (255 * theColor.Blue()); |
33 | |
2b2be3fb |
34 | unsigned int aHash = 0; |
35 | |
decdee7d |
36 | updateHash (aHash, aRed); |
37 | updateHash (aHash, aGreen); |
38 | updateHash (aHash, aBlue); |
39 | aHash += (aHash << 3); |
40 | aHash ^= (aHash >> 11); |
41 | aHash += (aHash << 15); |
2b2be3fb |
42 | |
43 | return IntegerHashCode(aHash, 0x7fff, theUpperBound); |
decdee7d |
44 | } |
45 | |
46 | //! Returns true if two colors are equal. |
47 | static Standard_Boolean IsEqual (const Quantity_Color& theColor1, |
48 | const Quantity_Color& theColor2) |
49 | { |
50 | return theColor1 == theColor2; |
51 | } |
52 | |
53 | protected: |
2b2be3fb |
54 | static void updateHash (unsigned int& theHash, const Standard_Integer theValue) |
decdee7d |
55 | { |
56 | theHash += theValue; |
57 | theHash += (theHash << 10); |
58 | theHash ^= (theHash >> 6); |
59 | } |
60 | }; |
61 | |
62 | #endif |