Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QtColorTranslator.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS QtColorTranslator - IMPLEMENTATION
55 //
56 //=============================================================================
57 
58 
59 //== INCLUDES =================================================================
60 
61 #include "QtColorTranslator.hh"
62 #include <iostream>
63 #include <cassert>
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 namespace ACG {
69 
70 
71 //== IMPLEMENTATION ==========================================================
72 
73 
74 void
76 {
77  glGetIntegerv( GL_RED_BITS, &redBits_ );
78  glGetIntegerv( GL_GREEN_BITS, &greenBits_ );
79  glGetIntegerv( GL_BLUE_BITS, &blueBits_ );
80 
81  if (redBits_ > 8) redBits_ = 8;
82  if (greenBits_ > 8) greenBits_ = 8;
83  if (blueBits_ > 8) blueBits_ = 8;
84 
85  redMask_ = ((1 << redBits_) - 1);
86  greenMask_ = ((1 << greenBits_) - 1);
87  blueMask_ = ((1 << blueBits_) - 1);
88  redShift_ = 8 - redBits_;
89  greenShift_ = 8 - greenBits_;
90  blueShift_ = 8 - blueBits_;
91  redRound_ = 1 << (redShift_ - 1);
92  greenRound_ = 1 << (greenShift_ - 1);
93  blueRound_ = 1 << (blueShift_ - 1);
94 
95  initialized_ = true;
96 
97  if (redBits_ > 8) std::cerr << "error: more than 8 red bits\n";
98  if (greenBits_ > 8) std::cerr << "error: more than 8 green bits\n";
99  if (blueBits_ > 8) std::cerr << "error: more than 8 blue bits\n";
100 }
101 
102 
103 //-----------------------------------------------------------------------------
104 
105 
106 bool
107 QtColorTranslator::index2color(unsigned int _idx, QRgb& _col) const
108 {
109  assert(initialized());
110  unsigned char r, g, b;
111  unsigned int idx(_idx+1);
112 
113  b = ((idx & blueMask_) << blueShift_) | blueRound_;
114  idx >>= blueBits_;
115  g = ((idx & greenMask_) << greenShift_) | greenRound_;
116  idx >>= greenBits_;
117  r = ((idx & redMask_) << redShift_) | redRound_;
118  idx >>= redBits_;
119 
120  if (!idx)
121  {
122  _col = qRgb(r, g, b);
123  return true;
124  }
125  else {
126  std::cerr << "Can't convert index " << _idx << " to RGB\n";
127  _col = qRgb(0,0,0);
128  return false;
129  }
130 }
131 
132 
133 //-----------------------------------------------------------------------------
134 
135 
136 bool
137 QtColorTranslator::index2color(unsigned int _idx, QRgb& _fc, QRgb& _bc) const
138 {
139  assert(initialized());
140  unsigned char r, g, b;
141  unsigned int idx(_idx+1);
142 
143  b = ((idx & blueMask_) << blueShift_) | blueRound_;
144  idx >>= blueBits_;
145  g = ((idx & greenMask_) << greenShift_) | greenRound_;
146  idx >>= greenBits_;
147  r = ((idx & redMask_) << redShift_) | redRound_;
148  idx >>= redBits_;
149 
150  if (!idx)
151  {
152  _bc = qRgb(r, g, b);
153  _fc = qRgb(0,0,0);
154  return true;
155  }
156  else
157  {
158  _bc = qRgb(r, g, b);
159 
160  b = ((idx & blueMask_) << blueShift_) | blueRound_;
161  idx >>= blueBits_;
162  g = ((idx & greenMask_) << greenShift_) | greenRound_;
163  idx >>= greenBits_;
164  r = ((idx & redMask_) << redShift_) | redRound_;
165  idx >>= redBits_;
166 
167  if (!idx)
168  {
169  _fc = qRgb(r,g,b);
170  return true;
171  }
172  else
173  {
174  std::cerr << "Can't convert index " << _idx << " to RGB\n";
175  _bc = qRgb(0,0,0);
176  _fc = qRgb(0,0,0);
177  return false;
178  }
179  }
180 }
181 
182 
183 //-----------------------------------------------------------------------------
184 
185 
186 int
188 {
189  assert(initialized());
190  unsigned int result;
191 
192  result = qRed(_c) >> redShift_;
193  result <<= greenBits_;
194  result |= qGreen(_c) >> greenShift_;
195  result <<= blueBits_;
196  result |= qBlue(_c) >> blueShift_;
197 
198  return (result-1);
199 }
200 
201 
202 //-----------------------------------------------------------------------------
203 
204 
205 int
206 QtColorTranslator::color2index(QRgb _fc, QRgb _bc) const
207 {
208  assert(initialized());
209  unsigned int result;
210 
211  result = qRed(_fc) >> redShift_;
212  result <<= greenBits_;
213  result |= qGreen(_fc) >> greenShift_;
214  result <<= blueBits_;
215  result |= qBlue(_fc) >> blueShift_;
216 
217  result <<= redBits_;
218  result |= qRed(_bc) >> redShift_;
219  result <<= greenBits_;
220  result |= qGreen(_bc) >> greenShift_;
221  result <<= blueBits_;
222  result |= qBlue(_bc) >> blueShift_;
223 
224  return (result-1);
225 }
226 
227 
228 //-----------------------------------------------------------------------------
229 
230 
231 unsigned int
233 {
234  assert(initialized());
235  unsigned int result(~0);
236  result >>= 32 - redBits_ - greenBits_ - blueBits_;
237  return (result-1);
238 }
239 
240 
241 //=============================================================================
242 } // namespace ACG
243 //=============================================================================
244 
bool initialized() const
has it been initialized?
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
int color2index(QRgb _c) const
color -> index (one buffer)
void initialize()
init (takes current QGLcontext)
unsigned int maxIndex() const
returns max convertable index (using ONE buffer)
bool index2color(unsigned int _idx, QRgb &_col) const
index -> color (one buffer)