Oxygine  1
2g game engine
Color.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include "ScalarMath.h"
4 #include "Vector4.h"
5 
6 namespace oxygine
7 {
8  class Color
9  {
10  public:
11  enum
12  {
14  Zero = 0x00000000,
15  AliceBlue = 0xF0F8FFFF,
16  AntiqueWhite = 0xFAEBD7FF,
17  Aqua = 0x00FFFFFF,
18  Aquamarine = 0x7FFFD4FF,
19  Azure = 0xF0FFFFFF,
20  Beige = 0xF5F5DCFF,
21  Bisque = 0xFFE4C4FF,
22  Black = 0x000000FF,
23  BlanchedAlmond = 0xFFEBCDFF,
24  Blue = 0x0000FFFF,
25  BlueViolet = 0x8A2BE2FF,
26  Brown = 0xA52A2AFF,
27  BurlyWood = 0xDEB887FF,
28  CadetBlue = 0x5F9EA0FF,
29  Chartreuse = 0x7FFF00FF,
30  Chocolate = 0xD2691EFF,
31  Coral = 0xFF7F50FF,
32  CornflowerBlue = 0x6495EDFF,
33  Cornsilk = 0xFFF8DCFF,
34  Crimson = 0xDC143CFF,
35  Cyan = 0x00FFFFFF,
36  DarkBlue = 0x00008BFF,
37  DarkCyan = 0x008B8BFF,
38  DarkGoldenrod = 0xB8860BFF,
39  DarkGray = 0xA9A9A9FF,
40  DarkGreen = 0x006400FF,
41  DarkKhaki = 0xBDB76BFF,
42  DarkMagenta = 0x8B008BFF,
43  DarkOliveGreen = 0x556B2FFF,
44  DarkOrange = 0xFF8C00FF,
45  DarkOrchid = 0x9932CCFF,
46  DarkRed = 0x8B0000FF,
47  DarkSalmon = 0xE9967AFF,
48  DarkSeaGreen = 0x8FBC8BFF,
49  DarkSlateBlue = 0x483D8BFF,
50  DarkSlateGray = 0x2F4F4FFF,
51  DarkTurquoise = 0x00CED1FF,
52  DarkViolet = 0x9400D3FF,
53  DeepPink = 0xFF1493FF,
54  DeepSkyBlue = 0x00BFFFFF,
55  DimGray = 0x696969FF,
56  DodgerBlue = 0x1E90FFFF,
57  Firebrick = 0xB22222FF,
58  FloralWhite = 0xFFFAF0FF,
59  ForestGreen = 0x228B22FF,
60  Fuchsia = 0xFF00FFFF,
61  Gainsboro = 0xDCDCDCFF,
62  GhostWhite = 0xF8F8FFFF,
63  Gold = 0xFFD700FF,
64  Goldenrod = 0xDAA520FF,
65  Gray = 0x808080FF,
66  Green = 0x008000FF,
67  GreenYellow = 0xADFF2FFF,
68  Honeydew = 0xF0FFF0FF,
69  HotPink = 0xFF69B4FF,
70  IndianRed = 0xCD5C5CFF,
71  Indigo = 0x4B0082FF,
72  Ivory = 0xFFFFF0FF,
73  Khaki = 0xF0E68CFF,
74  Lavender = 0xE6E6FAFF,
75  LavenderBlush = 0xFFF0F5FF,
76  LawnGreen = 0x7CFC00FF,
77  LemonChiffon = 0xFFFACDFF,
78  LightBlue = 0xADD8E6FF,
79  LightCoral = 0xF08080FF,
80  LightCyan = 0xE0FFFFFF,
81  LightGoldenrodYellow = 0xFAFAD2FF,
82  LightGray = 0xD3D3D3FF,
83  LightGreen = 0x90EE90FF,
84  LightPink = 0xFFB6C1FF,
85  LightSalmon = 0xFFA07AFF,
86  LightSeaGreen = 0x20B2AAFF,
87  LightSkyBlue = 0x87CEFAFF,
88  LightSlateGray = 0x778899FF,
89  LightSteelBlue = 0xB0C4DEFF,
90  LightYellow = 0xFFFFE0FF,
91  Lime = 0x00FF00FF,
92  LimeGreen = 0x32CD32FF,
93  Linen = 0xFAF0E6FF,
94  Magenta = 0xFF00FFFF,
95  Maroon = 0x800000FF,
96  MediumAquamarine = 0x66CDAAFF,
97  MediumBlue = 0x0000CDFF,
98  MediumOrchid = 0xBA55D3FF,
99  MediumPurple = 0x9370DBFF,
100  MediumSeaGreen = 0x3CB371FF,
101  MediumSlateBlue = 0x7B68EEFF,
102  MediumSpringGreen = 0x00FA9AFF,
103  MediumTurquoise = 0x48D1CCFF,
104  MediumVioletRed = 0xC71585FF,
105  MidnightBlue = 0x191970FF,
106  MintCream = 0xF5FFFAFF,
107  MistyRose = 0xFFE4E1FF,
108  Moccasin = 0xFFE4B5FF,
109  NavajoWhite = 0xFFDEADFF,
110  Navy = 0x000080FF,
111  OldLace = 0xFDF5E6FF,
112  Olive = 0x808000FF,
113  OliveDrab = 0x6B8E23FF,
114  Orange = 0xFFA500FF,
115  OrangeRed = 0xFF4500FF,
116  Orchid = 0xDA70D6FF,
117  PaleGoldenrod = 0xEEE8AAFF,
118  PaleGreen = 0x98FB98FF,
119  PaleTurquoise = 0xAFEEEEFF,
120  PaleVioletRed = 0xDB7093FF,
121  PapayaWhip = 0xFFEFD5FF,
122  PeachPuff = 0xFFDAB9FF,
123  Peru = 0xCD853FFF,
124  Pink = 0xFFC0CBFF,
125  Plum = 0xDDA0DDFF,
126  PowderBlue = 0xB0E0E6FF,
127  Purple = 0x800080FF,
128  Red = 0xFF0000FF,
129  RosyBrown = 0xBC8F8FFF,
130  RoyalBlue = 0x4169E1FF,
131  SaddleBrown = 0x8B4513FF,
132  Salmon = 0xFA8072FF,
133  SandyBrown = 0xF4A460FF,
134  SeaGreen = 0x2E8B57FF,
135  SeaShell = 0xFFF5EEFF,
136  Sienna = 0xA0522DFF,
137  Silver = 0xC0C0C0FF,
138  SkyBlue = 0x87CEEBFF,
139  SlateBlue = 0x6A5ACDFF,
140  SlateGray = 0x708090FF,
141  Snow = 0xFFFAFAFF,
142  SpringGreen = 0x00FF7FFF,
143  SteelBlue = 0x4682B4FF,
144  Tan = 0xD2B48CFF,
145  Teal = 0x008080FF,
146  Thistle = 0xD8BFD8FF,
147  Tomato = 0xFF6347FF,
148  Turquoise = 0x40E0D0FF,
149  Violet = 0xEE82EEFF,
150  Wheat = 0xF5DEB3FF,
151  White = 0xFFFFFFFF,
152  WhiteSmoke = 0xF5F5F5FF,
153  Yellow = 0xFFFF00FF,
154  YellowGreen = 0x9ACD32FF
155  };
156 
157  Color(): argb(0xffffffff) {}
158  Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255)
159  {
160  this->a = a;
161  this->r = r;
162  this->g = g;
163  this->b = b;
164  }
165 
166  static Color fromRGBA(unsigned int rgba)
167  {
168  return Color(rgba & 0xFF, (rgba >> 8) & 0xFF, (rgba >> 16) & 0xFF, rgba >> 24);
169  }
170 
172  Color(unsigned int abgr)
173  {
174  r = abgr >> 24;
175  g = (abgr >> 16) & 0xFF;
176  b = (abgr >> 8) & 0xFF;
177  a = abgr & 0xFF;
178  }
179 
181  Color(unsigned int abgr, unsigned char a_)
182  {
183  r = abgr >> 24;
184  g = (abgr >> 16) & 0xFF;
185  b = (abgr >> 8) & 0xFF;
186  a = a_;
187  }
188 
189  unsigned char operator [](int i) const
190  {
191  return colors[i];
192  }
193 
194 
195  unsigned int rgba() const
196  {
197  unsigned int c = 0;
198  unsigned char* p = (unsigned char*)(&c);
199  p[0] = r;
200  p[1] = g;
201  p[2] = b;
202  p[3] = a;
203  return c;
204  }
205 
206  static Color lerp(const Color& a, const Color& b, float v)
207  {
208  Color c;
209  for (int i = 0; i < 4; ++i)
210  c.colors[i] = oxygine::lerp(a.colors[i], b.colors[i], v);
211 
212  return c;
213  }
214 
215  float getRedF() const {return r / 255.0f;}
216  float getGreenF() const {return g / 255.0f;}
217  float getBlueF() const {return b / 255.0f;}
218  float getAlphaF() const {return a / 255.0f;}
219 
220  Vector4 toVector()const { return Vector4(getRedF(), getGreenF(), getBlueF(), getAlphaF()); }
221 
222  Color operator + (const Color& c) const
223  {
224  Color v(
225  scalar::clamp(r + c.r, 0, 255),
226  scalar::clamp(g + c.g, 0, 255),
227  scalar::clamp(b + c.b, 0, 255),
228  scalar::clamp(a + c.a, 0, 255));
229  return v;
230  }
231 
232  Color operator - (const Color& c) const
233  {
234  Color v(
235  scalar::clamp(r - c.r, 0, 255),
236  scalar::clamp(g - c.g, 0, 255),
237  scalar::clamp(b - c.b, 0, 255),
238  scalar::clamp(a - c.a, 0, 255));
239  return v;
240  }
241 
242 
243  Color operator * (const Color& c) const
244  {
245  Color v(
246  r * c.r / 255,
247  g * c.g / 255,
248  b * c.b / 255,
249  a * c.a / 255);
250  return v;
251  }
252  /*
253  Color operator / (const Color &c) const
254  {
255  Color cc = c;
256  const int v = 1;
257  if (cc.r == 0)
258  cc.r = v;
259  if (cc.g == 0)
260  cc.g = v;
261  if (cc.b == 0)
262  cc.b = v;
263  if (cc.a == 0)
264  cc.a = v;
265  Color r(
266  r * 255 / cc.r,
267  g * 255 / cc.g,
268  b * 255 / cc.b,
269  a * 255 / cc.a);
270  return r;
271  }
272  */
273 
274  Color premultiplied() const
275  {
276  Color c = *this;
277  c.r = (c.r * a) / 255;
278  c.g = (c.g * a) / 255;
279  c.b = (c.b * a) / 255;
280 
281  return c;
282  }
283 
284  Color withAlpha(unsigned char alpha) const
285  {
286  Color c = *this;
287  c.a = alpha;
288  return c;
289  }
290 
291  bool operator == (const Color& color) const
292  {
293  return argb == color.argb;
294  }
295 
296  bool operator != (const Color& color) const
297  {
298  return argb != color.argb;
299  }
300 
301  union
302  {
303  struct
304  {
305  unsigned char a, r, g, b;
306  };
307  unsigned int argb;
308  unsigned char colors[4];
309  };
310  };
311 
312  inline Color lerp(const Color& a, const Color& b, float v)
313  {
314  return Color::lerp(a, b, v);
315  }
316 }
Definition: Color.h:14
Color(unsigned int abgr, unsigned char a_)
Definition: Color.h:181
–oxgl-end–!
Definition: Actor.h:14
Color(unsigned int abgr)
Definition: Color.h:172
Definition: Color.h:8