Color.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_COLOR_H
8 #define NUKLEI_COLOR_H
9 
10 #include <memory>
11 #include <boost/utility.hpp>
12 #include <nuklei/LinearAlgebra.h>
13 #include <nuklei/Common.h>
14 #include <nuklei/GenericKernel.h>
15 
16 namespace nuklei {
17 
18  class Color : boost::noncopyable
19  {
20  public:
21 
22  typedef NUKLEI_UNIQUE_PTR< Color > ptr;
23 
24  typedef enum { RGB = 0, HSV, HSVCONE, UNKNOWN } Type;
25  static const Type defaultType = RGB;
26  static const std::string TypeNames[];
27 
28  virtual ~Color() {}
29 
30  virtual void assertConsistency() const = 0;
31 
32  virtual NUKLEI_UNIQUE_PTR<Color> clone() const = 0;
33  virtual NUKLEI_UNIQUE_PTR<Color> create() const = 0;
34 
35  virtual appear_t distanceTo(const Color& c) const = 0;
36  virtual appear_t getMaxDist() const = 0;
37 
38  virtual void makeRandom() = 0;
39 
40  virtual GVector getVector() const = 0;
41  virtual void setVector(const GVector &v) = 0;
42 
43  private:
44  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
45  template<class Archive>
46  void serialize(Archive &ar, const unsigned int version)
47  {
48  }
49  };
50 
51  inline Color* new_clone(const Color& d)
52  {
53  return d.clone().release();
54  }
55 
56  class RGBColor : public Color
57  {
58  public:
59  typedef NUKLEI_UNIQUE_PTR< RGBColor > ptr;
60 
61  RGBColor() : c_(0, 0, 0) {assertConsistency();}
62  RGBColor(appear_t r, appear_t g, appear_t b) : c_(r, g, b) {assertConsistency();}
63  RGBColor(const Vector3 &rgb) : c_(rgb) {assertConsistency();}
64  //fixme: this is not good, change this to a method.
65  // I don't remember what that comment is about.. :-/
66  explicit RGBColor(const Color& c);
67 
68  NUKLEI_UNIQUE_PTR<Color> clone() const { return NUKLEI_UNIQUE_PTR<Color>(new RGBColor(c_)); }
69  NUKLEI_UNIQUE_PTR<Color> create() const { return NUKLEI_UNIQUE_PTR<Color>(NUKLEI_MOVE(new RGBColor)); }
70 
71  void assertConsistency() const
72  {
73  NUKLEI_RANGE_CHECK(R(), 0, 1);
74  NUKLEI_RANGE_CHECK(G(), 0, 1);
75  NUKLEI_RANGE_CHECK(B(), 0, 1);
76  }
77 
78  appear_t& R() { return c_.X(); }
79  appear_t R() const { return c_.X(); }
80  appear_t& G() { return c_.Y(); }
81  appear_t G() const { return c_.Y(); }
82  appear_t& B() { return c_.Z(); }
83  appear_t B() const { return c_.Z(); }
84 
85  Vector3 getRGB() const { return c_; }
86  void setRGB(const Vector3& c) { c_ = c; }
87 
88  appear_t& at(unsigned i) { NUKLEI_ASSERT(0 <= i && i < 3); return c_[i]; }
89  appear_t at(unsigned i) const { NUKLEI_ASSERT(0 <= i && i < 3); return c_[i]; }
90 
91  appear_t distanceTo(const Color& c) const
92  {
93  return distanceTo(dynamic_cast<const RGBColor&>(c));
94  }
95  appear_t distanceTo(const RGBColor& rgb) const
96  {
97  return (c_-rgb.c_).Length();
98  }
99  appear_t getMaxDist() const { return MAX_DIST; }
100 
101  void makeRandom();
102 
103  GVector getVector() const
104  {
105  return GVector(3, getRGB());
106  }
107  void setVector(const GVector &v)
108  {
109  c_ = la::vector3Copy(v);
110  }
111 
112  static const appear_t MAX_DIST;
113 
114  private:
115  Vector3 c_;
116  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
117  template<class Archive>
118  void serialize(Archive &ar, const unsigned int version)
119  {
120  ar & NUKLEI_SERIALIZATION_MAKE_NVP
121  ( "base",
122  NUKLEI_SERIALIZATION_BASE(Color) );
123  ar & NUKLEI_SERIALIZATION_NVP(c_);
124  }
125  };
126 
127  class HSVColor : public Color
128  {
129  public:
130  typedef NUKLEI_UNIQUE_PTR< HSVColor > ptr;
131 
132  HSVColor() : c_(0, 0, 0) {assertConsistency();}
133  HSVColor(appear_t h, appear_t s, appear_t v) : c_(h, s, v) {assertConsistency();}
134  HSVColor(const Vector3 &hsv) : c_(hsv) {assertConsistency();}
135  explicit HSVColor(const Color& c);
136 
137  NUKLEI_UNIQUE_PTR<Color> clone() const { return NUKLEI_UNIQUE_PTR<Color>(new HSVColor(c_)); }
138  NUKLEI_UNIQUE_PTR<Color> create() const { return NUKLEI_UNIQUE_PTR<Color>(NUKLEI_MOVE(new HSVColor)); }
139 
140  void assertConsistency() const
141  {
142  NUKLEI_RANGE_CHECK(H(), 0, 2*M_PI);
143  NUKLEI_RANGE_CHECK(S(), 0, 1);
144  NUKLEI_RANGE_CHECK(V(), 0, 1);
145  }
146 
147  appear_t& H() { return c_.X(); }
148  appear_t H() const { return c_.X(); }
149  appear_t& S() { return c_.Y(); }
150  appear_t S() const { return c_.Y(); }
151  appear_t& V() { return c_.Z(); }
152  appear_t V() const { return c_.Z(); }
153 
154  Vector3 getHSV() const { return c_; }
155  void setHSV(const Vector3& c) { c_ = c; }
156 
157  appear_t& at(unsigned i) { NUKLEI_ASSERT(0 <= i && i < 3); return c_[i]; }
158  appear_t at(unsigned i) const { NUKLEI_ASSERT(0 <= i && i < 3); return c_[i]; }
159 
160  appear_t distanceTo(const Color& c) const;
161  appear_t distanceTo(const HSVColor& hsv) const;
162  appear_t getMaxDist() const { return MAX_DIST; }
163 
164  void makeRandom();
165 
166  GVector getVector() const
167  {
168  return GVector(3, getHSV());
169  }
170  void setVector(const GVector &v)
171  {
172  c_ = la::vector3Copy(v);
173  }
174 
175  static const appear_t MAX_DIST;
176 
177  private:
178  Vector3 c_;
179  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
180  template<class Archive>
181  void serialize(Archive &ar, const unsigned int version)
182  {
183  ar & NUKLEI_SERIALIZATION_MAKE_NVP
184  ( "base",
185  NUKLEI_SERIALIZATION_BASE(Color) );
186  ar & NUKLEI_SERIALIZATION_NVP(c_);
187  }
188  };
189 
190  class HSVConeColor : public Color
191  {
192  public:
193  typedef NUKLEI_UNIQUE_PTR< HSVConeColor > ptr;
194 
195  HSVConeColor() : c_(0, 0, 0), valueWeight_(1) {assertConsistency();}
196  HSVConeColor(appear_t sch, appear_t ssh, appear_t weightedValue, appear_t valueWeight = 1) :
197  c_(sch, ssh, weightedValue), valueWeight_(valueWeight) {assertConsistency();}
198  HSVConeColor(const Vector3 &c, appear_t valueWeight = 1) : c_(c), valueWeight_(valueWeight) {assertConsistency();}
199  explicit HSVConeColor(const Color& c);
200 
201  NUKLEI_UNIQUE_PTR<Color> clone() const { return NUKLEI_UNIQUE_PTR<Color>(new HSVConeColor(c_)); }
202  NUKLEI_UNIQUE_PTR<Color> create() const { return NUKLEI_UNIQUE_PTR<Color>(NUKLEI_MOVE(new HSVConeColor)); }
203 
204  void assertConsistency() const
205  {
206  NUKLEI_RANGE_CHECK(SCosH(), -1, 1);
207  NUKLEI_RANGE_CHECK(SSinH(), -1, 1);
208  NUKLEI_RANGE_CHECK(WV(), 0, 1);
209  }
210 
211  appear_t& SCosH() { return c_.X(); }
212  appear_t SCosH() const { return c_.X(); }
213  appear_t& SSinH() { return c_.Y(); }
214  appear_t SSinH() const { return c_.Y(); }
215  appear_t& WV() { return c_.Z(); }
216  appear_t WV() const { return c_.Z(); }
217  appear_t& W() { return valueWeight_; }
218  appear_t W() const { return valueWeight_; }
219 
220  Vector3 getHSVCone() const { return c_; }
221  void setHSVCone(const Vector3& c) { c_ = c; }
222  appear_t getValueWeight() const { return valueWeight_; }
223  void setValueWeight(const appear_t& valueWeight) { valueWeight_ = valueWeight; }
224 
225 
226  appear_t& at(unsigned i) { NUKLEI_ASSERT(0 <= i && i < 3); return c_[i]; }
227  appear_t at(unsigned i) const { NUKLEI_ASSERT(0 <= i && i < 3); return c_[i]; }
228 
229  appear_t distanceTo(const Color& c) const;
230  appear_t distanceTo(const HSVConeColor& hsvc) const;
231  appear_t getMaxDist() const { return MAX_DIST; }
232 
233  void makeRandom();
234 
235  GVector getVector() const
236  {
237  GVector gv(4);
238  for (int i = 0; i < 3; ++i) gv[i] = c_[i];
239  gv[3] = valueWeight_;
240  return gv;
241  }
242  void setVector(const GVector &v)
243  {
244  NUKLEI_ASSERT(v.GetSize() == 4);
245  c_ = Vector3(v[0], v[1], v[2]);
246  valueWeight_ = v[3];
247  }
248 
249  static const appear_t MAX_DIST;
250 
251  private:
252  Vector3 c_;
253  appear_t valueWeight_;
254  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
255  template<class Archive>
256  void serialize(Archive &ar, const unsigned int version)
257  {
258  ar & NUKLEI_SERIALIZATION_MAKE_NVP
259  ( "base",
260  NUKLEI_SERIALIZATION_BASE(Color) );
261  ar & NUKLEI_SERIALIZATION_NVP(c_) & NUKLEI_SERIALIZATION_NVP(valueWeight_);
262  }
263  };
264 
265 
266 }
267 
268 #endif
Definition: Color.h:56
Public namespace.
Definition: Color.cpp:9
Definition: Color.h:127
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
double appear_t
Type for appearance-related values (e.g., color)
Definition: Definitions.h:29
Definition: Color.h:190
Definition: Color.h:18
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.