OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
color.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_COLOR_HH
20 #define OST_COLOR_HH
21 
22 /*
23  Author: Ansgar Philippsen, Marco Biasini
24 */
25 
26 #include <iosfwd>
27 
28 #include <boost/operators.hpp>
29 
30 #include <ost/gfx/module_config.hh>
31 #include <ost/geom/geom.hh>
32 
33 namespace ost { namespace gfx {
34 
37  private boost::additive<Color>,
38  private boost::additive<Color, float>,
39  private boost::multiplicative<Color, float>{
40 public:
41  Color() {
42  rgba[0]=1.0;
43  rgba[1]=1.0;
44  rgba[2]=1.0;
45  rgba[3]=1.0;
46  }
47 
48  Color(float r, float g, float b, float a=1.0) {
49  rgba[0]=r;
50  rgba[1]=g;
51  rgba[2]=b;
52  rgba[3]=a;
53  }
54 
55  float& Red() {return rgba[0];}
56  const float& Red() const {return rgba[0];}
57  float& Green() {return rgba[1];}
58  const float& Green() const {return rgba[1];}
59  float& Blue() {return rgba[2];}
60  const float& Blue() const {return rgba[2];}
61  float& Alpha() {return rgba[3];}
62  const float& Alpha() const {return rgba[3];}
63 
64  static Color FromRGB(unsigned char r, unsigned char g,
65  unsigned char b, unsigned char a = 0xff) {
66  static float f=1.0/255.0;
67  return Color(f*static_cast<float>(r),f*static_cast<float>(g),
68  f*static_cast<float>(b),f*static_cast<float>(a));
69  }
70 
71  geom::Vec3 ToHSV();
72 
73  // these also take care of operator[](uint i) !
74  operator float* () {return rgba;}
75  operator const float* () const {return rgba;}
76 
77  Color& operator*=(float rhs);
78  Color& operator+=(float rhs);
79 
80  Color& operator+=(const Color& rhs);
81  Color& operator-=(const Color& rhs);
82  Color& operator-=(float rhs);
83  Color& operator/=(float rhs);
84 private:
85  float rgba[4];
86 };
87 
95 Color DLLEXPORT_OST_GFX HSV(double h, double s, double v);
96 
97 DLLEXPORT_OST_GFX std::ostream& operator<<(std::ostream&, const Color& c);
98 
99 }}
100 
101 #endif
102