0024428: Implementation of LGPL license
[occt.git] / src / Aspect / Aspect_ColorCubeColorMap.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
7 // under the terms of the GNU Lesser General Public 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 // Modified : GG ; 14/09/01 Implements the new AddEntry method
16
17 #include <Aspect_ColorCubeColorMap.ixx>
18 #include <Aspect_ColorMapEntry.hxx>
19
20 #ifdef TRACE
21 static int Tlevel = 0 ;
22 #endif
23
24 Aspect_ColorCubeColorMap::Aspect_ColorCubeColorMap( 
25         const Standard_Integer basepixel ,
26         const Standard_Integer red_max ,
27         const Standard_Integer red_mult ,
28         const Standard_Integer green_max ,
29         const Standard_Integer green_mult ,
30         const Standard_Integer blue_max ,
31         const Standard_Integer blue_mult )
32         :Aspect_ColorMap( Aspect_TOC_ColorCube )
33
34 { Standard_Integer      r,g,b,i ;
35   Aspect_ColorMapEntry value ;
36   Quantity_Color        rgb ;
37   Standard_Integer      max[3], mult[3], ti[3];
38
39   mybasepixel = basepixel ;
40   myredmax    = red_max ;
41   myredmult   = red_mult ;
42   mygreenmax  = green_max ;
43   mygreenmult = green_mult ;
44   mybluemax   = blue_max ;
45   mybluemult  = blue_mult ;
46
47   r = 0 ; g = 1 ; b = 2 ;
48
49   if ( ( myredmult < mygreenmult ) && ( myredmult < mybluemult ) ) {
50         r = 0 ; 
51         if ( mygreenmult < mybluemult ) { g = 1 ; b = 2 ; }
52         else                            { b = 1 ; g = 2 ; }
53   }
54   if ( ( mygreenmult < myredmult ) && ( mygreenmult < mybluemult ) ) {
55         g = 0 ; 
56         if ( myredmult < mybluemult ) { r = 1 ; b = 2 ; }
57         else                          { b = 1 ; r = 2 ; }
58   }
59   if ( ( mybluemult < myredmult ) && ( mybluemult < mygreenmult ) ) {
60         b = 0 ; 
61         if ( myredmult < mygreenmult ) { r = 1 ; g = 2 ; }
62         else                           { g = 1 ; r = 2 ; }
63   }
64
65 #ifdef TRACE
66   if ( Tlevel ) 
67         cout << "ColorCubeColorMap constructor (r,g,b) :" 
68                 << r << "," << g << "," << b << endl << flush ;
69 #endif
70
71   mult[r] = myredmult ;         max[r] = myredmax ;
72   mult[g] = mygreenmult ;       max[g] = mygreenmax ;
73   mult[b] = mybluemult ;        max[b] = mybluemax ;
74
75   for( ti[2] = 0 ; ti[2] <= max[2] ; ti[2]++ ) {
76     for( ti[1] = 0 ; ti[1] <= max[1] ; ti[1]++ ) {
77       for( ti[0] = 0 ; ti[0] <= max[0] ; ti[0]++ ) {
78         rgb.SetValues( (Standard_Real) ti[r] / (Standard_Real) max[r],
79                        (Standard_Real) ti[g] / (Standard_Real) max[g],
80                        (Standard_Real) ti[b] / (Standard_Real) max[b],
81                         Quantity_TOC_RGB ) ;
82
83         i = ti[r] * mult[r] + ti[g] * mult[g] + ti[b] * mult[b] ;
84
85         value.SetValue( mybasepixel+i, rgb ) ;
86 #ifdef TRACE
87   if ( Tlevel ) 
88         value.Dump() ;
89 #endif
90         mydata.Append( value ) ;
91       }
92     }
93   }
94 }
95
96 const Aspect_ColorMapEntry& Aspect_ColorCubeColorMap::NearestEntry( 
97                                 const Quantity_Color&  color ) const
98
99
100   return( Entry( NearestColorMapIndex( color ) ) ) ;
101 }
102
103 Standard_Integer Aspect_ColorCubeColorMap::NearestColorMapIndex( 
104                                         const Quantity_Color&  color ) const
105
106 { Standard_Integer      r,g,b ;
107
108   r = (Standard_Integer ) ( color.Red()   * myredmax   + 0.5) ;
109   g = (Standard_Integer ) ( color.Green() * mygreenmax + 0.5) ;
110   b = (Standard_Integer ) ( color.Blue()  * mybluemax  + 0.5) ;
111
112   return( r*myredmult+g*mygreenmult+b*mybluemult + 1 ) ;
113 }
114
115 const Aspect_ColorMapEntry& Aspect_ColorCubeColorMap::FindEntry( 
116                 const Standard_Integer index ) const 
117
118
119   return( Entry( FindColorMapIndex( index ) ) ) ;
120 }
121
122 Standard_Integer Aspect_ColorCubeColorMap::FindColorMapIndex( 
123                 const Standard_Integer index ) const 
124 {
125
126   if (  index < mybasepixel || 
127         index >= ( mybasepixel+Size() ) ){
128          Aspect_BadAccess::Raise ("FindEntryIndex() index not found.");
129   }
130
131   return( index - mybasepixel + 1 ) ;
132 }
133
134 void Aspect_ColorCubeColorMap::ColorCubeDefinition(
135          Standard_Integer& basepixel ,
136          Standard_Integer& red_max ,
137          Standard_Integer& red_mult ,
138          Standard_Integer& green_max ,
139          Standard_Integer& green_mult ,
140          Standard_Integer& blue_max ,
141          Standard_Integer& blue_mult ) const
142
143 {
144   basepixel = mybasepixel ;
145   red_max    = myredmax ;
146   red_mult   = myredmult ;
147   green_max  = mygreenmax ;
148   green_mult = mygreenmult ;
149   blue_max   = mybluemax ;
150   blue_mult  = mybluemult ;
151 }
152
153 Standard_Integer Aspect_ColorCubeColorMap::AddEntry (const Quantity_Color &aColor) {
154
155     return mybasepixel + NearestColorMapIndex(aColor) - 1;
156 }