1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
/*!
\class SbColor4f SbColor4f.h Inventor/SbColor4f.h
\brief The SbColor4f class contains the red, green, blue and alpha
components which make up a color value.
\ingroup base
This class is used internally within other classes in Coin. It contains
a 4 component vector as a position in the RGB cube with an additional
transparency value.
The red, green and blue values should be between 0.0 and 1.0, where
0.0 is interpreted as minimum intensity, and 1.0 is maximum intensity.
The transparency value is also between 0.0 and 1.0.
SbColor4f contains methods for convenient handling of setting and
getting color values as 32 bit packed values or as HSV values.
Note: this class is not part of Open Inventor, but is an extension to
the API. Don't use it if you want your code to be compatible with
Open Inventor.
\sa SbColor
*/
#include <cassert>
#include <Inventor/SbColor4f.h>
#include <Inventor/SbVec4f.h>
#if COIN_DEBUG
#include <Inventor/errors/SoDebugError.h>
#endif // COIN_DEBUG
/*!
Default constructor. The color value will be uninitialized until
explicitly set.
*/
SbColor4f::SbColor4f(void)
{
}
/*!
Construct and initialize an SbColor4f with the red, green, blue
and alpha values given by the \c v vector.
*/
SbColor4f::SbColor4f(const SbVec4f& v)
{
this->vec[0] = v[0];
this->vec[1] = v[1];
this->vec[2] = v[2];
this->vec[3] = v[3];
}
/*!
Construct and initialize an SbColor4f with the red, green, blue
and alpha taken from given \c rgba array
*/
SbColor4f::SbColor4f(const float* const rgba)
{
this->vec[0] = rgba[0];
this->vec[1] = rgba[1];
this->vec[2] = rgba[2];
this->vec[3] = rgba[3];
}
/*!
Construct and initialize an SbColor4f with the red, green and blue
components from the SbColor \c rgb, and the alpha value from the
supplied \c alpha argument.
*/
SbColor4f::SbColor4f(const SbColor &rgb, const float alpha)
{
this->vec[0] = rgb[0];
this->vec[1] = rgb[1];
this->vec[2] = rgb[2];
this->vec[3] = alpha;
}
/*!
Construct and initialize an SbColor4f with the given red, green, blue
and alpha values.
*/
SbColor4f::SbColor4f(const float r, const float g, const float b,
const float a)
{
this->vec[0] = r;
this->vec[1] = g;
this->vec[2] = b;
this->vec[3] = a;
}
/*!
Set a new color.
*/
void
SbColor4f::setValue(const float r, const float g, const float b,
const float a)
{
this->vec[0] = r;
this->vec[1] = g;
this->vec[2] = b;
this->vec[3] = a;
}
/*!
Set a new color. The elements of the array will be read in turned
as red, green, blue and transparency.
*/
void
SbColor4f::setValue(const float col[4])
{
this->vec[0] = col[0];
this->vec[1] = col[1];
this->vec[2] = col[2];
this->vec[3] = col[3];
}
/*!
Return pointer to array of 4 float values representing the red, green,
blue and transparency values of the color.
*/
const float *
SbColor4f::getValue() const
{
return this->vec;
}
/*!
Return components of the stored color.
*/
void
SbColor4f::getValue(float &r, float &g, float &b, float &a)
{
r = this->vec[0];
g = this->vec[1];
b = this->vec[2];
a = this->vec[3];
}
/*!
Set the color value as a 32 bit combined red/green/blue/alpha value.
Each component is 8 bit wide (i.e. from 0x00 to 0xff), and the red
value should be stored leftmost, like this: 0xRRGGBBAA.
\sa getPackedValue().
*/
SbColor4f&
SbColor4f::setPackedValue(const uint32_t rgba)
{
this->setValue((rgba >> 24)/255.0f,
((rgba >> 16)&0xff)/255.0f,
((rgba >> 8)&0xff)/255.0f,
(rgba & 0xff)/255.0f);
return *this;
}
/*!
Return color as a 32 bit packed integer in the form 0xRRGGBBAA.
\sa setPackedValue().
*/
uint32_t
SbColor4f::getPackedValue() const
{
return ((static_cast<uint32_t>(red()*255.0f + 0.5f) << 24) |
(static_cast<uint32_t>(green()*255.0f + 0.5f) << 16) |
(static_cast<uint32_t>(blue()*255.0f + 0.5f) << 8) |
static_cast<uint32_t>(alpha()*255.0f + 0.5f));
}
/*!
Sets the RGB components of the color. The alpha component is
left unchanged.
*/
SbColor4f&
SbColor4f::setRGB(const SbColor &col)
{
this->vec[0] = col[0];
this->vec[1] = col[1];
this->vec[2] = col[2];
return *this;
}
/*!
Returns the RGB components of this color.
*/
void
SbColor4f::getRGB(SbColor &color)
{
color[0] = this->red();
color[1] = this->green();
color[2] = this->blue();
}
/*!
Set the color as a \c hue, \c saturation, \c value triplet.
The hue component should be normalized to within [0, 1] before you
call this method, where 0 is equal to 0 and 1 is equal to 360.
\sa getHSVValue().
*/
SbColor4f&
SbColor4f::setHSVValue(float hue, float saturation,
float value, float alpha)
{
#if COIN_DEBUG
if (!(hue>=0.0f && hue<=1.0f)) {
SoDebugError::postWarning("SbColor4f::setHSVValue",
"'hue' (%f) not within [0.0,1.0]; clamping.",
hue);
if (hue<0.0f) hue=0.0f;
else if (hue>1.0f) hue=1.0f;
}
if (!(saturation>=0.0f && saturation<=1.0f)) {
SoDebugError::postWarning("SbColor4f::setHSVValue",
"'saturation' (%f) not within [0.0,1.0]; "
"clamping.", saturation);
if (saturation<0.0f) saturation=0.0f;
else if (saturation>1.0f) saturation=1.0f;
}
if (!(value>=0.0f && value<=1.0f)) {
SoDebugError::postWarning("SbColor4f::setHSVValue",
"'value' (%f) not within [0.0,1.0]; clamping.",
value);
if (value<0.0f) value=0.0f;
else if (value>1.0f) value=1.0f;
}
if (!(alpha >= 0.0f && alpha <= 1.0f)) {
SoDebugError::postWarning("SbColor4f::setHSVValue",
"'alpha' (%f) not within [0.0,1.0]; clamping.",
alpha);
alpha = SbClamp(alpha, 0.0f, 1.0f);
}
#endif // COIN_DEBUG
SbColor col;
col.setHSVValue(hue, saturation, value);
this->vec[0] = col[0];
this->vec[1] = col[1];
this->vec[2] = col[2];
this->vec[3] = alpha;
return *this;
}
/*!
Return the color as a \c hue, \c saturation, \c value triplet. Alpha
component is ignored.
\sa setHSVValue().
*/
void
SbColor4f::getHSVValue(float &h, float &s, float &v) const
{
SbColor col;
col[0] = this->vec[0];
col[1] = this->vec[1];
col[2] = this->vec[2];
col.getHSVValue(h, s, v);
}
/*!
Set the color as a \c hue, \c saturation, \c value triplet.
The hue component should be normalized to within [0, 1] before you
call this method, where 0 is equal to 0 and 1 is equal to 360.
\sa getHSVValue().
*/
SbColor4f&
SbColor4f::setHSVValue(const float hsv[3], float a)
{
return this->setHSVValue(hsv[0], hsv[1], hsv[2], a);
}
/*!
Return the color as a \c hue, \c saturation, \c value triplet. Alpha
component is ignored.
\sa setHSVValue().
*/
void
SbColor4f::getHSVValue(float hsv[3]) const
{
this->getHSVValue(hsv[0], hsv[1], hsv[2]);
}
/*!
Returns the color component represented by the given index \a idx.
0 is red, 1 is green, 2 is blue and 3 is the transparency value.
*/
//$ EXPORT INLINE
float
SbColor4f::operator[](const int idx) const
{
return this->vec[idx];
}
/*!
Returns the color component represented by the given index \a idx.
0 is red, 1 is green, 2 is blue and 3 is the transparency value.
*/
//$ EXPORT INLINE
float &
SbColor4f::operator[](const int idx)
{
return this->vec[idx];
}
/*!
Multiplies the RGB components by \c d. The alpha component is left
unchanged.
*/
SbColor4f &
SbColor4f::operator*=(const float d)
{
this->vec[0] *= d;
this->vec[1] *= d;
this->vec[2] *= d;
return *this;
}
/*!
Divides the RGB components by \c d. The alpha component is left
unchanged.
*/
SbColor4f &
SbColor4f::operator/=(const float d)
{
this->vec[0] /= d;
this->vec[1] /= d;
this->vec[2] /= d;
return *this;
}
/*!
Adds the RGB components. Alpha is ignored.
*/
SbColor4f &
SbColor4f::operator+=(const SbColor4f &c)
{
this->vec[0] += c[0];
this->vec[1] += c[1];
this->vec[2] += c[2];
return *this;
}
/*!
Subtracts the RGB components. Alpha is ignored.
*/
SbColor4f &
SbColor4f::operator-=(const SbColor4f &c)
{
this->vec[0] -= c[0];
this->vec[1] -= c[1];
this->vec[2] -= c[2];
return *this;
}
/*!
Multiplies the RGB components by \c d. Alpha is left unchanged.
*/
SbColor4f
operator *(const SbColor4f &c, const float d)
{
return SbColor4f(c.vec[0]*d, c.vec[1]*d, c.vec[2]*d, c.vec[3]);
}
/*!
Multiplies the RGB components by \c d. Alpha is left unchanged.
*/
SbColor4f
operator *(const float d, const SbColor4f &c)
{
return SbColor4f(c.vec[0]*d, c.vec[1]*d, c.vec[2]*d, c.vec[3]);
}
/*!
Divides the RGB components by \c d. Alpha is left unchanged.
*/
SbColor4f
operator /(const SbColor4f &c, const float d)
{
assert(d != 0.0f);
float inv = 1.0f / d;
return SbColor4f(c.vec[0]*inv,
c.vec[1]*inv,
c.vec[2]*inv,
c.vec[3]);
}
/*!
Adds the RGB components of the two colors. Alpha is taken from the
first color (\c v1).
*/
SbColor4f
operator +(const SbColor4f &v1, const SbColor4f &v2)
{
return SbColor4f(v1.vec[0] + v2.vec[0],
v1.vec[1] + v2.vec[1],
v1.vec[2] + v2.vec[2],
v1.vec[3]);
}
/*!
Subtracts the RGB components of the two colors. Alpha is taken from the
first color (\c v1).
*/
SbColor4f
operator -(const SbColor4f &v1, const SbColor4f &v2)
{
return SbColor4f(v1.vec[0] - v2.vec[0],
v1.vec[1] - v2.vec[1],
v1.vec[2] - v2.vec[2],
v1.vec[3]);
}
/*!
Check if two colors are equal. Returns 1 if equal, 0 if unequal.
*/
int
operator ==(const SbColor4f &v1, const SbColor4f &v2)
{
return (v1.vec[0] == v2.vec[0] &&
v1.vec[1] == v2.vec[1] &&
v1.vec[2] == v2.vec[2] &&
v1.vec[3] == v2.vec[3]);
}
/*!
Check if two colors are unequal. Returns 0 if equal, 1 if unequal.
*/
//$ EXPORT INLINE
int
operator !=(const SbColor4f &v1, const SbColor4f &v2)
{
return !(v1 == v2);
}
|