QtColorTranslator.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include "QtColorTranslator.hh"
00055 #include <iostream>
00056 #include <assert.h>
00057
00058
00059
00060
00061 namespace ACG {
00062
00063
00064
00065
00066
00067 void
00068 QtColorTranslator::initialize()
00069 {
00070 glGetIntegerv( GL_RED_BITS, &redBits_ );
00071 glGetIntegerv( GL_GREEN_BITS, &greenBits_ );
00072 glGetIntegerv( GL_BLUE_BITS, &blueBits_ );
00073
00074 if (redBits_ > 8) redBits_ = 8;
00075 if (greenBits_ > 8) greenBits_ = 8;
00076 if (blueBits_ > 8) blueBits_ = 8;
00077
00078 redMask_ = ((1 << redBits_) - 1);
00079 greenMask_ = ((1 << greenBits_) - 1);
00080 blueMask_ = ((1 << blueBits_) - 1);
00081 redShift_ = 8 - redBits_;
00082 greenShift_ = 8 - greenBits_;
00083 blueShift_ = 8 - blueBits_;
00084 redRound_ = 1 << (redShift_ - 1);
00085 greenRound_ = 1 << (greenShift_ - 1);
00086 blueRound_ = 1 << (blueShift_ - 1);
00087
00088 initialized_ = true;
00089
00090 if (redBits_ > 8) std::cerr << "error: more than 8 red bits\n";
00091 if (greenBits_ > 8) std::cerr << "error: more than 8 green bits\n";
00092 if (blueBits_ > 8) std::cerr << "error: more than 8 blue bits\n";
00093 }
00094
00095
00096
00097
00098
00099 bool
00100 QtColorTranslator::index2color(unsigned int _idx, QRgb& _col) const
00101 {
00102 assert(initialized());
00103 unsigned char r, g, b;
00104 unsigned int idx(_idx+1);
00105
00106 b = ((idx & blueMask_) << blueShift_) | blueRound_;
00107 idx >>= blueBits_;
00108 g = ((idx & greenMask_) << greenShift_) | greenRound_;
00109 idx >>= greenBits_;
00110 r = ((idx & redMask_) << redShift_) | redRound_;
00111 idx >>= redBits_;
00112
00113 if (!idx)
00114 {
00115 _col = qRgb(r, g, b);
00116 return true;
00117 }
00118 else {
00119 std::cerr << "Can't convert index " << _idx << " to RGB\n";
00120 _col = qRgb(0,0,0);
00121 return false;
00122 }
00123 }
00124
00125
00126
00127
00128
00129 bool
00130 QtColorTranslator::index2color(unsigned int _idx, QRgb& _fc, QRgb& _bc) const
00131 {
00132 assert(initialized());
00133 unsigned char r, g, b;
00134 unsigned int idx(_idx+1);
00135
00136 b = ((idx & blueMask_) << blueShift_) | blueRound_;
00137 idx >>= blueBits_;
00138 g = ((idx & greenMask_) << greenShift_) | greenRound_;
00139 idx >>= greenBits_;
00140 r = ((idx & redMask_) << redShift_) | redRound_;
00141 idx >>= redBits_;
00142
00143 if (!idx)
00144 {
00145 _bc = qRgb(r, g, b);
00146 _fc = qRgb(0,0,0);
00147 return true;
00148 }
00149 else
00150 {
00151 _bc = qRgb(r, g, b);
00152
00153 b = ((idx & blueMask_) << blueShift_) | blueRound_;
00154 idx >>= blueBits_;
00155 g = ((idx & greenMask_) << greenShift_) | greenRound_;
00156 idx >>= greenBits_;
00157 r = ((idx & redMask_) << redShift_) | redRound_;
00158 idx >>= redBits_;
00159
00160 if (!idx)
00161 {
00162 _fc = qRgb(r,g,b);
00163 return true;
00164 }
00165 else
00166 {
00167 std::cerr << "Can't convert index " << _idx << " to RGB\n";
00168 _bc = qRgb(0,0,0);
00169 _fc = qRgb(0,0,0);
00170 return false;
00171 }
00172 }
00173 }
00174
00175
00176
00177
00178
00179 int
00180 QtColorTranslator::color2index(QRgb _c) const
00181 {
00182 assert(initialized());
00183 unsigned int result;
00184
00185 result = qRed(_c) >> redShift_;
00186 result <<= greenBits_;
00187 result |= qGreen(_c) >> greenShift_;
00188 result <<= blueBits_;
00189 result |= qBlue(_c) >> blueShift_;
00190
00191 return (result-1);
00192 }
00193
00194
00195
00196
00197
00198 int
00199 QtColorTranslator::color2index(QRgb _fc, QRgb _bc) const
00200 {
00201 assert(initialized());
00202 unsigned int result;
00203
00204 result = qRed(_fc) >> redShift_;
00205 result <<= greenBits_;
00206 result |= qGreen(_fc) >> greenShift_;
00207 result <<= blueBits_;
00208 result |= qBlue(_fc) >> blueShift_;
00209
00210 result <<= redBits_;
00211 result |= qRed(_bc) >> redShift_;
00212 result <<= greenBits_;
00213 result |= qGreen(_bc) >> greenShift_;
00214 result <<= blueBits_;
00215 result |= qBlue(_bc) >> blueShift_;
00216
00217 return (result-1);
00218 }
00219
00220
00221
00222
00223
00224 unsigned int
00225 QtColorTranslator::maxIndex() const
00226 {
00227 assert(initialized());
00228 unsigned int result(~0);
00229 result >>= 32 - redBits_ - greenBits_ - blueBits_;
00230 return (result-1);
00231 }
00232
00233
00234
00235 }
00236
00237