OpenStructure
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-2020 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 
53  private boost::additive<Color>,
54  private boost::additive<Color, float>,
55  private boost::multiplicative<Color, float>{
56 
57 public:
59  Color();
60 
62  void SetRGB(float r, float g, float b);
64  geom::Vec3 GetRGB() const;
68  float GetRed() const;
70  void SetRed(float);
72  float GetGreen() const;
74  void SetGreen(float);
76  float GetBlue() const;
78  void SetBlue(float);
80  void SetHSV(float h, float s, float v);
82  geom::Vec3 GetHSV() const;
86  float GetHue() const;
88  void SetHue(float);
90  float GetSat() const;
92  void SetSat(float);
94  float GetVal() const;
96  void SetVal(float);
98  float GetAlpha() const;
100  void SetAlpha(float);
101 
102 
114  operator const float* () const;
115  operator float* ();
116 
117  Color& operator*=(float rhs);
118  Color& operator+=(float rhs);
119 
120  Color& operator+=(const Color& rhs);
121  Color& operator-=(const Color& rhs);
122  Color& operator-=(float rhs);
123  Color& operator/=(float rhs);
124 
126 
128  geom::Vec3 ToHSV() const {return GetHSV();}
129 
131  Color(float r, float g, float b, float a=1.0);
132 
134  float Red() const {return GetRed();}
136  float Green() const {return GetGreen();}
138  float Blue() const {return GetBlue();}
140  float Alpha() const {return GetAlpha();}
141 
143  static Color FromRGB(unsigned char r, unsigned char g,
144  unsigned char b, unsigned char a = 0xff) {
145  static float f=1.0/255.0;
146  return Color(f*static_cast<float>(r),f*static_cast<float>(g),
147  f*static_cast<float>(b),f*static_cast<float>(a));
148  }
149 
151 
152 private:
153  void to_hsv() const;
154  void to_rgb() const;
155 
156  mutable float rgba_[4];
157  mutable float hsv_[3];
158  mutable bool rgb_dirty_;
159  mutable bool hsv_dirty_;
160 };
161 
162 #undef RGB
164 Color DLLEXPORT_OST_GFX RGB(float r, float g, float b);
165 
168 
170 Color DLLEXPORT_OST_GFX RGBi(unsigned int r, unsigned int g, unsigned int b);
171 
173 Color DLLEXPORT_OST_GFX RGBA(float r, float g, float b, float a);
174 
177 
179 Color DLLEXPORT_OST_GFX RGBAi(unsigned int r, unsigned int g, unsigned int b, unsigned int a);
180 
181 
202 Color DLLEXPORT_OST_GFX HSV(float h, float s, float v);
203 
217  Color DLLEXPORT_OST_GFX HSVi(int h, int s, int v);
218 
224 Color DLLEXPORT_OST_GFX HSVA(float h, float s, float v, float a);
225 
231 Color DLLEXPORT_OST_GFX HSVAi(int h, int s, int v, int a);
232 
233 
235 DLLEXPORT_OST_GFX std::ostream& operator<<(std::ostream&, const Color& c);
236 
237 }}
238 
239 #endif
240 
Three dimensional vector class, using Real precision.
Definition: vec3.hh:48
float Blue() const
DEPRECATED.
Definition: color.hh:138
Color & operator/=(float rhs)
void SetGreen(float)
set green
void SetVal(float)
set value
Color & operator*=(float rhs)
Color()
initialize to white
float GetRed() const
retrieve red
void SetBlue(float)
set blue
static Color FromRGB(unsigned char r, unsigned char g, unsigned char b, unsigned char a=0xff)
DEPRECATED.
Definition: color.hh:143
float Green() const
DEPRECATED.
Definition: color.hh:136
Color(float r, float g, float b, float a=1.0)
DEPRECATED.
float Red() const
DEPRECATED.
Definition: color.hh:134
Color & operator+=(float rhs)
geom::Vec4 GetHSVA() const
convenience to get HSVA as Vec4
void SetHSV(float h, float s, float v)
convenience to set HSV triplet (ranges 0-1)
float GetVal() const
retrieve value
geom::Vec3 GetHSV() const
convenience to get HSV triplet as Vec3
float GetSat() const
retrieve saturatuin
void SetHue(float)
set hue
void SetSat(float)
set saturation
float GetAlpha() const
retrieve alpha
geom::Vec3 ToHSV() const
DEPRECATED.
Definition: color.hh:128
geom::Vec3 GetRGB() const
convenience to get RGB triplet as Vec3
float GetHue() const
retrieve hue
void SetRed(float)
set red
void SetRGB(float r, float g, float b)
convenience to set RGB triplet (ranges 0-1)
float GetBlue() const
retrieve blue
float Alpha() const
DEPRECATED.
Definition: color.hh:140
Color & operator-=(float rhs)
geom::Vec4 GetRGBA() const
convenience to get RGBA as Vec4
Color & operator+=(const Color &rhs)
Color & operator-=(const Color &rhs)
float GetGreen() const
retrieve green
void SetAlpha(float)
set alpha
#define DLLEXPORT_OST_GFX
unsigned char uchar
Definition: base.hh:33
Color DLLEXPORT_OST_GFX HSVAi(int h, int s, int v, int a)
HSVA color spec from integers.
Color DLLEXPORT_OST_GFX RGBi(unsigned int r, unsigned int g, unsigned int b)
RGB color spec from integers (0-65535)
Color DLLEXPORT_OST_GFX RGB(float r, float g, float b)
RGB color spec from floats (0.0-1.0)
Color DLLEXPORT_OST_GFX RGBA(float r, float g, float b, float a)
RGBA color spec from floats (0.0-1.0)
DLLEXPORT_OST_GFX std::ostream & operator<<(std::ostream &, const Color &c)
string form
Color DLLEXPORT_OST_GFX HSVi(int h, int s, int v)
HSV color spec from integers.
Color DLLEXPORT_OST_GFX HSV(float h, float s, float v)
HSV color spec from floats.
Color DLLEXPORT_OST_GFX RGBAb(uchar r, uchar g, uchar b, uchar a)
RGBA color spec from bytes (0-255)
Color DLLEXPORT_OST_GFX RGBAi(unsigned int r, unsigned int g, unsigned int b, unsigned int a)
RGBA color spec from integers (0-65535)
Color DLLEXPORT_OST_GFX HSVA(float h, float s, float v, float a)
HSVA color spec from floats.
Color DLLEXPORT_OST_GFX RGBb(uchar r, uchar g, uchar b)
RGB color spec from bytes (0-255)
Definition: base.dox:1