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 #include <stdlib.h>
00040 #include <stdio.h>
00041
00042 #include "OSGConfig.h"
00043
00044 #include <stdlib.h>
00045 #include <assert.h>
00046
00047 #ifndef WIN32
00048 #include <unistd.h>
00049 #endif
00050
00051 #include "OSGVector.h"
00052
00053 OSG_BEGIN_NAMESPACE
00054
00055 template <class ValueTypeT>
00056 const Color3<ValueTypeT> Color3<ValueTypeT>::Null;
00057
00058
00059 template <class ValueTypeT> inline
00060 void Color3<ValueTypeT>::convertFromHSV( ValueType *rgbP,
00061 const Real32 h,
00062 const Real32 s,
00063 const Real32 v)
00064 {
00065 if(rgbP == NULL)
00066 return;
00067
00068 Int32 i;
00069 Real32 f;
00070 Real32 p;
00071 Real32 q;
00072 Real32 t;
00073
00074 if(s)
00075 {
00076 f = (h == 360) ? 0.0 : (h / 60.0);
00077 i = Int32(f);
00078
00079 f = f - Real32(i);
00080
00081 p = v * (1.0 - s );
00082 q = v * (1.0 - (s * f) );
00083 t = v * (1.0 - (s * (1 - f)));
00084
00085 switch (i)
00086 {
00087 case 0:
00088 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(v);
00089 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(t);
00090 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(p);
00091 break;
00092 case 1:
00093 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(q);
00094 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(v);
00095 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(p);
00096 break;
00097 case 2:
00098 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(p);
00099 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(v);
00100 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(t);
00101 break;
00102 case 3:
00103 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(p);
00104 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(q);
00105 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(v);
00106 break;
00107 case 4:
00108 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(t);
00109 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(p);
00110 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(v);
00111 break;
00112 case 5:
00113 rgbP[0] = TypeTraits<ValueTypeT>::getPortion(v);
00114 rgbP[1] = TypeTraits<ValueTypeT>::getPortion(p);
00115 rgbP[2] = TypeTraits<ValueTypeT>::getPortion(q);
00116 break;
00117 default:
00118 std::cerr << "ERROR i not in [0, 5] in Color::setHSV()!"
00119 << std::endl;
00120 break;
00121 }
00122 }
00123 else
00124 {
00125 rgbP[0] = rgbP[1] = rgbP[2] =
00126 TypeTraits<ValueTypeT>::getPortion(v);
00127 }
00128 }
00129
00130
00131 template <class ValueTypeT> inline
00132 void Color3<ValueTypeT>::convertToHSV(const ValueType *rgbP,
00133 Real32 &h,
00134 Real32 &s,
00135 Real32 &v)
00136 {
00137 if(rgbP == NULL)
00138 return;
00139
00140 const Real32 r = TypeTraits<ValueTypeT>::getFraction(rgbP[0]);
00141 const Real32 g = TypeTraits<ValueTypeT>::getFraction(rgbP[1]);
00142 const Real32 b = TypeTraits<ValueTypeT>::getFraction(rgbP[2]);
00143
00144 const Int32 maxIndex = maxPart(rgbP);
00145 const Int32 minIndex = minPart(rgbP);
00146
00147 const Real32 max = TypeTraits<ValueTypeT>::getFraction(
00148 rgbP[maxIndex]);
00149
00150 const Real32 min = TypeTraits<ValueTypeT>::getFraction(
00151 rgbP[minIndex]);
00152
00153 const Real32 delta = max - min;
00154
00155 v = max;
00156 s = max ? (max - min) / max : 0.0;
00157
00158 if(s)
00159 {
00160 switch(maxIndex)
00161 {
00162 case 0:
00163 h = (( g - b) / delta) * 60.0;
00164 break;
00165 case 1:
00166 h = (2.0 + (( b - r) / delta)) * 60.0;
00167 break;
00168 case 2:
00169 h = (4.0 + (( r - g) / delta)) * 60.0;
00170 break;
00171 }
00172
00173 if(h < 0.0)
00174 h += 360.0;
00175 }
00176 else
00177 {
00178 h = 0.0;
00179 }
00180 }
00181
00182
00183 template <class ValueTypeT> inline
00184 UInt32 Color3<ValueTypeT>::maxPart(const ValueType *rgbP)
00185 {
00186 if(rgbP[0] > rgbP[1])
00187 {
00188 if(rgbP[2] > rgbP[0])
00189 {
00190 return 2;
00191 }
00192 else
00193 {
00194 return 0;
00195 }
00196 }
00197 else
00198 {
00199 if (rgbP[2] > rgbP[1])
00200 {
00201 return 2;
00202 }
00203 else
00204 {
00205 return 1;
00206 }
00207 }
00208 }
00209
00210
00211 template <class ValueTypeT> inline
00212 UInt32 Color3<ValueTypeT>::minPart(const ValueType *rgbP)
00213 {
00214 if(rgbP[0] < rgbP[1])
00215 {
00216 if(rgbP[2] < rgbP[0])
00217 {
00218 return 2;
00219 }
00220 else
00221 {
00222 return 0;
00223 }
00224 }
00225 else
00226 {
00227 if(rgbP[2] < rgbP[1])
00228 {
00229 return 2;
00230 }
00231 else
00232 {
00233 return 1;
00234 }
00235 }
00236 }
00237
00238
00239 template <class ValueTypeT> inline
00240 Color3<ValueTypeT>::Color3(void)
00241 {
00242 _rgb[0] = TypeTraits<ValueTypeT>::getZeroElement();
00243 _rgb[1] = TypeTraits<ValueTypeT>::getZeroElement();
00244 _rgb[2] = TypeTraits<ValueTypeT>::getZeroElement();
00245 }
00246
00247
00248 template <class ValueTypeT> inline
00249 Color3<ValueTypeT>::Color3(const Color3 &source)
00250 {
00251 _rgb[0] = source._rgb[0];
00252 _rgb[1] = source._rgb[1];
00253 _rgb[2] = source._rgb[2];
00254 }
00255
00256
00257 template <class ValueTypeT> inline
00258 Color3<ValueTypeT>::Color3(ValueType r,
00259 ValueType g,
00260 ValueType b)
00261 {
00262 _rgb[0] = r;
00263 _rgb[1] = g;
00264 _rgb[2] = b;
00265 }
00266
00267
00268 template <class ValueTypeT> inline
00269 Color3<ValueTypeT>::~Color3(void)
00270 {
00271 }
00272
00273
00274 template <class ValueTypeT> inline
00275 void Color3<ValueTypeT>::clear(void)
00276 {
00277 _rgb[0] = TypeTraits<ValueTypeT>::getZeroElement();
00278 _rgb[1] = TypeTraits<ValueTypeT>::getZeroElement();
00279 _rgb[2] = TypeTraits<ValueTypeT>::getZeroElement();
00280 }
00281
00282
00283 template <class ValueTypeT> inline
00284 void Color3<ValueTypeT>::setValuesRGB(const ValueType red,
00285 const ValueType green,
00286 const ValueType blue)
00287 {
00288 _rgb[0] = red;
00289 _rgb[1] = green;
00290 _rgb[2] = blue;
00291 }
00292
00293
00294 template <class ValueTypeT> inline
00295 void Color3<ValueTypeT>::setValuesHSV(const Real32 h,
00296 const Real32 s,
00297 const Real32 v)
00298 {
00299 convertFromHSV(_rgb, h, s, v);
00300 }
00301
00302
00303 template <class ValueTypeT> inline
00304 void Color3<ValueTypeT>::setRandom(void)
00305 {
00306 Real32 rf = 1.0 / Real32(RAND_MAX);
00307
00308 setValuesRGB(TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00309 TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00310 TypeTraits<ValueTypeT>::getPortion(rf * rand()));
00311 }
00312
00316 template <class ValueTypeT> inline
00317 void Color3<ValueTypeT>::setRGB(UInt32 rgbPack)
00318 {
00319 for(Int32 i = 0; i < 3; ++i)
00320 {
00321 Real32 rTmp = Real32(rgbPack & 255) / 255.0f;
00322
00323 _rgb[i] = TypeTraits<ValueTypeT>::getPortion(rTmp);
00324
00325 rgbPack >>= 8;
00326 }
00327 }
00328
00329
00330 template <class ValueTypeT> inline
00331 void Color3<ValueTypeT>::setValue(const Char8 *szString)
00332 {
00333
00334
00335 Vec3f v;
00336
00337 v.setValueFromCString(szString);
00338
00339 _rgb[0] = ValueTypeT(v[0]);
00340 _rgb[1] = ValueTypeT(v[1]);
00341 _rgb[2] = ValueTypeT(v[2]);
00342 }
00343
00344
00345 template <class ValueTypeT> inline
00346 void Color3<ValueTypeT>::setValue(Char8 *szString)
00347 {
00348 setValue(static_cast<const Char8 *>(szString));
00349 }
00350
00351
00355 template <class ValueTypeT> inline
00356 UInt32 Color3<ValueTypeT>::getRGB(void) const
00357 {
00358 UInt32 pack = 0;
00359
00360 for(Int32 i = 2; i >= 0; --i)
00361 {
00362 pack = (pack << 8) |
00363 Int32(TypeTraits<ValueTypeT>::getFraction(_rgb[i]) *
00364 255.0f +
00365 0.5f );
00366 }
00367
00368 return pack;
00369 }
00370
00371
00372 template <class ValueTypeT> inline
00373 void Color3<ValueTypeT>::getValuesRGB(ValueType &red,
00374 ValueType &green,
00375 ValueType &blue) const
00376 {
00377 red = _rgb[0];
00378 green = _rgb[1];
00379 blue = _rgb[2];
00380 }
00381
00382
00383 template <class ValueTypeT> inline
00384 void Color3<ValueTypeT>::getValuesHSV(Real32 &h,
00385 Real32 &s,
00386 Real32 &v) const
00387 {
00388 convertToHSV(_rgb, h, s, v);
00389 }
00390
00391
00392 template <class ValueTypeT> inline
00393 typename Color3<ValueTypeT>::ValueType
00394 Color3<ValueTypeT>::red(void) const
00395 {
00396 return _rgb[0];
00397 }
00398
00399 template <class ValueTypeT> inline
00400 typename Color3<ValueTypeT>::ValueType
00401 Color3<ValueTypeT>::green(void) const
00402 {
00403 return _rgb[1];
00404 }
00405
00406 template <class ValueTypeT> inline
00407 typename Color3<ValueTypeT>::ValueType
00408 Color3<ValueTypeT>::blue(void) const
00409 {
00410 return _rgb[2];
00411 }
00412
00413
00414 template <class ValueTypeT> inline
00415 typename Color3<ValueTypeT>::ValueType *
00416 Color3<ValueTypeT>::getValuesRGB(void)
00417 {
00418 return _rgb;
00419 }
00420
00421 template <class ValueTypeT> inline
00422 const typename Color3<ValueTypeT>::ValueType *
00423 Color3<ValueTypeT>::getValuesRGB(void) const
00424 {
00425 return _rgb;
00426 }
00427
00428
00429 template <class ValueTypeT> inline
00430 Color3<ValueTypeT> Color3<ValueTypeT>::operator *(const ValueType val)
00431 {
00432 Color3<ValueTypeT> returnValue;
00433
00434 returnValue._rgb[0] = _rgb[0] * val;
00435 returnValue._rgb[1] = _rgb[1] * val;
00436 returnValue._rgb[2] = _rgb[2] * val;
00437
00438 return returnValue;
00439 }
00440
00441 template <class ValueTypeT> inline
00442 Color3<ValueTypeT> Color3<ValueTypeT>::operator /(const ValueType val)
00443 {
00444 Color3<ValueTypeT> returnValue;
00445
00446 returnValue._rgb[0] = _rgb[0] / val;
00447 returnValue._rgb[1] = _rgb[1] / val;
00448 returnValue._rgb[2] = _rgb[2] / val;
00449
00450 return returnValue;
00451 }
00452
00453 template <class ValueTypeT> inline
00454 Color3<ValueTypeT> Color3<ValueTypeT>::operator +(const ValueType val)
00455 {
00456 Color3<ValueTypeT> returnValue;
00457
00458 returnValue._rgb[0] = _rgb[0] + val;
00459 returnValue._rgb[1] = _rgb[1] + val;
00460 returnValue._rgb[2] = _rgb[2] + val;
00461
00462 return returnValue;
00463 }
00464
00465 template <class ValueTypeT> inline
00466 Color3<ValueTypeT> Color3<ValueTypeT>::operator -(const ValueType val)
00467 {
00468 Color3<ValueTypeT> returnValue;
00469
00470 returnValue._rgb[0] = _rgb[0] - val;
00471 returnValue._rgb[1] = _rgb[1] - val;
00472 returnValue._rgb[2] = _rgb[2] - val;
00473
00474 return returnValue;
00475 }
00476
00477 template <class ValueTypeT> inline
00478 void Color3<ValueTypeT>::operator *=(const ValueType val)
00479 {
00480 _rgb[0] *= val;
00481 _rgb[1] *= val;
00482 _rgb[2] *= val;
00483 }
00484
00485 template <class ValueTypeT> inline
00486 void Color3<ValueTypeT>::operator /=(const ValueType val)
00487 {
00488 _rgb[0] /= val;
00489 _rgb[1] /= val;
00490 _rgb[2] /= val;
00491 }
00492
00493 template <class ValueTypeT> inline
00494 void Color3<ValueTypeT>::operator +=(const ValueType val)
00495 {
00496 _rgb[0] += val;
00497 _rgb[1] += val;
00498 _rgb[2] += val;
00499 }
00500
00501 template <class ValueTypeT> inline
00502 void Color3<ValueTypeT>::operator -=(const ValueType val)
00503 {
00504 _rgb[0] -= val;
00505 _rgb[1] -= val;
00506 _rgb[2] -= val;
00507 }
00508
00509 template <class ValueTypeT> inline
00510 Color3<ValueTypeT> Color3<ValueTypeT>::operator *(const Color3<ValueTypeT> &other) const
00511 {
00512 Color3<ValueTypeT> returnValue;
00513
00514 returnValue._rgb[0] = _rgb[0] * other._rgb[0];
00515 returnValue._rgb[1] = _rgb[1] * other._rgb[1];
00516 returnValue._rgb[2] = _rgb[2] * other._rgb[2];
00517
00518 return returnValue;
00519 }
00520
00521 template <class ValueTypeT> inline
00522 Color3<ValueTypeT> Color3<ValueTypeT>::operator /(const Color3<ValueTypeT> &other) const
00523 {
00524 Color3<ValueTypeT> returnValue;
00525
00526 returnValue._rgb[0] = _rgb[0] / other._rgb[0];
00527 returnValue._rgb[1] = _rgb[1] / other._rgb[1];
00528 returnValue._rgb[2] = _rgb[2] / other._rgb[2];
00529
00530 return returnValue;
00531 }
00532
00533 template <class ValueTypeT> inline
00534 Color3<ValueTypeT> Color3<ValueTypeT>::operator +(const Color3<ValueTypeT> &other) const
00535 {
00536 Color3<ValueTypeT> returnValue;
00537
00538 returnValue._rgb[0] = _rgb[0] + other._rgb[0];
00539 returnValue._rgb[1] = _rgb[1] + other._rgb[1];
00540 returnValue._rgb[2] = _rgb[2] + other._rgb[2];
00541
00542 return returnValue;
00543 }
00544
00545 template <class ValueTypeT> inline
00546 Color3<ValueTypeT> Color3<ValueTypeT>::operator -(const Color3<ValueTypeT> &other) const
00547 {
00548 Color3<ValueTypeT> returnValue;
00549
00550 returnValue._rgb[0] = _rgb[0] - other._rgb[0];
00551 returnValue._rgb[1] = _rgb[1] - other._rgb[1];
00552 returnValue._rgb[2] = _rgb[2] - other._rgb[2];
00553
00554 return returnValue;
00555 }
00556
00557 template <class ValueTypeT> inline
00558 void Color3<ValueTypeT>::operator *=(const Color3<ValueTypeT> &other)
00559 {
00560 _rgb[0] *= other._rgb[0];
00561 _rgb[1] *= other._rgb[1];
00562 _rgb[2] *= other._rgb[2];
00563 }
00564
00565 template <class ValueTypeT> inline
00566 void Color3<ValueTypeT>::operator /=(const Color3<ValueTypeT> &other)
00567 {
00568 _rgb[0] /= other._rgb[0];
00569 _rgb[1] /= other._rgb[1];
00570 _rgb[2] /= other._rgb[2];
00571 }
00572
00573 template <class ValueTypeT> inline
00574 void Color3<ValueTypeT>::operator +=(const Color3<ValueTypeT> &other)
00575 {
00576 _rgb[0] += other._rgb[0];
00577 _rgb[1] += other._rgb[1];
00578 _rgb[2] += other._rgb[2];
00579 }
00580
00581 template <class ValueTypeT> inline
00582 void Color3<ValueTypeT>::operator -=(const Color3<ValueTypeT> &other)
00583 {
00584 _rgb[0] -= other._rgb[0];
00585 _rgb[1] -= other._rgb[1];
00586 _rgb[2] -= other._rgb[2];
00587 }
00588
00589
00590 template <class ValueTypeT> inline
00591 typename Color3<ValueTypeT>::ValueType &Color3<ValueTypeT>::operator[] (
00592 const UInt32 uiIndex)
00593 {
00594 return _rgb[uiIndex];
00595 }
00596
00597
00598 template <class ValueTypeT> inline
00599 const typename Color3<ValueTypeT>::ValueType &Color3<ValueTypeT>::operator[](
00600 const UInt32 uiIndex) const
00601 {
00602 return _rgb[uiIndex];
00603 }
00604
00605
00606 template <class ValueTypeT> inline
00607 Color3<ValueTypeT> &Color3<ValueTypeT>::operator =(const Color3 &other)
00608 {
00609 if (this == &other)
00610 return *this;
00611
00612 _rgb[0] = other._rgb[0];
00613 _rgb[1] = other._rgb[1];
00614 _rgb[2] = other._rgb[2];
00615
00616 return *this;
00617 }
00618
00619
00620 template <class ValueTypeT> inline
00621 bool Color3<ValueTypeT>::equals(const Color3 &other,
00622 const ValueType tolerance) const
00623 {
00624 bool returnValue = true;
00625
00626 for(UInt32 i = 0; i < 3; i++)
00627 {
00628 returnValue &= ( ( _rgb[i] - other._rgb[i] <= tolerance) &&
00629 (other._rgb[i] - _rgb[i] <= tolerance));
00630 }
00631
00632 return returnValue;
00633 }
00634
00635
00636 template <class ValueTypeT> inline
00637 bool Color3<ValueTypeT>::operator < (const Color3 &other) const
00638 {
00639 bool ret = false;
00640
00641 for(UInt32 i = 0; i < 3; ++i)
00642 {
00643 if(_rgb[i] > other._rgb[i])
00644 {
00645 break;
00646 }
00647 if(_rgb[i] < other._rgb[i])
00648 {
00649 ret = true;
00650 break;
00651 }
00652 }
00653
00654 return ret;
00655 }
00656
00657 template <class ValueTypeT> inline
00658 bool Color3<ValueTypeT>::operator ==(const Color3 &other) const
00659 {
00660 bool returnValue = true;
00661
00662 for(UInt32 i = 0; i < 3; i++)
00663 {
00664 returnValue &= ( ( _rgb[i] - other._rgb[i] <= Eps) &&
00665 (other._rgb[i] - _rgb[i] <= Eps));
00666 }
00667
00668 return returnValue;
00669 }
00670
00671
00672 template <class ValueTypeT> inline
00673 bool Color3<ValueTypeT>::operator != (const Color3 &other) const
00674 {
00675 return ! (*this == other);
00676 }
00677
00678
00679
00680
00681 template <class ValueTypeT>
00682 const Color4<ValueTypeT> Color4<ValueTypeT>::Null;
00683
00684
00685
00686 template <class ValueTypeT> inline
00687 Color4<ValueTypeT>::Color4(void)
00688 {
00689 _rgba[0] = TypeTraits<ValueTypeT>::getZeroElement();
00690 _rgba[1] = TypeTraits<ValueTypeT>::getZeroElement();
00691 _rgba[2] = TypeTraits<ValueTypeT>::getZeroElement();
00692 _rgba[3] = TypeTraits<ValueTypeT>::getZeroElement();
00693 }
00694
00695
00696 template <class ValueTypeT> inline
00697 Color4<ValueTypeT>::Color4(const Color4 &source)
00698 {
00699 _rgba[0] = source._rgba[0];
00700 _rgba[1] = source._rgba[1];
00701 _rgba[2] = source._rgba[2];
00702 _rgba[3] = source._rgba[3];
00703 }
00704
00705
00706 template <class ValueTypeT> inline
00707 Color4<ValueTypeT>::Color4(const ValueType red,
00708 const ValueType green,
00709 const ValueType blue,
00710 const ValueType alpha)
00711 {
00712 _rgba[0] = red;
00713 _rgba[1] = green;
00714 _rgba[2] = blue;
00715 _rgba[3] = alpha;
00716 }
00717
00718
00719 template <class ValueTypeT> inline
00720 Color4<ValueTypeT>::~Color4(void)
00721 {
00722 }
00723
00724
00725 template <class ValueTypeT> inline
00726 void Color4<ValueTypeT>::clear(void)
00727 {
00728 _rgba[0] = TypeTraits<ValueTypeT>::getZeroElement();
00729 _rgba[1] = TypeTraits<ValueTypeT>::getZeroElement();
00730 _rgba[2] = TypeTraits<ValueTypeT>::getZeroElement();
00731 _rgba[3] = TypeTraits<ValueTypeT>::getZeroElement();
00732 }
00733
00734
00735 template <class ValueTypeT> inline
00736 void Color4<ValueTypeT>::setValuesRGBA(const ValueType red,
00737 const ValueType green,
00738 const ValueType blue,
00739 const ValueType alpha)
00740 {
00741 _rgba[0] = red;
00742 _rgba[1] = green;
00743 _rgba[2] = blue;
00744 _rgba[3] = alpha;
00745 }
00746
00747 template <class ValueTypeT> inline
00748 void Color4<ValueTypeT>::setValuesHSV(const Real32 h,
00749 const Real32 s,
00750 const Real32 v)
00751 {
00752 Color3<ValueType>::convertFromHSV(_rgba, h, s, v);
00753
00754 _rgba[3] = TypeTraits<ValueTypeT>::getOneElement();
00755 }
00756
00757
00758 template <class ValueTypeT> inline
00759 void Color4<ValueTypeT>::setRandom(void)
00760 {
00761 Real32 rf = 1.0 / Real32(RAND_MAX);
00762
00763 setValuesRGBA(TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00764 TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00765 TypeTraits<ValueTypeT>::getPortion(rf * rand()),
00766 TypeTraits<ValueTypeT>::getPortion(rf * rand()));
00767 }
00768
00772 template <class ValueTypeT> inline
00773 void Color4<ValueTypeT>::setRGBA(UInt32 rgbPack)
00774 {
00775 for(Int32 i = 0; i < 4; ++i)
00776 {
00777 Real32 rTmp = Real32(rgbPack & 255) / 255.0f;
00778
00779 _rgba[i] = TypeTraits<ValueTypeT>::getPortion(rTmp);
00780
00781 rgbPack >>= 8;
00782 }
00783 }
00784
00785
00786 template <class ValueTypeT> inline
00787 void Color4<ValueTypeT>::setValue(const Char8 *szString)
00788 {
00789
00790 Vec4f v;
00791
00792 v.setValueFromCString(szString);
00793
00794 _rgba[0] = ValueTypeT(v[0]);
00795 _rgba[1] = ValueTypeT(v[1]);
00796 _rgba[2] = ValueTypeT(v[2]);
00797 _rgba[3] = ValueTypeT(v[3]);
00798 }
00799
00800
00801 template <class ValueTypeT> inline
00802 void Color4<ValueTypeT>::setValue(Char8 *szString)
00803 {
00804 setValue(static_cast<const Char8 *>(szString));
00805 }
00806
00810 template <class ValueTypeT> inline
00811 UInt32 Color4<ValueTypeT>::getRGBA(void) const
00812 {
00813 UInt32 pack = 0;
00814
00815 for(Int32 i = 3; i >= 0; --i)
00816 {
00817 pack = (pack << 8) |
00818 Int32(TypeTraits<ValueTypeT>::getFraction(_rgba[i]) *
00819 255.0f +
00820 0.5f );
00821 }
00822
00823 return pack;
00824 }
00825
00826
00827 template <class ValueTypeT> inline
00828 void Color4<ValueTypeT>::getValuesRGBA(ValueType &red,
00829 ValueType &green,
00830 ValueType &blue,
00831 ValueType &alpha) const
00832 {
00833 red = _rgba[0];
00834 green = _rgba[1];
00835 blue = _rgba[2];
00836 alpha = _rgba[3];
00837 }
00838
00839
00840 template <class ValueTypeT> inline
00841 void Color4<ValueTypeT>::getValuesHSV(Real32 &h,
00842 Real32 &s,
00843 Real32 &v) const
00844 {
00845 Color3<ValueType>::convertToHSV(_rgba, h, s, v);
00846 }
00847
00848
00849 template <class ValueTypeT> inline
00850 typename Color4<ValueTypeT>::ValueType
00851 Color4<ValueTypeT>::red(void) const
00852 {
00853 return _rgba[0];
00854 }
00855
00856
00857 template <class ValueTypeT> inline
00858 typename Color4<ValueTypeT>::ValueType
00859 Color4<ValueTypeT>::green(void) const
00860 {
00861 return _rgba[1];
00862 }
00863
00864
00865 template <class ValueTypeT> inline
00866 typename Color4<ValueTypeT>::ValueType
00867 Color4<ValueTypeT>::blue(void) const
00868 {
00869 return _rgba[2];
00870 }
00871
00872
00873 template <class ValueTypeT> inline
00874 typename Color4<ValueTypeT>::ValueType
00875 Color4<ValueTypeT>::alpha(void) const
00876 {
00877 return _rgba[3];
00878 }
00879
00880
00881 template <class ValueTypeT> inline
00882 typename Color4<ValueTypeT>::ValueType *
00883 Color4<ValueTypeT>::getValuesRGBA(void)
00884 {
00885 return _rgba;
00886 }
00887
00888
00889 template <class ValueTypeT> inline
00890 const typename Color4<ValueTypeT>::ValueType *
00891 Color4<ValueTypeT>::getValuesRGBA(void) const
00892 {
00893 return _rgba;
00894 }
00895
00896
00897 template <class ValueTypeT> inline
00898 Color4<ValueTypeT> Color4<ValueTypeT>::operator *(const ValueType val)
00899 {
00900 Color4<ValueTypeT> returnValue;
00901
00902 returnValue._rgba[0] = _rgba[0] * val;
00903 returnValue._rgba[1] = _rgba[1] * val;
00904 returnValue._rgba[2] = _rgba[2] * val;
00905 returnValue._rgba[3] = _rgba[3] * val;
00906
00907 return returnValue;
00908 }
00909
00910 template <class ValueTypeT> inline
00911 Color4<ValueTypeT> Color4<ValueTypeT>::operator /(const ValueType val)
00912 {
00913 Color4<ValueTypeT> returnValue;
00914
00915 returnValue._rgba[0] = _rgba[0] / val;
00916 returnValue._rgba[1] = _rgba[1] / val;
00917 returnValue._rgba[2] = _rgba[2] / val;
00918 returnValue._rgba[3] = _rgba[3] / val;
00919
00920 return returnValue;
00921 }
00922
00923 template <class ValueTypeT> inline
00924 Color4<ValueTypeT> Color4<ValueTypeT>::operator +(const ValueType val)
00925 {
00926 Color4<ValueTypeT> returnValue;
00927
00928 returnValue._rgba[0] = _rgba[0] + val;
00929 returnValue._rgba[1] = _rgba[1] + val;
00930 returnValue._rgba[2] = _rgba[2] + val;
00931 returnValue._rgba[3] = _rgba[3] + val;
00932
00933 return returnValue;
00934 }
00935
00936 template <class ValueTypeT> inline
00937 Color4<ValueTypeT> Color4<ValueTypeT>::operator -(const ValueType val)
00938 {
00939 Color4<ValueTypeT> returnValue;
00940
00941 returnValue._rgba[0] = _rgba[0] - val;
00942 returnValue._rgba[1] = _rgba[1] - val;
00943 returnValue._rgba[2] = _rgba[2] - val;
00944 returnValue._rgba[3] = _rgba[3] - val;
00945
00946 return returnValue;
00947 }
00948
00949 template <class ValueTypeT> inline
00950 void Color4<ValueTypeT>::operator *=(const ValueType val)
00951 {
00952 _rgba[0] *= val;
00953 _rgba[1] *= val;
00954 _rgba[2] *= val;
00955 _rgba[3] *= val;
00956 }
00957
00958 template <class ValueTypeT> inline
00959 void Color4<ValueTypeT>::operator /=(const ValueType val)
00960 {
00961 _rgba[0] /= val;
00962 _rgba[1] /= val;
00963 _rgba[2] /= val;
00964 _rgba[3] /= val;
00965 }
00966
00967 template <class ValueTypeT> inline
00968 void Color4<ValueTypeT>::operator +=(const ValueType val)
00969 {
00970 _rgba[0] += val;
00971 _rgba[1] += val;
00972 _rgba[2] += val;
00973 _rgba[3] += val;
00974 }
00975
00976 template <class ValueTypeT> inline
00977 void Color4<ValueTypeT>::operator -=(const ValueType val)
00978 {
00979 _rgba[0] -= val;
00980 _rgba[1] -= val;
00981 _rgba[2] -= val;
00982 _rgba[3] -= val;
00983 }
00984
00985
00986 template <class ValueTypeT> inline
00987 Color4<ValueTypeT> Color4<ValueTypeT>::operator *(const Color4<ValueTypeT> &other) const
00988 {
00989 Color4<ValueTypeT> returnValue;
00990
00991 returnValue._rgba[0] = _rgba[0] * other._rgba[0];
00992 returnValue._rgba[1] = _rgba[1] * other._rgba[1];
00993 returnValue._rgba[2] = _rgba[2] * other._rgba[2];
00994 returnValue._rgba[3] = _rgba[3] * other._rgba[3];
00995
00996 return returnValue;
00997 }
00998
00999 template <class ValueTypeT> inline
01000 Color4<ValueTypeT> Color4<ValueTypeT>::operator /(const Color4<ValueTypeT> &other) const
01001 {
01002 Color4<ValueTypeT> returnValue;
01003
01004 returnValue._rgba[0] = _rgba[0] / other._rgba[0];
01005 returnValue._rgba[1] = _rgba[1] / other._rgba[1];
01006 returnValue._rgba[2] = _rgba[2] / other._rgba[2];
01007 returnValue._rgba[3] = _rgba[3] / other._rgba[3];
01008
01009 return returnValue;
01010 }
01011
01012 template <class ValueTypeT> inline
01013 Color4<ValueTypeT> Color4<ValueTypeT>::operator +(const Color4<ValueTypeT> &other) const
01014 {
01015 Color4<ValueTypeT> returnValue;
01016
01017 returnValue._rgba[0] = _rgba[0] + other._rgba[0];
01018 returnValue._rgba[1] = _rgba[1] + other._rgba[1];
01019 returnValue._rgba[2] = _rgba[2] + other._rgba[2];
01020 returnValue._rgba[3] = _rgba[3] + other._rgba[3];
01021
01022 return returnValue;
01023 }
01024
01025 template <class ValueTypeT> inline
01026 Color4<ValueTypeT> Color4<ValueTypeT>::operator -(const Color4<ValueTypeT> &other) const
01027 {
01028 Color4<ValueTypeT> returnValue;
01029
01030 returnValue._rgba[0] = _rgba[0] - other._rgba[0];
01031 returnValue._rgba[1] = _rgba[1] - other._rgba[1];
01032 returnValue._rgba[2] = _rgba[2] - other._rgba[2];
01033 returnValue._rgba[3] = _rgba[3] - other._rgba[3];
01034
01035 return returnValue;
01036 }
01037
01038 template <class ValueTypeT> inline
01039 void Color4<ValueTypeT>::operator *=(const Color4<ValueTypeT> &other)
01040 {
01041 _rgba[0] *= other._rgba[0];
01042 _rgba[1] *= other._rgba[1];
01043 _rgba[2] *= other._rgba[2];
01044 _rgba[3] *= other._rgba[3];
01045 }
01046
01047 template <class ValueTypeT> inline
01048 void Color4<ValueTypeT>::operator /=(const Color4<ValueTypeT> &other)
01049 {
01050 _rgba[0] /= other._rgba[0];
01051 _rgba[1] /= other._rgba[1];
01052 _rgba[2] /= other._rgba[2];
01053 _rgba[3] /= other._rgba[3];
01054 }
01055
01056 template <class ValueTypeT> inline
01057 void Color4<ValueTypeT>::operator +=(const Color4<ValueTypeT> &other)
01058 {
01059 _rgba[0] += other._rgba[0];
01060 _rgba[1] += other._rgba[1];
01061 _rgba[2] += other._rgba[2];
01062 _rgba[3] += other._rgba[3];
01063 }
01064
01065 template <class ValueTypeT> inline
01066 void Color4<ValueTypeT>::operator -=(const Color4<ValueTypeT> &other)
01067 {
01068 _rgba[0] -= other._rgba[0];
01069 _rgba[1] -= other._rgba[1];
01070 _rgba[2] -= other._rgba[2];
01071 _rgba[3] -= other._rgba[3];
01072 }
01073
01074 template <class ValueTypeT> inline
01075 typename Color4<ValueTypeT>::ValueType &Color4<ValueTypeT>::operator[] (
01076 const UInt32 uiIndex)
01077 {
01078 return _rgba[uiIndex];
01079 }
01080
01081
01082 template <class ValueTypeT> inline
01083 const typename Color4<ValueTypeT>::ValueType &Color4<ValueTypeT>::operator[] (
01084 const UInt32 uiIndex) const
01085 {
01086 return _rgba[uiIndex];
01087 }
01088
01089
01090
01091 template <class ValueTypeT> inline
01092 Color4<ValueTypeT> &Color4<ValueTypeT>::operator = (
01093 const Color4 &other)
01094 {
01095 if (this == &other)
01096 return *this;
01097
01098 _rgba[0] = other._rgba[0];
01099 _rgba[1] = other._rgba[1];
01100 _rgba[2] = other._rgba[2];
01101 _rgba[3] = other._rgba[3];
01102
01103 return *this;
01104 }
01105
01106
01110 template <class ValueTypeT> inline
01111 bool Color4<ValueTypeT>::equals(const Color4 &other,
01112 const ValueType tolerance) const
01113 {
01114 bool returnValue = true;
01115
01116 for(UInt32 i = 0; i < 4; i++)
01117 {
01118 returnValue &= ( ( _rgba[i] - other._rgba[i] <= tolerance) &&
01119 (other._rgba[i] - _rgba[i] <= tolerance));
01120 }
01121
01122 return returnValue;
01123 }
01124
01125 template <class ValueTypeT> inline
01126 bool Color4<ValueTypeT>::operator < (const Color4 &other) const
01127 {
01128 bool ret = false;
01129
01130 for(UInt32 i = 0; i < 4; ++i)
01131 {
01132 if(_rgba[i] > other._rgba[i])
01133 {
01134 break;
01135 }
01136 if(_rgba[i] < other._rgba[i])
01137 {
01138 ret = true;
01139 break;
01140 }
01141 }
01142
01143 return ret;
01144 }
01145
01146 template <class ValueTypeT> inline
01147 bool Color4<ValueTypeT>::operator ==(const Color4 &other) const
01148 {
01149 bool returnValue = true;
01150
01151 for(UInt32 i = 0; i < 4; i++)
01152 {
01153 returnValue &= ( ( _rgba[i] - other._rgba[i] <= Eps) &&
01154 (other._rgba[i] - _rgba[i] <= Eps));
01155 }
01156
01157 return returnValue;
01158 }
01159
01160
01161 template <class ValueTypeT> inline
01162 bool Color4<ValueTypeT>::operator !=(const Color4 &other) const
01163 {
01164 return ! (*this == other);
01165 }
01166
01167
01168 template <class ValueTypeT> inline
01169 std::ostream &operator <<( std::ostream &outStream,
01170 const Color3<ValueTypeT> &color)
01171 {
01172 return outStream << color.red() << ' '
01173 << color.green() << ' '
01174 << color.blue();
01175 }
01176
01177
01178 template <class ValueTypeT> inline
01179 std::ostream &operator <<( std::ostream &outStream,
01180 const Color4<ValueTypeT> &color)
01181 {
01182 return outStream << color.red() << ' '
01183 << color.green() << ' '
01184 << color.blue() << ' '
01185 << color.alpha();
01186 }
01187
01188 OSG_END_NAMESPACE
01189
01190 #define OSGCOLOR_INLINE_CVSID "@(#)$Id: $"