Developer Documentation
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;
100  if (signed_mode_)
101  c = color_signed(_v);
102  else
103  c = color_unsigned(_v);
104  return (ACG::Vec3uc(c[0], c[1], c[2]) / 255.f);
105 }
106 
109 {
110  ACG::Vec4uc c;
111  if (signed_mode_)
112  c = color_signed(_v);
113  else
114  c = color_unsigned(_v);
115  return (ACG::Vec3f(c[0], c[1], c[2]) / 255.f);
116 }
117 
120 {
121  ACG::Vec4uc c;
122  if (signed_mode_)
123  c = color_signed(_v);
124  else
125  c = color_unsigned(_v);
126  return (ACG::Vec4f(c[0], c[1], c[2], c[3]) / 255.f);
127 }
128 
130 QColor ColorCoder::color_qcolor(float _v) const
131 {
132  ACG::Vec4uc c;
133  if (signed_mode_)
134  c = color_signed(_v);
135  else
136  c = color_unsigned(_v);
137 
138  return(QColor(c[0], c[1], c[2], c[3]));
139 }
140 
142 float ColorCoder::min() const
143 {
144  return val0_;
145 }
147 float ColorCoder::max() const
148 {
149  return val4_;
150 }
151 
152 ACG::Vec4uc ColorCoder::color_unsigned(float _v) const
153 {
154  if (val4_ <= val0_)
155  return ACG::Vec4uc(0, 0, 255, 255);
156 
157  unsigned char u;
158 
159  if (_v < val0_)
160  return ACG::Vec4uc(0, 0, 255, 255);
161  if (_v > val4_)
162  return ACG::Vec4uc(255, 0, 0, 255);
163 
164  if (_v <= val2_) {
165  // [v0, v1]
166  if (_v <= val1_) {
167  u = (unsigned char) (255.0 * (_v - val0_) / (val1_ - val0_));
168  return ACG::Vec4uc(0, u, 255, 255);
169  }
170  // ]v1, v2]
171  else {
172  u = (unsigned char) (255.0 * (_v - val1_) / (val2_ - val1_));
173  return ACG::Vec4uc(0, 255, 255 - u, 255);
174  }
175  } else {
176  // ]v2, v3]
177  if (_v <= val3_) {
178  u = (unsigned char) (255.0 * (_v - val2_) / (val3_ - val2_));
179  return ACG::Vec4uc(u, 255, 0, 255);
180  }
181  // ]v3, v4]
182  else {
183  u = (unsigned char) (255.0 * (_v - val3_) / (val4_ - val3_));
184  return ACG::Vec4uc(255, 255 - u, 0, 255);
185  }
186  }
187 }
188 
189 ACG::Vec4uc ColorCoder::color_signed(float _v) const
190 {
191  if (val4_ <= val0_)
192  return ACG::Vec4uc(0, 255, 0, 255);
193 
194  unsigned char r, g, b;
195 
196  if (_v < val0_)
197  _v = val0_;
198  else if (_v > val4_)
199  _v = val4_;
200 
201  if (_v < 0.0) {
202  r = val0_ ? (unsigned char) (255.0 * _v / val0_) : 0;
203  b = 0;
204  } else {
205  r = 0;
206  b = val4_ ? (unsigned char) (255.0 * _v / val4_) : 0;
207  }
208  g = 255 - r - b;
209 
210  return ACG::Vec4uc(r, g, b, 255);
211 }
212 
213 
214 //=============================================================================
215 }// namespace ACG
216 //=============================================================================
217 //=============================================================================
218 
ACG::Vec3uc color(float _v) const
color coding
Definition: ColorCoder.cc:97
float max() const
max scalar value
Definition: ColorCoder.cc:147
ACG::Vec4f color_float4(float _v) const
color coding
Definition: ColorCoder.cc:119
ColorCoder(float _min=0.0, float _max=1.0, bool _signed=false)
Default constructor.
Definition: ColorCoder.cc:68
ACG::Vec3f color_float(float _v) const
color coding
Definition: ColorCoder.cc:108
VectorT< unsigned char, 4 > Vec4uc
Definition: VectorT.hh:134
ACG::Vec4uc color4(float _v) const
color coding
Definition: ColorCoder.cc:91
void set_range(float _min, float _max, bool _signed)
set the color coding range for unsigned coding
Definition: ColorCoder.cc:74
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
QColor color_qcolor(float _v) const
color coding
Definition: ColorCoder.cc:130
float min() const
min scalar value
Definition: ColorCoder.cc:142