Developer Documentation
IColorCoder.hh
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 #ifndef ACG_ICOLORCODER_HH
43 #define ACG_ICOLORCODER_HH
44 
45 #include <ACG/Math/VectorT.hh>
46 #include <ACG/Config/ACGDefines.hh>
47 #include <QColor>
48 #include <algorithm>
49 
50 namespace {
51 // With C++17, we can use std::clamp() instead
52 template<class T>
53 const T clamp( const T v, const T lo, const T hi)
54 {
55  if (v < lo)
56  return lo;
57  if (v > hi)
58  return hi;
59  return v;
60 }
61 }
62 namespace ACG {
63 
64 class ACGDLLEXPORT IColorCoder {
65 public:
66  virtual ~IColorCoder() = default;
67 
68  // "raw" refers to directly using the value,
69  // assuming it to be inside [min(),max()] without any checks,
70  // those are implemented in color4() and color_float4()
71 
72  virtual ACG::Vec4uc color4_raw(float _v) const = 0;
73  virtual ACG::Vec4f color_float4_raw(float _v) const = 0;
74 
76  virtual float min() const = 0;
77 
79  virtual float max() const = 0;
80 
81  virtual ACG::Vec4uc color4(float _v) const
82  {
83  auto clamped = clamp(_v, min(), max());
84  auto col = color4_raw(clamped);
85  if (mapOutsideRangeToAlpha0_ && clamped != _v) {
86  col[3] = 0;
87  }
88  return col;
89  }
90 
91  virtual ACG::Vec4f color_float4(float _v) const {
92  auto clamped = clamp(_v, min(), max());
93  auto col = color_float4_raw(clamped);
94  if (mapOutsideRangeToAlpha0_ && clamped != _v) {
95  col[3] = 0.0f;
96  }
97  return col;
98  }
99 
100  inline ACG::Vec3uc color(float _v) const {
101  ACG::Vec4uc c = color4(_v);
102  return ACG::Vec3uc(c[0], c[1], c[2]);
103  }
104 
105  inline ACG::Vec3f color_float(float _v) const
106  {
107  ACG::Vec4f c = color_float4(_v);
108  return ACG::Vec3f(c[0], c[1], c[2]);
109  }
110 
111  inline QColor color_qcolor(float _v) const
112  {
113  ACG::Vec4uc c = color4(_v);
114  return QColor(c[0], c[1], c[2], c[3]);
115  }
116 
117  // Make the color coder usable as a function operator.
118  inline ACG::Vec4f operator() (float _v) const {
119  return color_float4(_v);
120  }
121 
122  void setMapOutsideRangeToAlpha0(bool value)
123  {
124  mapOutsideRangeToAlpha0_ = value;
125  }
126 
127  bool getMapOutsideRangeToAlpha0() const
128  {
129  return mapOutsideRangeToAlpha0_;
130  }
131 
132 protected:
133  bool mapOutsideRangeToAlpha0_ = false;
134 };
135 
136 
137 
138 
139 
140 } // namespace ACG
141 
142 #endif // ICOLORCODER_HH
Namespace providing different geometric functions concerning angles.
VectorT< unsigned char, 3 > Vec3uc
Definition: VectorT.hh:109
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:119