openscenegraph
Vec4ub
Go to the documentation of this file.
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
14#ifndef OSG_VEC4UB
15#define OSG_VEC4UB 1
16
17#include <osg/Vec4f>
18
19namespace osg {
20
21/** General purpose float quad.
22 * Uses include representation of color coordinates.
23 * No support yet added for float * Vec4ub - is it necessary?
24 * Need to define a non-member non-friend operator* etc.
25 * Vec4ub * float is okay
26*/
27class Vec4ub
28{
29 public:
30
31 /** Data type of vector components.*/
32 typedef unsigned char value_type;
33
34 /** Number of vector components. */
35 enum { num_components = 4 };
36
37 /** Vec member variable. */
38 value_type _v[4];
39
40 /** Constructor that sets all components of the vector to zero */
41 Vec4ub() { _v[0]=0; _v[1]=0; _v[2]=0; _v[3]=0; }
42
43 Vec4ub(value_type x, value_type y, value_type z, value_type w)
44 {
45 _v[0]=x;
46 _v[1]=y;
47 _v[2]=z;
48 _v[3]=w;
49 }
50
51 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]; }
52
53 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]; }
54
55 inline bool operator < (const Vec4ub& v) const
56 {
57 if (_v[0]<v._v[0]) return true;
58 else if (_v[0]>v._v[0]) return false;
59 else if (_v[1]<v._v[1]) return true;
60 else if (_v[1]>v._v[1]) return false;
61 else if (_v[2]<v._v[2]) return true;
62 else if (_v[2]>v._v[2]) return false;
63 else return (_v[3]<v._v[3]);
64 }
65
66 inline value_type* ptr() { return _v; }
67 inline const value_type* ptr() const { return _v; }
68
69 inline void set(value_type r, value_type g, value_type b, value_type a)
70 {
71 _v[0]=r; _v[1]=g; _v[2]=b; _v[3]=a;
72 }
73
74 inline value_type& operator [] (unsigned int i) { return _v[i]; }
75 inline value_type operator [] (unsigned int i) const { return _v[i]; }
76
77 inline value_type& x() { return _v[0]; }
78 inline value_type& y() { return _v[1]; }
79 inline value_type& z() { return _v[2]; }
80 inline value_type& w() { return _v[3]; }
81
82 inline value_type x() const { return _v[0]; }
83 inline value_type y() const { return _v[1]; }
84 inline value_type z() const { return _v[2]; }
85 inline value_type w() const { return _v[3]; }
86
87 inline value_type& r() { return _v[0]; }
88 inline value_type& g() { return _v[1]; }
89 inline value_type& b() { return _v[2]; }
90 inline value_type& a() { return _v[3]; }
91
92 inline value_type r() const { return _v[0]; }
93 inline value_type g() const { return _v[1]; }
94 inline value_type b() const { return _v[2]; }
95 inline value_type a() const { return _v[3]; }
96
97 /** Multiply by scalar. */
98 inline Vec4ub operator * (float rhs) const
99 {
100 Vec4ub col(*this);
101 col *= rhs;
102 return col;
103 }
104
105 /** Unary multiply by scalar. */
106 inline Vec4ub& operator *= (float rhs)
107 {
108 _v[0]=(value_type)((float)_v[0]*rhs);
109 _v[1]=(value_type)((float)_v[1]*rhs);
110 _v[2]=(value_type)((float)_v[2]*rhs);
111 _v[3]=(value_type)((float)_v[3]*rhs);
112 return *this;
113 }
114
115 /** Divide by scalar. */
116 inline Vec4ub operator / (float rhs) const
117 {
118 Vec4ub col(*this);
119 col /= rhs;
120 return col;
121 }
122
123 /** Unary divide by scalar. */
124 inline Vec4ub& operator /= (float rhs)
125 {
126 float div = 1.0f/rhs;
127 *this *= div;
128 return *this;
129 }
130
131 /** Binary vector add. */
132 inline Vec4ub operator + (const Vec4ub& rhs) const
133 {
134 return Vec4ub(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
135 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
136 }
137
138 /** Unary vector add. Slightly more efficient because no temporary
139 * intermediate object.
140 */
141 inline Vec4ub& operator += (const Vec4ub& rhs)
142 {
143 _v[0] += rhs._v[0];
144 _v[1] += rhs._v[1];
145 _v[2] += rhs._v[2];
146 _v[3] += rhs._v[3];
147 return *this;
148 }
149
150 /** Binary vector subtract. */
151 inline Vec4ub operator - (const Vec4ub& rhs) const
152 {
153 return Vec4ub(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
154 _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
155 }
156
157 /** Unary vector subtract. */
158 inline Vec4ub& operator -= (const Vec4ub& rhs)
159 {
160 _v[0]-=rhs._v[0];
161 _v[1]-=rhs._v[1];
162 _v[2]-=rhs._v[2];
163 _v[3]-=rhs._v[3];
164 return *this;
165 }
166
167}; // end of class Vec4ub
168
169inline Vec4ub convertToRGBA8(const Vec4f& color)
170{
171 return Vec4ub(static_cast<unsigned char>(color.r()*255.0f),
172 static_cast<unsigned char>(color.g()*255.0f),
173 static_cast<unsigned char>(color.b()*255.0f),
174 static_cast<unsigned char>(color.a()*255.0f));
175
176}
177
178inline Vec4ub convertToRGBA8(float r, float g, float b, float a)
179{
180 return Vec4ub(static_cast<unsigned char>(r*255.0f),
181 static_cast<unsigned char>(g*255.0f),
182 static_cast<unsigned char>(b*255.0f),
183 static_cast<unsigned char>(a*255.0f));
184
185}
186
187} // end of namespace osg
188
189#endif