Vec4ub 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_VEC4UB
  14. #define OSG_VEC4UB 1
  15. #include <osg/Vec4f>
  16. namespace osg {
  17. /** General purpose float quad.
  18. * Uses include representation of color coordinates.
  19. * No support yet added for float * Vec4ub - is it necessary?
  20. * Need to define a non-member non-friend operator* etc.
  21. * Vec4ub * float is okay
  22. */
  23. class Vec4ub
  24. {
  25. public:
  26. /** Data type of vector components.*/
  27. typedef unsigned char value_type;
  28. /** Number of vector components. */
  29. enum { num_components = 4 };
  30. /** Vec member variable. */
  31. value_type _v[4];
  32. /** Constructor that sets all components of the vector to zero */
  33. Vec4ub() { _v[0]=0; _v[1]=0; _v[2]=0; _v[3]=0; }
  34. Vec4ub(value_type x, value_type y, value_type z, value_type w)
  35. {
  36. _v[0]=x;
  37. _v[1]=y;
  38. _v[2]=z;
  39. _v[3]=w;
  40. }
  41. inline bool operator == (const Vec4ub& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
  42. inline bool operator != (const Vec4ub& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
  43. inline bool operator < (const Vec4ub& v) const
  44. {
  45. if (_v[0]<v._v[0]) return true;
  46. else if (_v[0]>v._v[0]) return false;
  47. else if (_v[1]<v._v[1]) return true;
  48. else if (_v[1]>v._v[1]) return false;
  49. else if (_v[2]<v._v[2]) return true;
  50. else if (_v[2]>v._v[2]) return false;
  51. else return (_v[3]<v._v[3]);
  52. }
  53. inline value_type* ptr() { return _v; }
  54. inline const value_type* ptr() const { return _v; }
  55. inline void set(value_type r, value_type g, value_type b, value_type a)
  56. {
  57. _v[0]=r; _v[1]=g; _v[2]=b; _v[3]=a;
  58. }
  59. inline value_type& operator [] (unsigned int i) { return _v[i]; }
  60. inline value_type operator [] (unsigned int i) const { return _v[i]; }
  61. inline value_type& x() { return _v[0]; }
  62. inline value_type& y() { return _v[1]; }
  63. inline value_type& z() { return _v[2]; }
  64. inline value_type& w() { return _v[3]; }
  65. inline value_type x() const { return _v[0]; }
  66. inline value_type y() const { return _v[1]; }
  67. inline value_type z() const { return _v[2]; }
  68. inline value_type w() const { return _v[3]; }
  69. inline value_type& r() { return _v[0]; }
  70. inline value_type& g() { return _v[1]; }
  71. inline value_type& b() { return _v[2]; }
  72. inline value_type& a() { return _v[3]; }
  73. inline value_type r() const { return _v[0]; }
  74. inline value_type g() const { return _v[1]; }
  75. inline value_type b() const { return _v[2]; }
  76. inline value_type a() const { return _v[3]; }
  77. /** Multiply by scalar. */
  78. inline Vec4ub operator * (float rhs) const
  79. {
  80. Vec4ub col(*this);
  81. col *= rhs;
  82. return col;
  83. }
  84. /** Unary multiply by scalar. */
  85. inline Vec4ub& operator *= (float rhs)
  86. {
  87. _v[0]=(value_type)((float)_v[0]*rhs);
  88. _v[1]=(value_type)((float)_v[1]*rhs);
  89. _v[2]=(value_type)((float)_v[2]*rhs);
  90. _v[3]=(value_type)((float)_v[3]*rhs);
  91. return *this;
  92. }
  93. /** Divide by scalar. */
  94. inline Vec4ub operator / (float rhs) const
  95. {
  96. Vec4ub col(*this);
  97. col /= rhs;
  98. return col;
  99. }
  100. /** Unary divide by scalar. */
  101. inline Vec4ub& operator /= (float rhs)
  102. {
  103. float div = 1.0f/rhs;
  104. *this *= div;
  105. return *this;
  106. }
  107. /** Binary vector add. */
  108. inline Vec4ub operator + (const Vec4ub& rhs) const
  109. {
  110. return Vec4ub(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
  111. _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
  112. }
  113. /** Unary vector add. Slightly more efficient because no temporary
  114. * intermediate object.
  115. */
  116. inline Vec4ub& operator += (const Vec4ub& rhs)
  117. {
  118. _v[0] += rhs._v[0];
  119. _v[1] += rhs._v[1];
  120. _v[2] += rhs._v[2];
  121. _v[3] += rhs._v[3];
  122. return *this;
  123. }
  124. /** Binary vector subtract. */
  125. inline Vec4ub operator - (const Vec4ub& rhs) const
  126. {
  127. return Vec4ub(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
  128. _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
  129. }
  130. /** Unary vector subtract. */
  131. inline Vec4ub& operator -= (const Vec4ub& rhs)
  132. {
  133. _v[0]-=rhs._v[0];
  134. _v[1]-=rhs._v[1];
  135. _v[2]-=rhs._v[2];
  136. _v[3]-=rhs._v[3];
  137. return *this;
  138. }
  139. }; // end of class Vec4ub
  140. inline Vec4ub convertToRGBA8(const Vec4f& color)
  141. {
  142. return Vec4ub(static_cast<unsigned char>(color.r()*255.0f),
  143. static_cast<unsigned char>(color.g()*255.0f),
  144. static_cast<unsigned char>(color.b()*255.0f),
  145. static_cast<unsigned char>(color.a()*255.0f));
  146. }
  147. inline Vec4ub convertToRGBA8(float r, float g, float b, float a)
  148. {
  149. return Vec4ub(static_cast<unsigned char>(r*255.0f),
  150. static_cast<unsigned char>(g*255.0f),
  151. static_cast<unsigned char>(b*255.0f),
  152. static_cast<unsigned char>(a*255.0f));
  153. }
  154. } // end of namespace osg
  155. #endif