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 OSG_BEGIN_NAMESPACE
00040
00041 template <class ValueTypeT>
00042 const Color3<ValueTypeT> Color3<ValueTypeT>::Null;
00043
00044
00045 template <class ValueTypeT> inline
00046 void Color3<ValueTypeT>::convertFromHSV( ValueType *rgbP,
00047 const Real32 h,
00048 const Real32 s,
00049 const Real32 v)
00050 {
00051 if(rgbP == NULL)
00052 return;
00053
00054 Int32 i;
00055 Real32 f;
00056 Real32 p;
00057 Real32 q;
00058 Real32 t;
00059
00060 if(s)
00061 {
00062 f = (h == 360) ? 0.0 : (h / 60.0);
00063 i = Int32(f);
00064
00065 f = f - Real32(i);
00066
00067 p = v * (1.0 - s );
00068 q = v * (1.0 - (s * f) );
00069 t = v * (1.0 - (s * (1 - f)));
00070
00071 switch (i)
00072 {
00073 case 0:
00074 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(v);
00075 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(t);
00076 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(p);
00077 break;
00078 case 1:
00079 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(q);
00080 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(v);
00081 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(p);
00082 break;
00083 case 2:
00084 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(p);
00085 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(v);
00086 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(t);
00087 break;
00088 case 3:
00089 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(p);
00090 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(q);
00091 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(v);
00092 break;
00093 case 4:
00094 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(t);
00095 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(p);
00096 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(v);
00097 break;
00098 case 5:
00099 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(v);
00100 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(p);
00101 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(q);
00102 break;
00103 default:
00104 std::cerr << "ERROR i not in [0, 5] in Color::setHSV()!"
00105 << std::endl;
00106 break;
00107 }
00108 }
00109 else
00110 {
00111 rgbP[0] = rgbP[1] = rgbP[2] =
00112 TypeTraits<ValueTypeT>::getPortion(v);
00113 }
00114 }
00115
00116
00117 template <class ValueTypeT> inline
00118 void Color3<ValueTypeT>::convertToHSV(const ValueType *rgbP,
00119 Real32 &h,
00120 Real32 &s,
00121 Real32 &v)
00122 {
00123 if(rgbP == NULL)
00124 return;
00125
00126 const Real32 r = TypeTraits<ValueTypeT>::getFraction(rgbP[0]);
00127 const Real32 g = TypeTraits<ValueTypeT>::getFraction(rgbP[1]);
00128 const Real32 b = TypeTraits<ValueTypeT>::getFraction(rgbP[2]);
00129
00130 const Int32 maxIndex = maxPart(rgbP);
00131 const Int32 minIndex = minPart(rgbP);
00132
00133 const Real32 max = TypeTraits<ValueTypeT>::getFraction(
00134 rgbP[maxIndex]);
00135
00136 const Real32 min = TypeTraits<ValueTypeT>::getFraction(
00137 rgbP[minIndex]);
00138
00139 const Real32 delta = max - min;
00140
00141 v = max;
00142 s = max ? (max - min) / max : 0.0;
00143
00144 if(s)
00145 {
00146 switch(maxIndex)
00147 {
00148 case 0:
00149 h = (( g - b) / delta) * 60.0;
00150 break;
00151 case 1:
00152 h = (2.0 + (( b - r) / delta)) * 60.0;
00153 break;
00154 case 2:
00155 h = (4.0 + (( r - g) / delta)) * 60.0;
00156 break;
00157 }
00158
00159 if(h < 0.0)
00160 h += 360.0;
00161 }
00162 else
00163 {
00164 h = 0.0;
00165 }
00166 }
00167
00168
00169 template <class ValueTypeT> inline
00170 UInt32 Color3<ValueTypeT>::maxPart(const ValueType *rgbP)
00171 {
00172 if(rgbP[0] > rgbP[1])
00173 {
00174 if(rgbP[2] > rgbP[0])
00175 {
00176 return 2;
00177 }
00178 else
00179 {
00180 return 0;
00181 }
00182 }
00183 else
00184 {
00185 if (rgbP[2] > rgbP[1])
00186 {
00187 return 2;
00188 }
00189 else
00190 {
00191 return 1;
00192 }
00193 }
00194 }
00195
00196
00197 template <class ValueTypeT> inline
00198 UInt32 Color3<ValueTypeT>::minPart(const ValueType *rgbP)
00199 {
00200 if(rgbP[0] < rgbP[1])
00201 {
00202 if(rgbP[2] < rgbP[0])
00203 {
00204 return 2;
00205 }
00206 else
00207 {
00208 return 0;
00209 }
00210 }
00211 else
00212 {
00213 if(rgbP[2] < rgbP[1])
00214 {
00215 return 2;
00216 }
00217 else
00218 {
00219 return 1;
00220 }
00221 }
00222 }
00223
00224 template <class ValueTypeT> inline
00225 Color3<ValueTypeT>::Color3(void)
00226 {
00227 setValues(TypeTraits<ValueTypeT>::getZeroElement(),
00228 TypeTraits<ValueTypeT>::getZeroElement(),
00229 TypeTraits<ValueTypeT>::getZeroElement());
00230 }
00231
00232
00233 template <class ValueTypeT> inline
00234 Color3<ValueTypeT>::Color3(const
00235 Vector<ValueTypeT, 3> &source) :
00236 Inherited(source)
00237 {
00238 }
00239
00240
00241 template <class ValueTypeT> inline
00242 Color3<ValueTypeT>::Color3(const ValueType red,
00243 const ValueType green,
00244 const ValueType blue)
00245 {
00246 setValues(red, green, blue);
00247 }
00248
00249
00250 template <class ValueTypeT> inline
00251 Color3<ValueTypeT>::~Color3(void)
00252 {
00253 }
00254
00255 template <class ValueTypeT> inline
00256 void Color3<ValueTypeT>::clear(void)
00257 {
00258 setValues(TypeTraits<ValueTypeT>::getZeroElement(),
00259 TypeTraits<ValueTypeT>::getZeroElement(),
00260 TypeTraits<ValueTypeT>::getZeroElement());
00261 }
00262
00263
00264 template <class ValueTypeT> inline
00265 void Color3<ValueTypeT>::setValuesRGB(const ValueType red,
00266 const ValueType green,
00267 const ValueType blue)
00268 {
00269 setValues(red, green, blue);
00270 }
00271
00272
00273 template <class ValueTypeT> inline
00274 void Color3<ValueTypeT>::setValuesHSV(const Real32 h,
00275 const Real32 s,
00276 const Real32 v)
00277 {
00278 convertFromHSV(this->getValues(), h, s, v);
00279 }
00280
00281
00282 template <class ValueTypeT> inline
00283 void Color3<ValueTypeT>::setRandom(void)
00284 {
00285 Real32 rf = 1.0 / Real32(RAND_MAX);
00286
00287 setValuesRGB(TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00288 TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00289 TypeTraits<ValueTypeT>::getPortion(rf * rand()));
00290 }
00291
00295 template <class ValueTypeT> inline
00296 void Color3<ValueTypeT>::setRGB(UInt32 rgbPack)
00297 {
00298 for(Int32 i = 0; i < 3; ++i)
00299 {
00300 Real32 rTmp = Real32(rgbPack & 255) / 255.0f;
00301
00302 (*this)[i] = TypeTraits<ValueTypeT>::getPortion(rTmp);
00303
00304 rgbPack >>= 8;
00305 }
00306 }
00307
00308
00309 template <class ValueTypeT> inline
00310 void Color3<ValueTypeT>::setValue(const Char8 *szString)
00311 {
00312 this->setValueFromCString(szString);
00313 }
00314
00315
00319 template <class ValueTypeT> inline
00320 UInt32 Color3<ValueTypeT>::getRGB(void) const
00321 {
00322 UInt32 pack = 0;
00323
00324 for(Int32 i = 2; i >= 0; --i)
00325 {
00326 pack = (pack << 8) |
00327 Int32(TypeTraits<ValueTypeT>::getFraction((*this)[i]) *
00328 255.0f +
00329 0.5f );
00330 }
00331
00332 return pack;
00333 }
00334
00335
00336 template <class ValueTypeT> inline
00337 void Color3<ValueTypeT>::getValuesRGB(ValueType &red,
00338 ValueType &green,
00339 ValueType &blue) const
00340 {
00341 red = (*this)[0];
00342 green = (*this)[1];
00343 blue = (*this)[2];
00344 }
00345
00346
00347 template <class ValueTypeT> inline
00348 void Color3<ValueTypeT>::getValuesHSV(Real32 &h,
00349 Real32 &s,
00350 Real32 &v) const
00351 {
00352 convertToHSV(this->getValues(), h, s, v);
00353 }
00354
00355
00356 template <class ValueTypeT> inline
00357 typename Color3<ValueTypeT>::ValueType
00358 Color3<ValueTypeT>::red(void) const
00359 {
00360 return (*this)[0];
00361 }
00362
00363 template <class ValueTypeT> inline
00364 typename Color3<ValueTypeT>::ValueType
00365 Color3<ValueTypeT>::green(void) const
00366 {
00367 return (*this)[1];
00368 }
00369
00370 template <class ValueTypeT> inline
00371 typename Color3<ValueTypeT>::ValueType
00372 Color3<ValueTypeT>::blue(void) const
00373 {
00374 return (*this)[2];
00375 }
00376
00377
00378 template <class ValueTypeT> inline
00379 typename Color3<ValueTypeT>::ValueType *
00380 Color3<ValueTypeT>::getValuesRGB(void)
00381 {
00382 return this->getValues();
00383 }
00384
00385 template <class ValueTypeT> inline
00386 const typename Color3<ValueTypeT>::ValueType *
00387 Color3<ValueTypeT>::getValuesRGB(void) const
00388 {
00389 return this->getValues();
00390 }
00391
00392
00393
00394
00395 template <class ValueTypeT>
00396 const Color4<ValueTypeT> Color4<ValueTypeT>::Null;
00397
00398
00399 template <class ValueTypeT> inline
00400 Color4<ValueTypeT>::Color4(void)
00401 {
00402 setValues(TypeTraits<ValueTypeT>::getZeroElement(),
00403 TypeTraits<ValueTypeT>::getZeroElement(),
00404 TypeTraits<ValueTypeT>::getZeroElement(),
00405 TypeTraits<ValueTypeT>::getZeroElement());
00406 }
00407
00408
00409 template <class ValueTypeT> inline
00410 Color4<ValueTypeT>::Color4(const
00411 Vector<ValueTypeT, 4> &source) :
00412 Inherited(source)
00413 {
00414 }
00415
00416
00417 template <class ValueTypeT> inline
00418 Color4<ValueTypeT>::Color4(const ValueType red,
00419 const ValueType green,
00420 const ValueType blue,
00421 const ValueType alpha)
00422 {
00423 setValues(red, green, blue, alpha);
00424 }
00425
00426
00427 template <class ValueTypeT> inline
00428 Color4<ValueTypeT>::~Color4(void)
00429 {
00430 }
00431
00432 template <class ValueTypeT> inline
00433 void Color4<ValueTypeT>::clear(void)
00434 {
00435 setValues(TypeTraits<ValueTypeT>::getZeroElement(),
00436 TypeTraits<ValueTypeT>::getZeroElement(),
00437 TypeTraits<ValueTypeT>::getZeroElement(),
00438 TypeTraits<ValueTypeT>::getZeroElement());
00439 }
00440
00441
00442 template <class ValueTypeT> inline
00443 void Color4<ValueTypeT>::setValuesRGBA(const ValueType red,
00444 const ValueType green,
00445 const ValueType blue,
00446 const ValueType alpha)
00447 {
00448 setValues(red, green, blue, alpha);
00449 }
00450
00451 template <class ValueTypeT> inline
00452 void Color4<ValueTypeT>::setValuesHSV(const Real32 h,
00453 const Real32 s,
00454 const Real32 v)
00455 {
00456 Color3<ValueType>::convertFromHSV(this->getValues(), h, s, v);
00457
00458 (*this)[3] = TypeTraits<ValueTypeT>::getOneElement();
00459 }
00460
00461
00462 template <class ValueTypeT> inline
00463 void Color4<ValueTypeT>::setRandom(void)
00464 {
00465 Real32 rf = 1.0 / Real32(RAND_MAX);
00466
00467 setValuesRGBA(TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00468 TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00469 TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00470 TypeTraits<ValueTypeT>::getPortion(rf * rand()));
00471 }
00472
00476 template <class ValueTypeT> inline
00477 void Color4<ValueTypeT>::setRGBA(UInt32 rgbPack)
00478 {
00479 for(Int32 i = 0; i < 4; ++i)
00480 {
00481 Real32 rTmp = Real32(rgbPack & 255) / 255.0f;
00482
00483 (*this)[i] = TypeTraits<ValueTypeT>::getPortion(rTmp);
00484
00485 rgbPack >>= 8;
00486 }
00487 }
00488
00489
00490 template <class ValueTypeT> inline
00491 void Color4<ValueTypeT>::setValue(const Char8 *szString)
00492 {
00493 this->setValueFromCString(szString);
00494 }
00495
00496
00497
00501 template <class ValueTypeT> inline
00502 UInt32 Color4<ValueTypeT>::getRGBA(void) const
00503 {
00504 UInt32 pack = 0;
00505
00506 for(Int32 i = 3; i >= 0; --i)
00507 {
00508 pack = (pack << 8) |
00509 Int32(TypeTraits<ValueTypeT>::getFraction((*this)[i]) *
00510 255.0f +
00511 0.5f );
00512 }
00513
00514 return pack;
00515 }
00516
00517
00518 template <class ValueTypeT> inline
00519 void Color4<ValueTypeT>::getValuesRGBA(ValueType &red,
00520 ValueType &green,
00521 ValueType &blue,
00522 ValueType &alpha) const
00523 {
00524 red = (*this)[0];
00525 green = (*this)[1];
00526 blue = (*this)[2];
00527 alpha = (*this)[3];
00528 }
00529
00530
00531 template <class ValueTypeT> inline
00532 void Color4<ValueTypeT>::getValuesHSV(Real32 &h,
00533 Real32 &s,
00534 Real32 &v) const
00535 {
00536 Color3<ValueType>::convertToHSV(this->getValues(), h, s, v);
00537 }
00538
00539
00540 template <class ValueTypeT> inline
00541 typename Color4<ValueTypeT>::ValueType
00542 Color4<ValueTypeT>::red(void) const
00543 {
00544 return (*this)[0];
00545 }
00546
00547
00548 template <class ValueTypeT> inline
00549 typename Color4<ValueTypeT>::ValueType
00550 Color4<ValueTypeT>::green(void) const
00551 {
00552 return (*this)[1];
00553 }
00554
00555
00556 template <class ValueTypeT> inline
00557 typename Color4<ValueTypeT>::ValueType
00558 Color4<ValueTypeT>::blue(void) const
00559 {
00560 return (*this)[2];
00561 }
00562
00563
00564 template <class ValueTypeT> inline
00565 typename Color4<ValueTypeT>::ValueType
00566 Color4<ValueTypeT>::alpha(void) const
00567 {
00568 return (*this)[3];
00569 }
00570
00571
00572 template <class ValueTypeT> inline
00573 typename Color4<ValueTypeT>::ValueType *
00574 Color4<ValueTypeT>::getValuesRGBA(void)
00575 {
00576 return this->getValues();
00577 }
00578
00579
00580 template <class ValueTypeT> inline
00581 const typename Color4<ValueTypeT>::ValueType *
00582 Color4<ValueTypeT>::getValuesRGBA(void) const
00583 {
00584 return this->getValues();
00585 }
00586
00587 OSG_END_NAMESPACE