ColorCoder.hh 5.17 KB
Newer Older
Henrik Zimmer's avatar
Henrik Zimmer committed
1
2
/*===========================================================================*\
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
3
 *                          Plugin-HarmonicExample                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
4
5
6
7
 *      Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.rwth-graphics.de                            *
 *                                                                           *
 *---------------------------------------------------------------------------* 
Henrik Zimmer's avatar
Henrik Zimmer committed
8
 *  This file is part of Plugin-HarmonicExample                              *
Henrik Zimmer's avatar
Henrik Zimmer committed
9
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
10
11
12
13
 *  Plugin-HarmonicExample is free software: you can redistribute it and/or  *
 *  modify it under the terms of the GNU General Public License as published *
 *  by the Free Software Foundation, either version 3 of the License, or     *
 *  (at your option) any later version.                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
14
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
15
 *  Plugin-HarmonicExample is distributed in the hope that it will be useful,*
Henrik Zimmer's avatar
Henrik Zimmer committed
16
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
Henrik Zimmer's avatar
Henrik Zimmer committed
18
 *  GNU General Public License for more details.                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
19
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
20
21
22
 *  You should have received a copy of the GNU General Public License        *
 *  along with Plugin-HarmonicExample.                                       *
 *  If not, see <http://www.gnu.org/licenses/>.                              *
Henrik Zimmer's avatar
Henrik Zimmer committed
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
 *                                                                           *
\*===========================================================================*/ 

//=============================================================================
//
//  CLASS ColorCoder
//
//=============================================================================


#ifndef ACG_COLORCODER_HH
#define ACG_COLORCODER_HH

#ifdef WIN32
#undef min
#undef max
#endif


//== INCLUDES =================================================================


#include <ACG/Math/VectorT.hh>


//== NAMESPACES ===============================================================

namespace ACG {

//== CLASS DEFINITION =========================================================


class ColorCoder
{
public:
   
  /// Default constructor.
  ColorCoder(float _min=0.0, float _max=1.0, bool _signed=false) 
  { set_range(_min, _max, _signed); }
 


  /// set the color coding range for unsigned coding
  void set_range(float _min, float _max, bool _signed) 
  {
    if (_min == _max)
    {
      val0_ = val1_ = val2_ = val3_ = val4_ = _min;
    }
    else
    {
      if (_min > _max) std::swap(_min, _max);
      val0_ = _min;
      val4_ = _max;
      val2_ = 0.5f * (val0_ + val4_);
      val1_ = 0.5f * (val0_ + val2_);
      val3_ = 0.5f * (val2_ + val4_);
    }
    signed_mode_ = _signed;
  }


  /// color coding
  ACG::Vec3uc color(float _v) const {
    return signed_mode_ ? color_signed(_v) : color_unsigned(_v);
  }

  /// color coding
  ACG::Vec3f color_float(float _v) const {
    ACG::Vec3uc c;
    if(signed_mode_) c=color_signed(_v); else c=color_unsigned(_v);
    return (ACG::Vec3f(c[0],c[1],c[2])/255.f);
  }


  /// min scalar value
  float min() const { return val0_; }
  /// max scalar value
  float max() const { return val4_; }


private:


  ACG::Vec3uc color_unsigned(float _v) const 
  {
    if (val4_ <= val0_) return ACG::Vec3uc(0, 0, 255);

    unsigned char u;

    if (_v < val0_) return ACG::Vec3uc(0, 0, 255);
    if (_v > val4_) return ACG::Vec3uc(255, 0, 0);


    if (_v <= val2_) 
    {
      // [v0, v1]
      if (_v <= val1_) 
      {
	u = (unsigned char) (255.0 * (_v - val0_) / (val1_ - val0_));
	return ACG::Vec3uc(0, u, 255);
      }
      // ]v1, v2]
      else 
      {
	u = (unsigned char) (255.0 * (_v - val1_) / (val2_ - val1_));
	return ACG::Vec3uc(0, 255, 255-u);
      }
    }
    else 
    {
      // ]v2, v3]
      if (_v <= val3_) 
      {
	u = (unsigned char) (255.0 * (_v - val2_) / (val3_ - val2_));
	return ACG::Vec3uc(u, 255, 0);
      }
      // ]v3, v4]
      else 
      {
	u = (unsigned char) (255.0 * (_v - val3_) / (val4_ - val3_));
	return ACG::Vec3uc(255, 255-u, 0);
      }
    }
  }


  ACG::Vec3uc color_signed(float _v) const 
  {
    if (val4_ <= val0_) return ACG::Vec3uc(0,255,0);

    unsigned char r,g,b;

    if      (_v < val0_) _v = val0_;
    else if (_v > val4_) _v = val4_;

    if (_v < 0.0) 
    {
      r = val0_ ? (unsigned char)(255.0 * _v / val0_) : 0;
      b = 0;
    }
    else 
    {
      r = 0;
      b = val4_ ? (unsigned char)(255.0 * _v / val4_) : 0;
    }
    g = 255 - r - b;

    return ACG::Vec3uc(r, g, b);
  }


  
  
  float  val0_, val1_, val2_, val3_, val4_;
  bool   signed_mode_;
};


//=============================================================================
} // namespace ACG
//=============================================================================
#endif // ACG_COLORCODER_HH defined
//=============================================================================