MEPP2 Project
GradientColorMap.inl
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #include <cstdlib>
12 #include <vector>
13 
14 template< typename PValue >
16  const Value &_minV,
17  const Value &_maxV,
18  const Color &_firstColor,
19  const Color &_lastColor)
20  : myMin(_minV), myMax(_maxV)
21 {
22  // ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a
23  // colormap.");
24  if(_firstColor != Color::None() && _lastColor != Color::None())
25  {
26  myColors.push_back(_firstColor);
27  myColors.push_back(_lastColor);
28  }
29 }
30 
31 template< typename PValue >
33  const GradientColorMap< Value > &_other)
34  : myMin(_other.myMin), myMax(_other.myMax), myColors(_other.myColors)
35 {
36  // ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a
37  // colormap.");
38 }
39 
40 template< typename PValue >
44 {
45  if(&_other != this)
46  {
47  myMin = _other.myMin;
48  myMax = _other.myMax;
49  myColors = _other.myColors;
50  // ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a
51  // colormap.");
52  }
53  return *this;
54 }
55 
56 template< typename PValue >
57 inline FEVV::Color
59 {
60  return GradientColorMap< Value >::getColor(myColors, myMin, myMax, _value);
61 }
62 
63 template< typename PValue >
64 inline void
66 {
67  myColors.push_back(_color);
68 }
69 
70 template< typename PValue >
71 inline void
73 {
74  myColors.clear();
75 }
76 
77 template< typename PValue >
78 inline const PValue &
80 {
81  return myMin;
82 }
83 
84 template< typename PValue >
85 inline const PValue &
87 {
88  return myMax;
89 }
90 
91 template< typename PValue >
92 inline FEVV::Color
93 FEVV::GradientColorMap< PValue >::getColor(const std::vector< Color > &_colors,
94  const Value &_min,
95  const Value &_max,
96  const Value &_value)
97 {
98  // ASSERT_MSG(min < max, "Max should be strictly greather than Min in a
99  // colormap.");
100  if(_colors.size() < 2)
101  {
102  return Color::None();
103  }
104 
105  double scale = static_cast< double >(_value - _min) / (_max - _min);
106  const int intervals = (const int)_colors.size() - 1;
107  int upper_index = static_cast< int >(ceil(intervals * scale));
108  if(!upper_index) // Special case when value == min.
109  {
110  upper_index = 1;
111  }
112  const Color &firstColor = _colors[upper_index - 1];
113  const Color &lastColor = _colors[upper_index];
114  scale = (scale * intervals) - (upper_index - 1);
115 
116  const unsigned char red = static_cast< unsigned char >(
117  firstColor.red() + scale * (lastColor.red() - firstColor.red()));
118  const unsigned char green = static_cast< unsigned char >(
119  firstColor.green() + scale * (lastColor.green() - firstColor.green()));
120  const unsigned char blue = static_cast< unsigned char >(
121  firstColor.blue() + scale * (lastColor.blue() - firstColor.blue()));
122  return Color(red, green, blue);
123 }
FEVV::Color::blue
void blue(const unsigned char _blueValue)
Definition: Color.inl:94
FEVV::GradientColorMap::maxValue
const Value & maxValue() const
Definition: GradientColorMap.inl:86
FEVV::GradientColorMap::operator=
GradientColorMap & operator=(const GradientColorMap &_other)
Definition: GradientColorMap.inl:43
FEVV::GradientColorMap::addColor
void addColor(const Color &_color)
Definition: GradientColorMap.inl:65
FEVV::Color::green
void green(const unsigned char _greenValue)
Definition: Color.inl:87
FEVV::GradientColorMap::myColors
std::vector< Color > myColors
Definition: GradientColorMap.h:161
FEVV::GradientColorMap::Value
PValue Value
Definition: GradientColorMap.h:63
FEVV::GradientColorMap::getColor
static Color getColor(const std::vector< Color > &_colors, const Value &_min, const Value &_max, const Value &_value)
Definition: GradientColorMap.inl:93
FEVV::GradientColorMap::clear
void clear()
Definition: GradientColorMap.inl:72
FEVV::Color::None
static Color None(void)
Definition: Color.inl:317
FEVV::Color
Definition: Color.hpp:18
FEVV::GradientColorMap
This templated class may be used to (linearly) convert scalar values in a given range into a color in...
Definition: GradientColorMap.h:60
FEVV::GradientColorMap::myMax
Value myMax
Definition: GradientColorMap.h:160
FEVV::GradientColorMap::minValue
const Value & minValue() const
Definition: GradientColorMap.inl:79
FEVV::GradientColorMap::operator()
Color operator()(const Value &_value) const
Definition: GradientColorMap.inl:58
FEVV::GradientColorMap::GradientColorMap
GradientColorMap()=delete
FEVV::Color::red
void red(const unsigned char _redValue)
Definition: Color.inl:80
FEVV::GradientColorMap::myMin
Value myMin
Definition: GradientColorMap.h:159