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 //
45 // CLASS ColorCoder
46 //
47 //=============================================================================
48 
49 //== INCLUDES =================================================================
50 
51 #include <ACG/Utils/ColorCoder.hh>
52 
53 //== NAMESPACES ===============================================================
54 
55 namespace ACG {
56 
57 //== CLASS DEFINITION =========================================================
58 
59 
61 ColorCoder::ColorCoder(float _min, float _max, bool _signed)
62 {
63  set_range(_min, _max, _signed);
64 }
65 
67 void ColorCoder::set_range(float _min, float _max, bool _signed)
68 {
69  if (_min == _max) {
70  val0_ = val1_ = val2_ = val3_ = val4_ = _min;
71  } else {
72  if (_min > _max)
73  std::swap(_min, _max);
74  val0_ = _min;
75  val4_ = _max;
76  val2_ = 0.5 * (val0_ + val4_);
77  val1_ = 0.5 * (val0_ + val2_);
78  val3_ = 0.5 * (val2_ + val4_);
79  }
80  signed_mode_ = _signed;
81 }
82 
85 {
86  return signed_mode_ ? color_signed(_v) : color_unsigned(_v);
87 }
88 
91 {
92  ACG::Vec4uc c = color4_raw(_v);
93  return (ACG::Vec4f(c[0], c[1], c[2], c[3]) / 255.f);
94 }
95 
97 float ColorCoder::min() const
98 {
99  return val0_;
100 }
102 float ColorCoder::max() const
103 {
104  return val4_;
105 }
106 
107 ACG::Vec4uc ColorCoder::color_unsigned(float _v) const
108 {
109  if (val4_ <= val0_)
110  return ACG::Vec4uc(0, 0, 255, 255);
111 
112  unsigned char u;
113 
114  if (_v < val0_)
115  return ACG::Vec4uc(0, 0, 255, 255);
116  if (_v > val4_)
117  return ACG::Vec4uc(255, 0, 0, 255);
118 
119  if (_v <= val2_) {
120  // [v0, v1]
121  if (_v <= val1_) {
122  u = (unsigned char) (255.0 * (_v - val0_) / (val1_ - val0_));
123  return ACG::Vec4uc(0, u, 255, 255);
124  }
125  // ]v1, v2]
126  else {
127  u = (unsigned char) (255.0 * (_v - val1_) / (val2_ - val1_));
128  return ACG::Vec4uc(0, 255, 255 - u, 255);
129  }
130  } else {
131  // ]v2, v3]
132  if (_v <= val3_) {
133  u = (unsigned char) (255.0 * (_v - val2_) / (val3_ - val2_));
134  return ACG::Vec4uc(u, 255, 0, 255);
135  }
136  // ]v3, v4]
137  else {
138  u = (unsigned char) (255.0 * (_v - val3_) / (val4_ - val3_));
139  return ACG::Vec4uc(255, 255 - u, 0, 255);
140  }
141  }
142 }
143 
144 ACG::Vec4uc ColorCoder::color_signed(float _v) const
145 {
146  if (val4_ <= val0_)
147  return ACG::Vec4uc(0, 255, 0, 255);
148 
149  unsigned char r, g, b;
150 
151  if (_v < val0_)
152  _v = val0_;
153  else if (_v > val4_)
154  _v = val4_;
155 
156  if (_v < 0.0) {
157  r = val0_ ? (unsigned char) (255.0 * _v / val0_) : 0;
158  b = 0;
159  } else {
160  r = 0;
161  b = val4_ ? (unsigned char) (255.0 * _v / val4_) : 0;
162  }
163  g = 255 - r - b;
164 
165  return ACG::Vec4uc(r, g, b, 255);
166 }
167 
168 
169 //=============================================================================
170 }// namespace ACG
171 //=============================================================================
172 //=============================================================================
173 
Namespace providing different geometric functions concerning angles.
float max() const override
max scalar value
Definition: ColorCoder.cc:102
VectorT< unsigned char, 4 > Vec4uc
Definition: VectorT.hh:128
ACG::Vec4uc color4_raw(float _v) const override
color coding
Definition: ColorCoder.cc:84
ColorCoder(float _min=0.0, float _max=1.0, bool _signed=false)
Default constructor.
Definition: ColorCoder.cc:61
void set_range(float _min, float _max, bool _signed)
set the color coding range for unsigned coding
Definition: ColorCoder.cc:67
float min() const override
min scalar value
Definition: ColorCoder.cc:97
ACG::Vec4f color_float4_raw(float _v) const override
color coding
Definition: ColorCoder.cc:90