Enki  1.9
Types.h
Go to the documentation of this file.
1 /*
2  Enki - a fast 2D robot simulator
3  Copyright (C) 1999-2016 Stephane Magnenat <stephane at magnenat dot net>
4  Copyright (C) 2004-2005 Markus Waibel <markus dot waibel at epfl dot ch>
5  Copyright (c) 2004-2005 Antoine Beyeler <abeyeler at ab-ware dot com>
6  Copyright (C) 2005-2006 Laboratory of Intelligent Systems, EPFL, Lausanne
7  Copyright (C) 2006-2008 Laboratory of Robotics Systems, EPFL, Lausanne
8  See AUTHORS for details
9 
10  This program is free software; the authors of any publication
11  arising from research using this software are asked to add the
12  following reference:
13  Enki - a fast 2D robot simulator
14  http://home.gna.org/enki
15  Stephane Magnenat <stephane at magnenat dot net>,
16  Markus Waibel <markus dot waibel at epfl dot ch>
17  Laboratory of Intelligent Systems, EPFL, Lausanne.
18 
19  You can redistribute this program and/or modify
20  it under the terms of the GNU General Public License as published by
21  the Free Software Foundation; either version 2 of the License, or
22  (at your option) any later version.
23 
24  This program is distributed in the hope that it will be useful,
25  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  GNU General Public License for more details.
28 
29  You should have received a copy of the GNU General Public License
30  along with this program; if not, write to the Free Software
31  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 */
33 
34 #ifndef __ENKI_TYPES_H
35 #define __ENKI_TYPES_H
36 
37 #include <vector>
38 #include <sstream>
39 #include <string>
40 #include <cassert>
41 #include <stdint.h> // C99 in waiting for widespread C++11 support
42 
52 namespace Enki
53 {
55  struct Color
56  {
58  double components[4];
59 
61  Color(double r = 0.0, double g = 0.0, double b = 0.0, double a = 1.0)
62  {
63  components[0] = r;
64  components[1] = g;
65  components[2] = b;
66  components[3] = a;
67  }
68 
70  const double& operator[](size_t i) const { assert((i >= 0) && (i < 4)); return components[i]; }
72  double& operator[](size_t i) { assert((i >= 0) && (i < 4)); return components[i]; }
73 
74  // operations with scalar
76  void operator +=(double d) { for (size_t i=0; i<3; i++) components[i] += d; }
78  Color operator +(double d) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] + d; return c; }
79 
81  void operator -=(double d) { for (size_t i=0; i<3; i++) components[i] -= d; }
83  Color operator -(double d) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] - d; return c; }
84 
86  void operator *=(double d) { for (size_t i=0; i<3; i++) components[i] *= d; }
88  Color operator *(double d) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] * d; return c; }
89 
91  void operator /=(double d) { for (size_t i=0; i<3; i++) components[i] /= d; }
93  Color operator /(double d) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] / d; return c; }
94 
95  // operation with another color
97  void operator +=(const Color &oc) { for (size_t i=0; i<3; i++) components[i] += oc.components[i]; }
99  Color operator +(const Color &oc) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] + oc.components[i]; return c; }
100 
102  void operator -=(const Color &oc) { for (size_t i=0; i<3; i++) components[i] -= oc.components[i]; }
104  Color operator -(const Color &oc) const { Color c; for (size_t i=0; i<3; i++) c.components[i] = components[i] - oc.components[i]; return c; }
105 
107  bool operator ==(const Color &c) const { for (size_t i=0; i<4; i++) if (components[i] != c.components[i]) return false; return true; }
109  bool operator !=(const Color &c) const { return !(*this == c); }
111  void threshold(const Color &limit) { for (size_t i=0; i<3; i++) components[i] = components[i] > limit.components[i] ? components[i] : 0; }
113  double toGray() const { return (components[0] + components[1] + components[2]) / 3; }
114 
116  std::string toString() const { std::ostringstream oss; oss << *this; return oss.str(); }
117 
119  double r() const { return components[0]; }
120 
122  void setR(double value) { components[0] = value; }
123 
125  double g() const { return components[1]; }
126 
128  void setG(double value) { components[1] = value; }
129 
131  double b() const { return components[2]; }
132 
134  void setB(double value) { components[2] = value; }
135 
137  double a() const { return components[3]; }
138 
140  void setA(double value) { components[3] = value; }
141 
143  static Color fromARGB(uint32_t color);
145  static Color fromABGR(uint32_t color);
147  static uint32_t toARGB(Color color);
148 
150  static const Color black;
152  static const Color white;
154  static const Color gray;
156  static const Color red;
158  static const Color green;
160  static const Color blue;
161 
162  friend std::ostream & operator<<(std::ostream &os, const Color& c);
163  };
164 
166  typedef std::vector<Color> Texture;
167 
169  typedef std::vector<Texture> Textures;
170 }
171 
172 #endif
void setA(double value)
Set the value of alpha component.
Definition: Types.h:140
void setG(double value)
Set the value of green component.
Definition: Types.h:128
void operator-=(double d)
Substract d from each component.
Definition: Types.h:81
void setB(double value)
Set the value of blue component.
Definition: Types.h:134
std::vector< Color > Texture
A texture.
Definition: Types.h:166
const double & operator[](size_t i) const
access component i
Definition: Types.h:70
double & operator[](size_t i)
access component i
Definition: Types.h:72
std::string toString() const
Return a string describing this color.
Definition: Types.h:116
static const Color white
white (1, 1, 1)
Definition: Types.h:152
Color operator/(double d) const
Divide each component with d and return result in a new color. I&#39;m left unchanged.
Definition: Types.h:93
double components[4]
RGBA values in range [0..1].
Definition: Types.h:58
static const Color black
black (0, 0, 0)
Definition: Types.h:150
static const Color gray
gray (0.5, 0.5, 0.5)
Definition: Types.h:154
static Color fromARGB(uint32_t color)
Build from an ARGB uint32_t (0xAARRGGBB in little endian)
Definition: Types.cpp:42
Color(double r=0.0, double g=0.0, double b=0.0, double a=1.0)
Constructor from separated components.
Definition: Types.h:61
static uint32_t toARGB(Color color)
Pack into ABGR uint32_t (0xAABBGGRR in little endian)
Definition: Types.cpp:60
Enki is the namespace of the Enki simulator. All Enki functions and classes excepted the math one are...
Definition: BluetoothBase.cpp:44
static const Color green
green (0, 1, 0)
Definition: Types.h:158
std::vector< Texture > Textures
Textures for all sides of an object.
Definition: Types.h:169
static Color fromABGR(uint32_t color)
Build from an ABGR uint32_t (0xAABBGGRR in little endian)
Definition: Types.cpp:51
void operator/=(double d)
Divide each component with d.
Definition: Types.h:91
double a() const
Alpha component value getter.
Definition: Types.h:137
bool operator!=(const Color &c) const
Compare all components and return false if they&#39;re the same.
Definition: Types.h:109
Color operator-(double d) const
Substract d from each component and return result in a new color. I&#39;m left unchanged.
Definition: Types.h:83
void operator*=(double d)
Multiply each component with d.
Definition: Types.h:86
bool operator==(const Color &c) const
Compare all components and return true if they&#39;re the same.
Definition: Types.h:107
void operator+=(double d)
Add d to each component.
Definition: Types.h:76
Color operator+(double d) const
Add d to each component and return result in a new color. I&#39;m left unchanged.
Definition: Types.h:78
static const Color blue
blue (0, 0, 1)
Definition: Types.h:160
A color in RGBA.
Definition: Types.h:55
double toGray() const
Return the grey level value.
Definition: Types.h:113
double b() const
Blue component value getter.
Definition: Types.h:131
Color operator*(double d) const
Multiply each component with d and return result in a new color. I&#39;m left unchanged.
Definition: Types.h:88
double g() const
Green component value getter.
Definition: Types.h:125
void setR(double value)
Set the value of red component.
Definition: Types.h:122
double r() const
Red component value getter.
Definition: Types.h:119
static const Color red
red (1, 0, 0)
Definition: Types.h:156
void threshold(const Color &limit)
Threshold the color using limit. For each component, if value is below limit, set it to 0...
Definition: Types.h:111