CFastColor.h
Go to the documentation of this file.
1 #ifndef icmm_CFastColor_included
2 #define icmm_CFastColor_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtGlobal>
7 #if QT_VERSION >= 0x050000
8 #include <QtCore/QtMath>
9 #else
10 #include <QtCore/qmath.h>
11 #endif
12 
13 // ACF includes
14 #include <iser/ISerializable.h>
15 #include <imath/IDoubleManip.h>
16 #include <imath/TFastVector.h>
17 #include <icmm/CVarColor.h>
18 #include <icmm/CLab.h>
19 
20 
21 namespace icmm
22 {
23 
24 
25 enum{
26 #ifdef COLOR_COMPONENTS_COUNT
27  MAX_COLOR_COMPONENTS_COUNT=COLOR_COMPONENTS_COUNT
28 #else
30 #endif // COLOR_COMPONENTS_COUNT
31 };
32 
33 
38 class CFastColor: public imath::TFastVector<MAX_COLOR_COMPONENTS_COUNT>
39 {
40 public:
42 
47  CFastColor();
48 
52  explicit CFastColor(int componentsCount, double value = 0);
53 
57  CFastColor(const CFastColor& color);
58 
59  template <int Size>
61  : BaseClass(vector)
62  {
63  }
64 
65  CFastColor(const icmm::CVarColor& color);
66 
67  icmm::CLab GetAsLab() const;
68  void SetAsLab(const icmm::CLab& lab);
69 
70  bool EnsureElementsCount(int count, double value = 0);
71 
77  void GetRounded(const imath::IDoubleManip& manipulator, CFastColor& result);
78 
84  bool IsRoundedEqual(const CFastColor& color, const imath::IDoubleManip& manipulator) const;
85 
89  bool IsSimilar(const CFastColor& color, double tolerance = I_BIG_EPSILON) const;
90 
95  bool IsNormalized() const;
96 
101  void Normalize();
102 
107  void GetNormalized(CFastColor& result) const;
108 
109  operator icmm::CVarColor() const;
110 
111  bool operator==(const CFastColor& vector) const;
112  bool operator!=(const CFastColor& vector) const;
113  bool operator<(const CFastColor& vector) const;
114  bool operator>(const CFastColor& vector) const;
115  bool operator<=(const CFastColor& vector) const;
116  bool operator>=(const CFastColor& vector) const;
117 
118  CFastColor operator+(const CFastColor& color) const;
119  CFastColor operator-(const CFastColor& color) const;
120  CFastColor operator*(const CFastColor& color) const;
121  CFastColor operator/(const CFastColor& color) const;
122 
123  CFastColor operator*(double value) const;
124  CFastColor operator/(double value) const;
125 
126  CFastColor& operator=(const CFastColor& color);
127 
128  const CFastColor& operator+=(const CFastColor& color);
129  const CFastColor& operator-=(const CFastColor& color);
130  const CFastColor& operator*=(const CFastColor& color);
131  const CFastColor& operator/=(const CFastColor& color);
132 
133  const CFastColor& operator*=(double value);
134  const CFastColor& operator/=(double value);
135 
136  // not reimplemented (iser::ISerializable)
137  bool Serialize(iser::IArchive& archive);
138 };
139 
140 
141 // inline methods
142 
144 {
145 #ifdef COLOR_COMPONENTS_COUNT
146  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
147  m_elements[i] = 0;
148  }
149 #endif // COLOR_COMPONENTS_COUNT
150 }
151 
152 
153 inline CFastColor::CFastColor(int componentsCount, double value)
154 #ifndef COLOR_COMPONENTS_COUNT
155 : BaseClass(componentsCount, value)
156 #endif // !COLOR_COMPONENTS_COUNT
157 {
158 #ifdef COLOR_COMPONENTS_COUNT
159  m_elementsCount = componentsCount;
160 
161  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
162  m_elements[i] = value;
163  }
164 #endif // COLOR_COMPONENTS_COUNT
165 }
166 
167 
168 inline CFastColor::CFastColor(const CFastColor& color)
169 #ifndef COLOR_COMPONENTS_COUNT
170 : BaseClass(color)
171 #endif // !COLOR_COMPONENTS_COUNT
172 {
173 #ifdef COLOR_COMPONENTS_COUNT
174  m_elementsCount = color.m_elementsCount;
175 
176  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
177  m_elements[i] = color.m_elements[i];
178  }
179 #endif // COLOR_COMPONENTS_COUNT
180 }
181 
182 
183 inline bool CFastColor::EnsureElementsCount(int count, double value)
184 {
185  if (count > GetElementsCount()){
186  return SetElementsCount(count, value);
187  }
188 
189  return true;
190 }
191 
192 
193 inline bool CFastColor::IsSimilar(const CFastColor& color, double tolerance) const
194 {
195  return GetDistance(color) <= tolerance;
196 }
197 
198 
199 inline bool CFastColor::IsNormalized() const
200 {
201  int elementsCount = GetElementsCount();
202 
203  for (int i = 0; i < elementsCount; ++i){
204  double component = m_elements[i];
205 
206  if ((component < 0) || (component > 1)){
207  return false;
208  }
209  }
210 
211  return true;
212 }
213 
214 
215 inline CFastColor::operator icmm::CVarColor() const
216 {
217  int elementsCount = GetElementsCount();
218 
219  icmm::CVarColor retVal(elementsCount);
220 
221  for (int i = 0; i < elementsCount; ++i){
222  retVal[i] = GetElement(i);
223  }
224 
225  return retVal;
226 }
227 
228 
229 inline bool CFastColor::operator==(const CFastColor& vector) const
230 {
231  int elementsCount = GetElementsCount();
232 
233  if (elementsCount != vector.GetElementsCount()){
234  return false;
235  }
236 
237  for (int i = 0; i < elementsCount; ++i){
238  if ( (operator[](i) > vector[i] + I_BIG_EPSILON) ||
239  (operator[](i) < vector[i] - I_BIG_EPSILON)){
240  return false;
241  }
242  }
243 
244  return true;
245 }
246 
247 
248 inline bool CFastColor::operator!=(const CFastColor& vector) const
249 {
250  return !operator==(vector);
251 }
252 
253 
254 inline bool CFastColor::operator<(const CFastColor& vector) const
255 {
256  int commonSize = qMin(GetElementsCount(), vector.GetElementsCount());
257 
258  for (int i = 0; i < commonSize; ++i){
259  if (operator[](i) > vector[i] + I_BIG_EPSILON){
260  return false;
261  }
262  else if (operator[](i) < vector[i] - I_BIG_EPSILON){
263  return true;
264  }
265  }
266 
267  return GetElementsCount() < vector.GetElementsCount();
268 }
269 
270 
271 inline bool CFastColor::operator>(const CFastColor& vector) const
272 {
273  int commonSize = qMin(GetElementsCount(), vector.GetElementsCount());
274  for (int i = 0; i < commonSize; ++i){
275  if (operator[](i) > vector[i] + I_BIG_EPSILON){
276  return true;
277  }
278  else if (operator[](i) < vector[i] - I_BIG_EPSILON){
279  return false;
280  }
281  }
282 
283  return GetElementsCount() > vector.GetElementsCount();
284 }
285 
286 
287 inline bool CFastColor::operator<=(const CFastColor& vector) const
288 {
289  int commonSize = qMin(GetElementsCount(), vector.GetElementsCount());
290  for (int i = 0; i < commonSize; ++i){
291  if (operator[](i) > vector[i] + I_BIG_EPSILON){
292  return false;
293  }
294  else if (operator[](i) < vector[i] - I_BIG_EPSILON){
295  return true;
296  }
297  }
298 
299  return GetElementsCount() <= vector.GetElementsCount();
300 }
301 
302 
303 inline bool CFastColor::operator>=(const CFastColor& vector) const
304 {
305  int commonSize = qMin(GetElementsCount(), vector.GetElementsCount());
306  for (int i = 0; i < commonSize; ++i){
307  if (operator[](i) > vector[i] + I_BIG_EPSILON){
308  return true;
309  }
310  else if (operator[](i) < vector[i] - I_BIG_EPSILON){
311  return false;
312  }
313  }
314 
315  return GetElementsCount() >= vector.GetElementsCount();
316 }
317 
318 
319 inline CFastColor CFastColor::operator+(const CFastColor& color) const
320 {
321  CFastColor retVal(*this);
322 
323  retVal += color;
324 
325  return retVal;
326 }
327 
328 
329 inline CFastColor CFastColor::operator-(const CFastColor& color) const
330 {
331  CFastColor retVal(*this);
332 
333  retVal -= color;
334 
335  return retVal;
336 }
337 
338 
339 inline CFastColor CFastColor::operator*(const CFastColor& color) const
340 {
341  CFastColor retVal(*this);
342 
343  retVal *= color;
344 
345  return retVal;
346 }
347 
348 
349 inline CFastColor CFastColor::operator/(const CFastColor& color) const
350 {
351  CFastColor retVal(*this);
352 
353  retVal /= color;
354 
355  return retVal;
356 }
357 
358 
359 inline CFastColor CFastColor::operator*(double value) const
360 {
361  int elementsCount = GetElementsCount();
362 
363  CFastColor retVal(elementsCount);
364 
365 #ifdef COLOR_COMPONENTS_COUNT
366  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
367 #else
368  for (int i = 0; i < elementsCount; ++i){
369 #endif // COLOR_COMPONENTS_COUNT
370  retVal.m_elements[i] = m_elements[i] * value;
371  }
372 
373  return retVal;
374 }
375 
376 
377 inline CFastColor CFastColor::operator/(double value) const
378 {
379  int elementsCount = GetElementsCount();
380 
381  CFastColor retVal(elementsCount);
382 
383 #ifdef COLOR_COMPONENTS_COUNT
384  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
385 #else
386  for (int i = 0; i < elementsCount; ++i){
387 #endif // COLOR_COMPONENTS_COUNT
388  retVal.m_elements[i] = m_elements[i] / value;
389  }
390 
391  return retVal;
392 }
393 
394 
396 {
397 #ifdef COLOR_COMPONENTS_COUNT
399  Q_ASSERT(m_elementsCount <= COLOR_COMPONENTS_COUNT);
400 
401  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
402  m_elements[i] = color.m_elements[i];
403  }
404 #else
405  BaseClass::operator=(color);
406 #endif // COLOR_COMPONENTS_COUNT
407 
408  return *this;
409 }
410 
411 
412 inline const CFastColor& CFastColor::operator+=(const CFastColor& color)
413 {
414 #ifdef COLOR_COMPONENTS_COUNT
416  Q_ASSERT(m_elementsCount <= COLOR_COMPONENTS_COUNT);
417 
418  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
419  m_elements[i] += color.m_elements[i];
420  }
421 #else
422  BaseClass::operator+=(color);
423 #endif // COLOR_COMPONENTS_COUNT
424 
425  return *this;
426 }
427 
428 
429 inline const CFastColor& CFastColor::operator-=(const CFastColor& color)
430 {
431 #ifdef COLOR_COMPONENTS_COUNT
433  Q_ASSERT(m_elementsCount <= COLOR_COMPONENTS_COUNT);
434 
435  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
436  m_elements[i] -= color.m_elements[i];
437  }
438 #else
439  BaseClass::operator-=(color);
440 #endif // COLOR_COMPONENTS_COUNT
441 
442  return *this;
443 }
444 
445 
446 inline const CFastColor& CFastColor::operator*=(const CFastColor& color)
447 {
448 #ifdef COLOR_COMPONENTS_COUNT
449  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
450 #else
451  int elementsCount = qMin(GetElementsCount(), color.GetElementsCount());
452  for (int i = 0; i < elementsCount; ++i){
453 #endif // COLOR_COMPONENTS_COUNT
454  m_elements[i] *= color.m_elements[i];
455  }
456 
457  return *this;
458 }
459 
460 
461 inline const CFastColor& CFastColor::operator/=(const CFastColor& color)
462 {
463 #ifdef COLOR_COMPONENTS_COUNT
464  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
465 #else
466  int elementsCount = qMin(GetElementsCount(), color.GetElementsCount());
467  for (int i = 0; i < elementsCount; ++i){
468 #endif // COLOR_COMPONENTS_COUNT
469  m_elements[i] /= color.m_elements[i];
470  }
471 
472  return *this;
473 }
474 
475 
476 inline const CFastColor& CFastColor::operator*=(double value)
477 {
478 #ifdef COLOR_COMPONENTS_COUNT
479  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
480  m_elements[i] *= value;
481  }
482 #else
483  BaseClass::operator*=(value);
484 #endif // COLOR_COMPONENTS_COUNT
485 
486  return *this;
487 }
488 
489 
490 inline const CFastColor& CFastColor::operator/=(double value)
491 {
492 #ifdef COLOR_COMPONENTS_COUNT
493  for (int i = 0; i < COLOR_COMPONENTS_COUNT; ++i){
494  m_elements[i] /= value;
495  }
496 #else
497  BaseClass::operator/=(value);
498 #endif // COLOR_COMPONENTS_COUNT
499 
500  return *this;
501 }
502 
503 
504 // related global functions
505 
506 uint qHash(const CFastColor& color, uint seed = 0);
507 
508 
509 } // namespace icmm
510 
511 
512 #endif // !icmm_CFastColor_included
513 
514 
void GetRounded(const imath::IDoubleManip &manipulator, CFastColor &result)
Get color after components value rounding with specified precision.
bool IsNormalized() const
Check if this color value is normalized.
Definition: CFastColor.h:199
TFastVector< MaxSize, double > operator-() const
Color implementation with variable components number and fixed maximal number of components.
Definition: CFastColor.h:38
bool Serialize(iser::IArchive &archive)
TFastVector< MaxSize, double > & operator+=(const TFastVector< MaxSize, double > &vector)
bool SetElementsCount(int count, const double &value=double())
Set number of elements.
icmm::CLab GetAsLab() const
const CFastColor & operator/=(const CFastColor &color)
Definition: CFastColor.h:461
CFastColor(const imath::TVector< Size > &vector)
Definition: CFastColor.h:60
Interface for all manipulation using values represent as double.
Definition: IDoubleManip.h:20
TFastVector< MaxSize, double > & operator-=(const TFastVector< MaxSize, double > &vector)
Primitive for representation of LAB color value.
Definition: CLab.h:18
const CFastColor & operator*=(const CFastColor &color)
Definition: CFastColor.h:446
CFastColor()
Default constructor.
Definition: CFastColor.h:143
bool IsSimilar(const CFastColor &color, double tolerance=I_BIG_EPSILON) const
Allows to compare two colors with tolerance.
Definition: CFastColor.h:193
imath::TFastVector< MAX_COLOR_COMPONENTS_COUNT > BaseClass
Definition: CFastColor.h:41
bool operator==(const CFastColor &vector) const
Definition: CFastColor.h:229
Generic color implementation with variable number of color components.
Definition: CVarColor.h:21
TFastVector< MaxSize, double > & operator*=(doublescalar)
CFastColor operator/(const CFastColor &color) const
Definition: CFastColor.h:349
bool operator<=(const CFastColor &vector) const
Definition: CFastColor.h:287
Represent input/output persistence archive.
Definition: IArchive.h:30
double GetDistance(const TFastVector< MaxSize, double > &vector) const
Return distance between two vectors.
bool operator>(const CFastColor &vector) const
Definition: CFastColor.h:271
Simple implementation of fixed-size vector.
Definition: TFastVector.h:23
CFastColor operator+(const CFastColor &color) const
Definition: CFastColor.h:319
bool operator!=(const CFastColor &vector) const
Definition: CFastColor.h:248
CFastColor operator*(const CFastColor &color) const
Definition: CFastColor.h:339
bool operator>=(const CFastColor &vector) const
Definition: CFastColor.h:303
void GetNormalized(CFastColor &result) const
Get normalized color.
CFastColor & operator=(const CFastColor &color)
Definition: CFastColor.h:395
TFastVector< MaxSize, double > & operator=(const TFastVector< MaxSize, double > &vector)
const CFastColor & operator+=(const CFastColor &color)
Definition: CFastColor.h:412
static const double I_BIG_EPSILON
Definition: istd.h:26
void Normalize()
Make this color to be normalized.
bool operator<(const CFastColor &vector) const
Definition: CFastColor.h:254
const CFastColor & operator-=(const CFastColor &color)
Definition: CFastColor.h:429
uint qHash(const CFastColor &color, uint seed=0)
int GetElementsCount() const
Get number of elements.
TFastVector< MaxSize, double > & operator/=(doublescalar)
bool IsRoundedEqual(const CFastColor &color, const imath::IDoubleManip &manipulator) const
Check if two values are equal after rounding.
bool EnsureElementsCount(int count, double value=0)
Definition: CFastColor.h:183
void SetAsLab(const icmm::CLab &lab)

© 2007-2017 Witold Gantzke and Kirill Lepskiy