AnimatLab  2
Test
StdColor.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 
9 namespace StdUtils
10 {
11 
12 
14 {
15  m_fltR = 0;
16  m_fltB = 0;
17  m_fltG = 0;
18  m_fltA = 0;
19  m_fltMaxRange = 1;
20 }
21 
22 
23 CStdColor::CStdColor(float fltMaxRange)
24 {
25  m_fltR = 0;
26  m_fltB = 0;
27  m_fltG = 0;
28  m_fltA = 0;
29  m_fltMaxRange = fltMaxRange;
30 }
31 
32 CStdColor::CStdColor(float valr, float valg, float valb, float vala, float fltMaxRange)
33 {
34  m_fltMaxRange = fltMaxRange;
35 
36  r(valr);
37  g(valg);
38  b(valb);
39  a(vala);
40 }
41 
51 void CStdColor::r(float fltR, bool bThrowError)
52 {
53  if(Std_InValidRange((float) 0, (float) 1, fltR, bThrowError, "R"))
54  m_fltR = fltR;
55 }
56 
65 float CStdColor::r() {return m_fltR;}
66 
76 void CStdColor::g(float fltG, bool bThrowError)
77 {
78  if(Std_InValidRange((float) 0, (float) 1, fltG, bThrowError, "G"))
79  m_fltG = fltG;
80 }
81 
90 float CStdColor::g() {return m_fltG;}
91 
101 void CStdColor::b(float fltB, bool bThrowError)
102 {
103  if(Std_InValidRange((float) 0, (float) 1, fltB, bThrowError, "B"))
104  m_fltB = fltB;
105 }
106 
115 float CStdColor::b() {return m_fltB;}
116 
126 void CStdColor::a(float fltA, bool bThrowError)
127 {
128  if(Std_InValidRange((float) 0, (float) 1, fltA, bThrowError, "A"))
129  m_fltA = fltA;
130 }
131 
140 float CStdColor::a() {return m_fltA;}
141 
153 void CStdColor::Set(float R, float G, float B, float A)
154 {
155  r(R);
156  g(G);
157  b(B);
158  a(A);
159 }
160 
171 bool CStdColor::operator==(const CStdColor &oPoint)
172 {
173  if( (m_fltR == oPoint.m_fltR) && (m_fltB == oPoint.m_fltB) && (m_fltG == oPoint.m_fltG) && (m_fltA == oPoint.m_fltA) )
174  return true;
175  return false;
176 };
177 
188 bool CStdColor::operator!=(const CStdColor &oPoint)
189 {
190  if( (m_fltR == oPoint.m_fltR) && (m_fltB == oPoint.m_fltB) && (m_fltG == oPoint.m_fltG) && (m_fltA == oPoint.m_fltA) )
191  return false;
192  return true;
193 };
194 
203 void CStdColor::operator=(const CStdColor &oPoint)
204 {
205  m_fltR=oPoint.m_fltR;
206  m_fltG=oPoint.m_fltG;
207  m_fltB=oPoint.m_fltB;
208  m_fltA=oPoint.m_fltA;
209 };
210 
219 void CStdColor::operator+=(const CStdColor &oPoint)
220 {
221  m_fltR+=oPoint.m_fltR;
222  m_fltG+=oPoint.m_fltG;
223  m_fltB+=oPoint.m_fltB;
224  m_fltA+=oPoint.m_fltA;
225 };
226 
235 void CStdColor::operator-=(const CStdColor &oPoint)
236 {
237  m_fltR-=oPoint.m_fltR;
238  m_fltG-=oPoint.m_fltG;
239  m_fltB-=oPoint.m_fltB;
240  m_fltA-=oPoint.m_fltA;
241 };
242 
254 {
255  CStdColor oNewPoint(m_fltMaxRange);
256 
257  oNewPoint.m_fltR = m_fltR + oPoint.m_fltR;
258  oNewPoint.m_fltG = m_fltG + oPoint.m_fltG;
259  oNewPoint.m_fltB = m_fltB + oPoint.m_fltB;
260  oNewPoint.m_fltA = m_fltA + oPoint.m_fltA;
261  return oNewPoint;
262 };
263 
275 {
276  CStdColor oNewPoint(m_fltMaxRange);
277 
278  oNewPoint.m_fltR = m_fltR - oPoint.m_fltR;
279  oNewPoint.m_fltG = m_fltG - oPoint.m_fltG;
280  oNewPoint.m_fltB = m_fltB - oPoint.m_fltB;
281  oNewPoint.m_fltA = m_fltA - oPoint.m_fltA;
282  return oNewPoint;
283 };
284 
293 void CStdColor::operator+=(const float fltVal)
294 {
295  m_fltR+=fltVal;
296  m_fltG+=fltVal;
297  m_fltB+=fltVal;
298  m_fltA+=fltVal;
299 };
300 
309 void CStdColor::operator-=(const float fltVal)
310 {
311  m_fltR-=fltVal;
312  m_fltG-=fltVal;
313  m_fltB-=fltVal;
314  m_fltA-=fltVal;
315 };
316 
325 void CStdColor::operator*=(const float fltVal)
326 {
327  m_fltR*=fltVal;
328  m_fltG*=fltVal;
329  m_fltB*=fltVal;
330  m_fltA*=fltVal;
331 };
332 
341 void CStdColor::operator/=(const float fltVal)
342 {
343  if(fltVal)
344  {
345  m_fltR/=fltVal;
346  m_fltG/=fltVal;
347  m_fltB/=fltVal;
348  m_fltA/=fltVal;
349  }
350  else
351  THROW_ERROR(Std_Err_lDivByZero, Std_Err_strDivByZero);
352 };
353 
364 CStdColor CStdColor::operator+(const float fltVal)
365 {
366  CStdColor oNewPoint(m_fltMaxRange);
367 
368  oNewPoint.m_fltR = m_fltR + fltVal;
369  oNewPoint.m_fltG = m_fltG + fltVal;
370  oNewPoint.m_fltB = m_fltB + fltVal;
371  oNewPoint.m_fltA = m_fltA + fltVal;
372  return oNewPoint;
373 };
374 
385 CStdColor CStdColor::operator-(const float fltVal)
386 {
387  CStdColor oNewPoint(m_fltMaxRange);
388 
389  oNewPoint.m_fltR = m_fltR - fltVal;
390  oNewPoint.m_fltG = m_fltG - fltVal;
391  oNewPoint.m_fltB = m_fltB - fltVal;
392  oNewPoint.m_fltA = m_fltA - fltVal;
393  return oNewPoint;
394 };
395 
406 CStdColor CStdColor::operator*(const float fltVal)
407 {
408  CStdColor oNewPoint(m_fltMaxRange);
409 
410  oNewPoint.m_fltR = m_fltR * fltVal;
411  oNewPoint.m_fltG = m_fltG * fltVal;
412  oNewPoint.m_fltB = m_fltB * fltVal;
413  oNewPoint.m_fltA = m_fltA * fltVal;
414  return oNewPoint;
415 };
416 
427 CStdColor CStdColor::operator/(const float fltVal)
428 {
429  if(!fltVal)
430  THROW_ERROR(Std_Err_lDivByZero, Std_Err_strDivByZero);
431 
432  CStdColor oNewPoint(m_fltMaxRange);
433 
434  oNewPoint.m_fltR = m_fltR / fltVal;
435  oNewPoint.m_fltG = m_fltG / fltVal;
436  oNewPoint.m_fltB = m_fltB / fltVal;
437  oNewPoint.m_fltA = m_fltA / fltVal;
438  return oNewPoint;
439 };
440 
450 {return sqrt( (m_fltR*m_fltR) + (m_fltB*m_fltB) + (m_fltG*m_fltG) + (m_fltA*m_fltA) );};
451 
459 {
460  double dblMag = Magnitude();
461 
462  if(dblMag > 0)
463  {
464  m_fltR/=dblMag;
465  m_fltG/=dblMag;
466  m_fltB/=dblMag;
467  m_fltA/=dblMag;
468  }
469  else
470  {
471  m_fltR=1;
472  m_fltG=0;
473  m_fltB=0;
474  m_fltA=1;
475  }
476 };
477 
478 
490 void CStdColor::ClearNearZero(float fltTolerance)
491 {
492  if(fabs(m_fltR) < fltTolerance)
493  m_fltR = 0;
494  if(fabs(m_fltG) < fltTolerance)
495  m_fltG = 0;
496  if(fabs(m_fltB) < fltTolerance)
497  m_fltB = 0;
498  if(fabs(m_fltA) < fltTolerance)
499  m_fltA = 0;
500 }
501 
512 float CStdColor::operator[](const int iIndex)
513 {
514  switch(iIndex)
515  {
516  case 0:
517  return m_fltR;
518  case 1:
519  return m_fltG;
520  case 2:
521  return m_fltB;
522  case 3:
523  return m_fltA;
524  default:
525  THROW_PARAM_ERROR(Std_Err_lInvalidIndex, Std_Err_strInvalidIndex, "Index", iIndex);
526  }
527  return 0;
528 };
529 
540 void CStdColor::Load(CStdXml &oXml, std::string strParamName, bool bThrowError)
541 {
542  if(oXml.FindChildElement(strParamName, bThrowError))
543  {
544  oXml.IntoChildElement(strParamName);
545  r(oXml.GetAttribFloat("Red"), bThrowError);
546  g(oXml.GetAttribFloat("Green"), bThrowError);
547  b(oXml.GetAttribFloat("Blue"), bThrowError);
548  a(oXml.GetAttribFloat("Alpha"), bThrowError);
549  oXml.OutOfElem();
550  }
551  else if(bThrowError)
552  THROW_TEXT_ERROR(Std_Err_lParamNotFound, Std_Err_strParamNotFound, strParamName);
553  //If not found then use the default settings.
554 }
555 
566 void CStdColor::Load(std::string strXml, std::string strParamName, bool bThrowError)
567 {
568  CStdXml oXml;
569  oXml.Deserialize(strXml);
570  oXml.FindElement("Root");
571  oXml.FindChildElement(strParamName);
572 
573  Load(oXml, strParamName, bThrowError);
574 }
575 
576 
577 } //StdUtils
virtual void Deserialize(std::string &strXml)
Deserializes a string into an xml document.
Definition: StdXml.cpp:162
Standard color class.
Definition: StdColor.h:22
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
void Set(float R, float G, float B, float A)
Sets the color values.
Definition: StdColor.cpp:153
void Load(CStdXml &oXml, std::string strParamName, bool bThrowError=true)
Loads the color.
Definition: StdColor.cpp:540
void Normalize()
Normalizes the color.
Definition: StdColor.cpp:458
virtual bool FindElement(std::string strElementName, bool fThrowError=true)
Finds an element with the specified name.
Definition: StdXml.cpp:179
double Magnitude()
Gets the magnitude of the color.
Definition: StdColor.cpp:449
float b()
Gets the blue value of the color.
Definition: StdColor.cpp:115
void operator/=(const float fltVal)
/= casting operator.
Definition: StdColor.cpp:341
float g()
Gets the green value of the color.
Definition: StdColor.cpp:90
float m_fltG
The green component of the color.
Definition: StdColor.h:32
CStdColor operator+(const CStdColor &oPoint)
casting operator.
Definition: StdColor.cpp:253
CStdColor operator-(const CStdColor &oPoint)
casting operator.
Definition: StdColor.cpp:274
bool Std_InValidRange(int iMinVal, int iMaxVal, int iVal, bool bThrowError, std::string strParamName)
Tests whether a number is within a valid range.
float m_fltR
The red component of the color.
Definition: StdColor.h:26
float m_fltMaxRange
some colors are 0-255, others are 0-1. This specifies the max range for these colors ...
Definition: StdColor.h:38
void operator+=(const CStdColor &oPoint)
+= casting operator.
Definition: StdColor.cpp:219
bool operator==(const CStdColor &oPoint)
== casting operator.
Definition: StdColor.cpp:171
A standard xml manipulation class.
Definition: StdXml.h:19
virtual float GetAttribFloat(std::string strAttribName, bool bThrowError=true, float fltDefault=0)
Gets an attribute float.
Definition: StdXml.cpp:1095
float m_fltB
The blue component of the color.
Definition: StdColor.h:29
float a()
Gets the alpha value of the color.
Definition: StdColor.cpp:140
Namespace for the standard utility objects.
Definition: MarkupSTL.cpp:19
CStdColor operator*(const float fltVal)
casting operator.
Definition: StdColor.cpp:406
bool operator!=(const CStdColor &oPoint)
!= casting operator.
Definition: StdColor.cpp:188
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
void operator*=(const float fltVal)
*= casting operator.
Definition: StdColor.cpp:325
void ClearNearZero(float fltTolerance=1e-5f)
Clears the near zero described by fltTolerance.
Definition: StdColor.cpp:490
virtual bool IntoChildElement(std::string strElementName, bool bThrowError=true)
Goes into the child element with the specified name.
Definition: StdXml.cpp:278
CStdColor operator/(const float fltVal)
/ casting operator.
Definition: StdColor.cpp:427
float m_fltA
The alpha component of the color.
Definition: StdColor.h:35
float operator[](const int iIndex)
[] casting operator.
Definition: StdColor.cpp:512
CStdColor()
Default constructor.
Definition: StdColor.cpp:13
float r()
Gets the red value of the color.
Definition: StdColor.cpp:65
void operator=(const CStdColor &oPoint)
= casting operator.
Definition: StdColor.cpp:203
void operator-=(const CStdColor &oPoint)
-= casting operator.
Definition: StdColor.cpp:235