TColorGradient.h
Go to the documentation of this file.
1 #ifndef icmm_TColorGradient_included
2 #define icmm_TColorGradient_included
3 
4 
5 // ACF includes
6 #include <imath/TIMathFunction.h>
8 
9 
10 namespace icmm
11 {
12 
13 
15 {
16 public:
17  static double GetValue(double x)
18  {
19  return x;
20  }
21 };
22 
23 
27 template <class GradientFunction>
28 class TColorGradient: virtual public IColorTransformation
29 {
30 public:
32 
33  TColorGradient(const GradientColor& startColor, const GradientColor& endColor);
34 
35  // reimplemented (icmm::IColorTransformation)
36  virtual bool GetValueAt(const ArgumentType& argument, ResultType& result) const;
37  virtual ResultType GetValueAt(const ArgumentType& argument) const;
38 
39 private:
40  GradientColor m_startColor;
41  GradientColor m_endColor;
42 };
43 
44 
45 template <class GradientFunction>
47  :m_startColor(startColor),
48  m_endColor(endColor)
49 {
50 }
51 
52 
53 // reimplemented (icmm::IColorTransformation)
54 
55 template <class GradientFunction>
57 {
58  Q_ASSERT(argument.GetElementsCount() == 1);
59 
60  static istd::CRange normRange(0.0, 1.0);
61 
62  double argumentValue = normRange.GetClipped(argument[0]);
63 
64  for (int componentIndex = 0; componentIndex < result.GetElementsCount(); componentIndex++){
65  double startValue = m_startColor[componentIndex];
66  double endValue = m_endColor[componentIndex];
67 
68  double gradientValue = 0.0;
69  if (startValue < endValue){
70  gradientValue = (endValue - startValue) * argumentValue + startValue;
71  }
72  else{
73  gradientValue = (startValue - endValue) * (1.0 - argumentValue) + endValue;
74  }
75 
76  result[componentIndex] = GradientFunction::GetValue(gradientValue);
77  }
78 
79  return true;
80 }
81 
82 
83 template <class GradientFunction>
85 {
86  ResultType result(m_startColor.GetElementsCount());
87 
88  GetValueAt(argument, result);
89 
90  return result;
91 }
92 
93 
95 
96 
97 } // namespace icmm
98 
99 
100 #endif // !icmm_TColorGradient_included
101 
102 
TColorGradient(const GradientColor &startColor, const GradientColor &endColor)
Simple implementation of gradient based on interpolation beetween the start und end colors...
int GetElementsCount() const
Get number of elements.
Definition: CVarVector.h:283
static double GetValue(double x)
Basic interface for a color transformation.
Generic color implementation with variable number of color components.
Definition: CVarColor.h:21
IColorTransformation::ResultType GradientColor
TColorGradient< LinearGradientFunction > CLinearColorGradient
virtual bool GetValueAt(const ArgumentType &argument, ResultType &result) const
Get function value for specified argument value.

© 2007-2017 Witold Gantzke and Kirill Lepskiy