alaCarte Maps
Renderer for OpenStreetMap tiles
color.hpp
Go to the documentation of this file.
1 
21 #pragma once
22 #ifndef COLOR_HPP
23 #define COLOR_HPP
24 
25 /*
26  * =====================================================================================
27  *
28  * Filename: settings.h
29  *
30  * Description:
31  *
32  * =====================================================================================
33  */
34 
35 #include "settings.hpp"
36 
37 
38 #define cxCOLOR_CONV (0.003921568627450980392156862745098f)
39 
40 class Color
41 {
42 public:
43  Color() : r(1.0f), g(1.0f), b(1.0f), a(1.0f) {}
44  Color(const Color& _c) : r(_c.r), g(_c.g), b(_c.b), a(_c.a) {}
45  Color(const float _f) : r(_f), g(_f), b(_f), a(1.0f) {}
46  explicit Color(const float _r, const float _g, const float _b)
47  : r(_r), g(_g), b(_b), a(1.0f) {}
48  explicit Color(const float _r, const float _g, const float _b, const float _a)
49  : r(_r), g(_g), b(_b), a(_a) {}
50  explicit Color(uint8 _r, uint8 _g, uint8 _b)
51  : r((float)(_r) * cxCOLOR_CONV)
52  , g((float)(_g) * cxCOLOR_CONV)
53  , b((float)(_b) * cxCOLOR_CONV)
54  , a(1.0f) {}
55  explicit Color(uint8 _r, uint8 _g, uint8 _b, uint8 _a)
56  : r((float)(_r) * cxCOLOR_CONV)
57  , g((float)(_g) * cxCOLOR_CONV)
58  , b((float)(_b) * cxCOLOR_CONV)
59  , a((float)(_a) * cxCOLOR_CONV) {}
60  Color(const float* _c)
61  : r(_c[0]), g(_c[1]), b(_c[2]), a(_c[3]) {}
62  Color(const uint8* pComponent)
63  : r((float)(pComponent[0]) * cxCOLOR_CONV)
64  , g((float)(pComponent[1]) * cxCOLOR_CONV)
65  , b((float)(pComponent[2]) * cxCOLOR_CONV)
66  , a((float)(pComponent[3]) * cxCOLOR_CONV) {}
67 
69  : r(cxCOLOR_CONV * (float)(uint8)(_c >> 16))
70  , g(cxCOLOR_CONV * (float)(uint8)(_c >> 8))
71  , b(cxCOLOR_CONV * (float)(uint8)(_c))
72  , a(cxCOLOR_CONV * (float)(uint8)(_c >> 24))
73  {}
74 
75 
76  operator uint32 () const
77  {
78  return ((a >= 1.0f ? 255 : a <= 0.0f ? 0 : (uint32)(a * 255.0f)) << 24) |
79  ((r >= 1.0f ? 255 : r <= 0.0f ? 0 : (uint32)(r * 255.0f)) << 16) |
80  ((g >= 1.0f ? 255 : g <= 0.0f ? 0 : (uint32)(g * 255.0f)) << 8) |
81  (b >= 1.0f ? 255 : b <= 0.0f ? 0 : (uint32)(b * 255.0f));
82  }
83 
84  operator float* () {return (float*)(c);}
85 
86  inline Color& operator += (const Color& c) {r += c.r; g += c.g; b += c.b; a += c.a; return *this;}
87  inline Color& operator -= (const Color& c) {r -= c.r; g -= c.g; b -= c.b; a -= c.a; return *this;}
88  inline Color& operator *= (const Color& c) {r *= c.r; g *= c.g; b *= c.b; a *= c.a; return *this;}
89  inline Color& operator *= (const float f) {r *= f; g *= f; b *= f; a *= f; return *this;}
90  inline Color& operator /= (const Color& c) {r /= c.r; g /= c.g; b /= c.b; a /= c.a; return *this;}
91  inline Color& operator /= (const float f) {r /= f; g /= f; b /= f; a /= f; return *this;}
92 
93  inline Color operator + (const Color& _c) const {return Color(r + _c.r, g + _c.g, b + _c.b, a + _c.a);}
94  inline Color operator - (const Color& _c) const {return Color(r - _c.r, g - _c.g, b - _c.b, a - _c.a);}
95  inline Color operator - () const {return Color(-r, -g, -b, a);}
96  inline Color operator * (const Color& _c) const {return Color(r * _c.r, g * _c.g, b * _c.b, a * _c.a);}
97  inline Color operator * (const float f) const {return Color(r * f, g * f, b * f, a * f);}
98  inline Color operator / (const Color& _c) const {return Color(r / _c.r, g / _c.g, b / _c.b, a / _c.a);}
99  inline Color operator / (const float f) const {return Color(r / f, g / f, b / f, a / f);}
100 
101  inline bool operator == (const Color& _c) const {return (uint32)*this == (uint32)_c;}
102  inline bool operator != (const Color& _c) const {return (uint32)*this != (uint32)_c;}
103 
104 
105  inline Color negate() const { return Color(1.0f - r, 1.0f - g, 1.0f - b, a); }
106  inline Color negateA() const { return Color(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a); }
107  inline Color min(const Color& _c) const { return Color(std::min(r, _c.r), std::min(g, _c.g), std::min(b, _c.b), std::min(a, _c.a)); }
108  inline Color max(const Color& _c) const { return Color(std::max(r, _c.r), std::max(g, _c.g), std::max(b, _c.b), std::max(a, _c.a)); }
109 
110  friend std::ostream& operator<< (std::ostream& stream, const Color& color)
111  {
112  stream << "Color(" << color.r << "," << color.g << "," << color.b << "," << color.a << ")";
113  return stream;
114  }
115 
116  //inline Color& rand() { r = Random::Rand<float>(1.0f); greturn (*this); }
117 
118  static const Color Null;
119  static const Color Black;
120  static const Color White;
121  static const Color Red;
122  static const Color Green;
123  static const Color Blue;
124  static const Color Yellow;
125  static const Color Grey;
126  static const Color LightGrey;
127 
128  union
129  {
130  struct
131  {
132  float r;
133  float g;
134  float b;
135  float a;
136  };
137  struct
138  {
139  float red;
140  float green;
141  float blue;
142  float alpha;
143  };
144  float c[4];
145  };
146 };
147 
148 #endif
#define cxCOLOR_CONV
Definition: color.hpp:38
float g
Definition: color.hpp:133
static const Color Black
Definition: color.hpp:119
Color min(const Color &_c) const
Definition: color.hpp:107
Color operator+(const Color &_c) const
Definition: color.hpp:93
std::uint8_t uint8
Definition: settings.hpp:112
float red
Definition: color.hpp:139
Color(const float _r, const float _g, const float _b)
Definition: color.hpp:46
static const Color Grey
Definition: color.hpp:125
Color & operator-=(const Color &c)
Definition: color.hpp:87
Color(const float _r, const float _g, const float _b, const float _a)
Definition: color.hpp:48
Color()
Definition: color.hpp:43
bool operator!=(const Color &_c) const
Definition: color.hpp:102
Color(const uint8 *pComponent)
Definition: color.hpp:62
std::uint32_t uint32
Definition: settings.hpp:116
Definition: color.hpp:40
Color negateA() const
Definition: color.hpp:106
float alpha
Definition: color.hpp:142
Color(const float _f)
Definition: color.hpp:45
float blue
Definition: color.hpp:141
Color & operator*=(const Color &c)
Definition: color.hpp:88
Color(uint32 _c)
Definition: color.hpp:68
float c[4]
Definition: color.hpp:144
friend std::ostream & operator<<(std::ostream &stream, const Color &color)
Definition: color.hpp:110
static const Color Green
Definition: color.hpp:122
Color negate() const
Definition: color.hpp:105
static const Color White
Definition: color.hpp:120
Color(const Color &_c)
Definition: color.hpp:44
bool operator==(const Color &_c) const
Definition: color.hpp:101
Color operator/(const Color &_c) const
Definition: color.hpp:98
Color & operator+=(const Color &c)
Definition: color.hpp:86
Color max(const Color &_c) const
Definition: color.hpp:108
float green
Definition: color.hpp:140
float a
Definition: color.hpp:135
static const Color Red
Definition: color.hpp:121
Color & operator/=(const Color &c)
Definition: color.hpp:90
Color(uint8 _r, uint8 _g, uint8 _b)
Definition: color.hpp:50
float b
Definition: color.hpp:134
static const Color Yellow
Definition: color.hpp:124
Color(const float *_c)
Definition: color.hpp:60
static const Color Blue
Definition: color.hpp:123
Color(uint8 _r, uint8 _g, uint8 _b, uint8 _a)
Definition: color.hpp:55
static const Color Null
Definition: color.hpp:118
Color operator*(const Color &_c) const
Definition: color.hpp:96
Color operator-() const
Definition: color.hpp:95
float r
Definition: color.hpp:132
static const Color LightGrey
Definition: color.hpp:126