Developer Documentation

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
45
46 //=============================================================================
47 //
48 // CLASS QtColorTranslator - IMPLEMENTATION
49 //
50 //=============================================================================
51
52
53 //== INCLUDES =================================================================
54
55 #include "QtColorTranslator.hh"
56 #include <iostream>
57 #include <cassert>
58
59
60 //== NAMESPACES ===============================================================
61
62 namespace ACG {
63
64
65 //== IMPLEMENTATION ==========================================================
66
67
68 void
69 QtColorTranslator::initialize()
70 {
71 glGetIntegerv( GL_RED_BITS, &redBits_ );
72 glGetIntegerv( GL_GREEN_BITS, &greenBits_ );
73 glGetIntegerv( GL_BLUE_BITS, &blueBits_ );
74
75 if (redBits_ > 8) redBits_ = 8;
76 if (greenBits_ > 8) greenBits_ = 8;
77 if (blueBits_ > 8) blueBits_ = 8;
78
79 redMask_ = ((1 << redBits_) - 1);
80 greenMask_ = ((1 << greenBits_) - 1);
81 blueMask_ = ((1 << blueBits_) - 1);
82 redShift_ = 8 - redBits_;
83 greenShift_ = 8 - greenBits_;
84 blueShift_ = 8 - blueBits_;
85 redRound_ = 1 << (redShift_ - 1);
86 greenRound_ = 1 << (greenShift_ - 1);
87 blueRound_ = 1 << (blueShift_ - 1);
88
89 initialized_ = true;
90
91 if (redBits_ > 8) std::cerr << "error: more than 8 red bits\n";
92 if (greenBits_ > 8) std::cerr << "error: more than 8 green bits\n";
93 if (blueBits_ > 8) std::cerr << "error: more than 8 blue bits\n";
94 }
95
96
97 //-----------------------------------------------------------------------------
98
99
100 bool
101 QtColorTranslator::index2color(unsigned int _idx, QRgb& _col) const
102 {
103 assert(initialized());
104 unsigned char r, g, b;
105 unsigned int idx(_idx+1);
106
107 b = ((idx & blueMask_) << blueShift_) | blueRound_;
108 idx >>= blueBits_;
109 g = ((idx & greenMask_) << greenShift_) | greenRound_;
110 idx >>= greenBits_;
111 r = ((idx & redMask_) << redShift_) | redRound_;
112 idx >>= redBits_;
113
114 if (!idx)
115 {
116 _col = qRgb(r, g, b);
117 return true;
118 }
119 else {
120 std::cerr << "Can't convert index " << _idx << " to RGB\n";
121 _col = qRgb(0,0,0);
122 return false;
123 }
124 }
125
126
127 //-----------------------------------------------------------------------------
128
129
130 bool
131 QtColorTranslator::index2color(unsigned int _idx, QRgb& _fc, QRgb& _bc) const
132 {
133 assert(initialized());
134 unsigned char r, g, b;
135 unsigned int idx(_idx+1);
136
137 b = ((idx & blueMask_) << blueShift_) | blueRound_;
138 idx >>= blueBits_;
139 g = ((idx & greenMask_) << greenShift_) | greenRound_;
140 idx >>= greenBits_;
141 r = ((idx & redMask_) << redShift_) | redRound_;
142 idx >>= redBits_;
143
144 if (!idx)
145 {
146 _bc = qRgb(r, g, b);
147 _fc = qRgb(0,0,0);
148 return true;
149 }
150 else
151 {
152 _bc = qRgb(r, g, b);
153
154 b = ((idx & blueMask_) << blueShift_) | blueRound_;
155 idx >>= blueBits_;
156 g = ((idx & greenMask_) << greenShift_) | greenRound_;
157 idx >>= greenBits_;
158 r = ((idx & redMask_) << redShift_) | redRound_;
159 idx >>= redBits_;
160
161 if (!idx)
162 {
163 _fc = qRgb(r,g,b);
164 return true;
165 }
166 else
167 {
168 std::cerr << "Can't convert index " << _idx << " to RGB\n";
169 _bc = qRgb(0,0,0);
170 _fc = qRgb(0,0,0);
171 return false;
172 }
173 }
174 }
175
176
177 //-----------------------------------------------------------------------------
178
179
180 int
181 QtColorTranslator::color2index(QRgb _c) const
182 {
183 assert(initialized());
184 unsigned int result;
185
186 result = qRed(_c) >> redShift_;
187 result <<= greenBits_;
188 result |= qGreen(_c) >> greenShift_;
189 result <<= blueBits_;
190 result |= qBlue(_c) >> blueShift_;
191
192 return (result-1);
193 }
194
195
196 //-----------------------------------------------------------------------------
197
198
199 int
200 QtColorTranslator::color2index(QRgb _fc, QRgb _bc) const
201 {
202 assert(initialized());
203 unsigned int result;
204
205 result = qRed(_fc) >> redShift_;
206 result <<= greenBits_;
207 result |= qGreen(_fc) >> greenShift_;
208 result <<= blueBits_;
209 result |= qBlue(_fc) >> blueShift_;
210
211 result <<= redBits_;
212 result |= qRed(_bc) >> redShift_;
213 result <<= greenBits_;
214 result |= qGreen(_bc) >> greenShift_;
215 result <<= blueBits_;
216 result |= qBlue(_bc) >> blueShift_;
217
218 return (result-1);
219 }
220
221
222 //-----------------------------------------------------------------------------
223
224
225 unsigned int
226 QtColorTranslator::maxIndex() const
227 {
228 assert(initialized());
229 unsigned int result(~0);
230 result >>= 32 - redBits_ - greenBits_ - blueBits_;
231 return (result-1);
232 }
233
234
235 //=============================================================================
236 } // namespace ACG
237 //=============================================================================
238
ACG::QtColorTranslator::index2color
bool index2color(unsigned int _idx, QRgb &_col) const
index -> color (one buffer)
Definition: QtColorTranslator.cc:101
ACG::QtColorTranslator::maxIndex
unsigned int maxIndex() const
returns max convertable index (using ONE buffer)
Definition: QtColorTranslator.cc:226
ACG
Namespace providing different geometric functions concerning angles.
Definition: BaseObjectData.hh:68
ACG::QtColorTranslator::initialized
bool initialized() const
has it been initialized?
Definition: QtColorTranslator.hh:108
ACG::QtColorTranslator::initialize
void initialize()
init (takes current QGLcontext)
Definition: QtColorTranslator.cc:69
ACG::QtColorTranslator::color2index
int color2index(QRgb _c) const
color -> index (one buffer)
Definition: QtColorTranslator.cc:181