Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ColorCoder.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48  \*===========================================================================*/
49 
50 //=============================================================================
51 //
52 // CLASS ColorCoder
53 //
54 //=============================================================================
55 
56 //== INCLUDES =================================================================
57 
58 #include <ACG/Utils/ColorCoder.hh>
59 
60 //== NAMESPACES ===============================================================
61 
62 namespace ACG {
63 
64 //== CLASS DEFINITION =========================================================
65 
66 
68 ColorCoder::ColorCoder(float _min, float _max, bool _signed)
69 {
70  set_range(_min, _max, _signed);
71 }
72 
74 void ColorCoder::set_range(float _min, float _max, bool _signed)
75 {
76  if (_min == _max) {
77  val0_ = val1_ = val2_ = val3_ = val4_ = _min;
78  } else {
79  if (_min > _max)
80  std::swap(_min, _max);
81  val0_ = _min;
82  val4_ = _max;
83  val2_ = 0.5 * (val0_ + val4_);
84  val1_ = 0.5 * (val0_ + val2_);
85  val3_ = 0.5 * (val2_ + val4_);
86  }
87  signed_mode_ = _signed;
88 }
89 
92 {
93  return signed_mode_ ? color_signed(_v) : color_unsigned(_v);
94 }
95 
98 {
99  ACG::Vec4uc c = color4(_v);
100  return (ACG::Vec4f(c[0], c[1], c[2], c[3]) / 255.f);
101 }
102 
104 float ColorCoder::min() const
105 {
106  return val0_;
107 }
109 float ColorCoder::max() const
110 {
111  return val4_;
112 }
113 
114 ACG::Vec4uc ColorCoder::color_unsigned(float _v) const
115 {
116  if (val4_ <= val0_)
117  return ACG::Vec4uc(0, 0, 255, 255);
118 
119  unsigned char u;
120 
121  if (_v < val0_)
122  return ACG::Vec4uc(0, 0, 255, 255);
123  if (_v > val4_)
124  return ACG::Vec4uc(255, 0, 0, 255);
125 
126  if (_v <= val2_) {
127  // [v0, v1]
128  if (_v <= val1_) {
129  u = (unsigned char) (255.0 * (_v - val0_) / (val1_ - val0_));
130  return ACG::Vec4uc(0, u, 255, 255);
131  }
132  // ]v1, v2]
133  else {
134  u = (unsigned char) (255.0 * (_v - val1_) / (val2_ - val1_));
135  return ACG::Vec4uc(0, 255, 255 - u, 255);
136  }
137  } else {
138  // ]v2, v3]
139  if (_v <= val3_) {
140  u = (unsigned char) (255.0 * (_v - val2_) / (val3_ - val2_));
141  return ACG::Vec4uc(u, 255, 0, 255);
142  }
143  // ]v3, v4]
144  else {
145  u = (unsigned char) (255.0 * (_v - val3_) / (val4_ - val3_));
146  return ACG::Vec4uc(255, 255 - u, 0, 255);
147  }
148  }
149 }
150 
151 ACG::Vec4uc ColorCoder::color_signed(float _v) const
152 {
153  if (val4_ <= val0_)
154  return ACG::Vec4uc(0, 255, 0, 255);
155 
156  unsigned char r, g, b;
157 
158  if (_v < val0_)
159  _v = val0_;
160  else if (_v > val4_)
161  _v = val4_;
162 
163  if (_v < 0.0) {
164  r = val0_ ? (unsigned char) (255.0 * _v / val0_) : 0;
165  b = 0;
166  } else {
167  r = 0;
168  b = val4_ ? (unsigned char) (255.0 * _v / val4_) : 0;
169  }
170  g = 255 - r - b;
171 
172  return ACG::Vec4uc(r, g, b, 255);
173 }
174 
175 
176 //=============================================================================
177 }// namespace ACG
178 //=============================================================================
179 //=============================================================================
180 
ACG::Vec4uc color4(float _v) const override
color coding
Definition: ColorCoder.cc:91
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
float max() const override
max scalar value
Definition: ColorCoder.cc:109
ColorCoder(float _min=0.0, float _max=1.0, bool _signed=false)
Default constructor.
Definition: ColorCoder.cc:68
void set_range(float _min, float _max, bool _signed)
set the color coding range for unsigned coding
Definition: ColorCoder.cc:74
VectorT< unsigned char, 4 > Vec4uc
Definition: VectorT.hh:134
ACG::Vec4f color_float4(float _v) const override
color coding
Definition: ColorCoder.cc:97
float min() const override
min scalar value
Definition: ColorCoder.cc:104