3es  0.7
3escolour.h
1 //
2 // author Kazys Stepanas
3 //
4 // Copyright (c) Kazys Stepanas 2014
5 //
6 #ifndef _3ESCOLOUR_H_
7 #define _3ESCOLOUR_H_
8 
9 #include "3es-core.h"
10 
11 #include <cstdint>
12 
13 namespace tes
14 {
19  class _3es_coreAPI Colour
20  {
21  public:
23  enum Channels
24  {
25 #if TES_IS_BIG_ENDIAN
26  R = 0,
27  G = 1,
28  B = 2,
29  A = 3
30 #else // TES_IS_BIG_ENDIAN
31  A = 0,
32  B = 1,
33  G = 2,
34  R = 3
35 #endif // TES_IS_BIG_ENDIAN
36  };
37  union
38  {
39  uint32_t c;
40 
41  struct
42  {
43 #if TES_IS_BIG_ENDIAN
44  uint8_t r;
45  uint8_t g;
46  uint8_t b;
47  uint8_t a;
48 #else // TES_IS_BIG_ENDIAN
49  uint8_t a;
50  uint8_t b;
51  uint8_t g;
52  uint8_t r;
53 #endif // TES_IS_BIG_ENDIAN
54  };
55  uint8_t rgba[4];
56  };
57 
60  Colour(uint32_t c = 0xffffffffu);
61 
64  Colour(const Colour &other);
65 
69  Colour(const Colour &other, uint8_t a);
73  Colour(const Colour &other, int a);
77  Colour(const Colour &other, float a);
78 
84  explicit Colour(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255u);
85 
91  Colour(int r, int g, int b, int a = 255);
92 
98  Colour(float r, float g, float b, float a = 1.0f);
99 
102  float rf() const;
105  float gf() const;
108  float bf() const;
111  float af() const;
112 
115  void setRf(float f);
118  void setGf(float f);
121  void setBf(float f);
124  void setAf(float f);
125 
129  void setf(float f, int index);
133  float getf(int index) const;
134 
138  Colour adjust(float factor) const;
139 
142  inline Colour lighten() const { return adjust(1.5f); }
143 
146  inline Colour darken() const { return adjust(0.5f); }
147 
155  static void rgbToHsv(float &h, float &s, float &v, const float r, const float g, const float b);
156 
164  static void hsvToRgb(float &r, float &g, float &b, const float h, const float s, const float v);
172  static void hsvToRgb(uint8_t &r, uint8_t &g, uint8_t &b,
173  const float h, const float s, const float v);
174 
178  Colour &operator=(const Colour &other);
179 
180  //inline operator uint32_t() const { return c; }
181 
185  bool operator==(const Colour &other) const;
189  bool operator!=(const Colour &other) const;
190 
193  {
194  // Greys and blacks.
195  Gainsboro,
196  LightGrey,
197  Silver,
198  DarkGrey,
199  Grey,
200  DimGrey,
201  LightSlateGrey,
202  SlateGrey,
203  DarkSlateGrey,
204  Black,
205 
206  // Whites
207  White,
208  Snow,
209  Honeydew,
210  MintCream,
211  Azure,
212  AliceBlue,
213  GhostWhite,
214  WhiteSmoke,
215  Seashell,
216  Beige,
217  OldLace,
218  FloralWhite,
219  Ivory,
220  AntiqueWhite,
221  Linen,
222  LavenderBlush,
223  MistyRose,
224 
225  // Pinks
226  Pink,
227  LightPink,
228  HotPink,
229  DeepPink,
230  PaleVioletRed,
231  MediumVioletRed,
232 
233  // Reds
234  LightSalmon,
235  Salmon,
236  DarkSalmon,
237  LightCoral,
238  IndianRed,
239  Crimson,
240  FireBrick,
241  DarkRed,
242  Red,
243 
244  // Oranges
245  OrangeRed,
246  Tomato,
247  Coral,
248  DarkOrange,
249  Orange,
250 
251  // Yellows
252  Yellow,
253  LightYellow,
254  LemonChiffon,
255  LightGoldenrodYellow,
256  PapayaWhip,
257  Moccasin,
258  PeachPuff,
259  PaleGoldenrod,
260  Khaki,
261  DarkKhaki,
262  Gold,
263 
264  // Browns
265  Cornsilk,
266  BlanchedAlmond,
267  Bisque,
268  NavajoWhite,
269  Wheat,
270  BurlyWood,
271  Tan,
272  RosyBrown,
273  SandyBrown,
274  Goldenrod,
275  DarkGoldenrod,
276  Peru,
277  Chocolate,
278  SaddleBrown,
279  Sienna,
280  Brown,
281  Maroon,
282 
283  // Greens
284  DarkOliveGreen,
285  Olive,
286  OliveDrab,
287  YellowGreen,
288  LimeGreen,
289  Lime,
290  LawnGreen,
291  Chartreuse,
292  GreenYellow,
293  SpringGreen,
294  MediumSpringGreen,
295  LightGreen,
296  PaleGreen,
297  DarkSeaGreen,
298  MediumSeaGreen,
299  SeaGreen,
300  ForestGreen,
301  Green,
302  DarkGreen,
303 
304  // Cyans
305  MediumAquamarine,
306  Aqua,
307  Cyan,
308  LightCyan,
309  PaleTurquoise,
310  Aquamarine,
311  Turquoise,
312  MediumTurquoise,
313  DarkTurquoise,
314  LightSeaGreen,
315  CadetBlue,
316  DarkCyan,
317  Teal,
318 
319  // Blues
320  LightSteelBlue,
321  PowderBlue,
322  LightBlue,
323  SkyBlue,
324  LightSkyBlue,
325  DeepSkyBlue,
326  DodgerBlue,
327  CornflowerBlue,
328  SteelBlue,
329  RoyalBlue,
330  Blue,
331  MediumBlue,
332  DarkBlue,
333  Navy,
334  MidnightBlue,
335 
336  // Purples
337  Lavender,
338  Thistle,
339  Plum,
340  Violet,
341  Orchid,
342  Fuchsia,
343  Magenta,
344  MediumOrchid,
345  MediumPurple,
346  BlueViolet,
347  DarkViolet,
348  DarkOrchid,
349  DarkMagenta,
350  Purple,
351  Indigo,
352  DarkSlateBlue,
353  SlateBlue,
354  MediumSlateBlue,
355 
356  PredefinedCount
357  };
358 
368  {
380  CycleCount
381  };
382 
384  static const Colour Colours[PredefinedCount];
385 
390  static const int *ColourCycles[CycleCount];
391 
393  static const int CycleCounts[CycleCount];
394 
405  static const Colour &cycle(unsigned number, ColourCycle cycle = StandardCycle);
406  };
407 
408 
409  inline Colour::Colour(uint32_t c)
410  : c(c)
411  {
412  }
413 
414 
415  inline Colour::Colour(const Colour &other)
416  : c(other.c)
417  {
418  }
419 
420 
421  inline Colour::Colour(const Colour &other, uint8_t a)
422  : c(other.c)
423  {
424  this->a = a;
425  }
426 
427 
428  inline Colour::Colour(const Colour &other, int a)
429  : c(other.c)
430  {
431  this->a = a;
432  }
433 
434 
435  inline Colour::Colour(const Colour &other, float a)
436  : c(other.c)
437  {
438  setAf(a);
439  }
440 
441 
442  inline Colour::Colour(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
443  : r(r), g(g), b(b), a(a)
444  {
445  }
446 
447 
448  inline Colour::Colour(int r, int g, int b, int a)
449  : r(uint8_t(r))
450  , g(uint8_t(g))
451  , b(uint8_t(b))
452  , a(uint8_t(a))
453  {
454  }
455 
456 
457  inline Colour::Colour(float r, float g, float b, float a)
458  {
459  setRf(r);
460  setGf(g);
461  setBf(b);
462  setAf(a);
463  }
464 
465  inline float Colour::rf() const
466  {
467  return getf(0);
468  }
469 
470 
471  inline float Colour::gf() const
472  {
473  return getf(1);
474  }
475 
476 
477  inline float Colour::bf() const
478  {
479  return getf(2);
480  }
481 
482 
483  inline float Colour::af() const
484  {
485  return getf(3);
486  }
487 
488 
489  inline void Colour::setRf(float f)
490  {
491  setf(f, 0);
492  }
493 
494 
495  inline void Colour::setGf(float f)
496  {
497  setf(f, 1);
498  }
499 
500 
501  inline void Colour::setBf(float f)
502  {
503  setf(f, 2);
504  }
505 
506 
507  inline void Colour::setAf(float f)
508  {
509  setf(f, 3);
510  }
511 
512 
513  inline void Colour::setf(float f, int index)
514  {
515  rgba[index] = uint8_t(f * 255.0f);
516  }
517 
518 
519  inline float Colour::getf(int index) const
520  {
521  return rgba[index] / 255.0f;
522  }
523 
524 
525  inline Colour &Colour::operator=(const Colour &other)
526  {
527  c = other.c;
528  return *this;
529  }
530 
531 
532  inline bool Colour::operator==(const Colour &other) const
533  {
534  return c == other.c;
535  }
536 
537 
538  inline bool Colour::operator!=(const Colour &other) const
539  {
540  return c != other.c;
541  }
542 }
543 
544 #endif // _3ESCOLOUR_H_
A 32-bit integer colour class.
Definition: 3escolour.h:19
float getf(int index) const
Get a channel in floating point form.
Definition: 3escolour.h:519
bool operator==(const Colour &other) const
Precise equality operator.
Definition: 3escolour.h:532
Colour(uint32_t c=0xffffffffu)
Construct a colour with the given numeric value.
Definition: 3escolour.h:409
void setGf(float f)
Set green channel from a floating point value.
Definition: 3escolour.h:495
uint8_t r
Red channel.
Definition: 3escolour.h:52
Colour lighten() const
Lighten the colour by 1.5.
Definition: 3escolour.h:142
Channels
Channel index enumeration.
Definition: 3escolour.h:23
uint8_t a
Alpha channel.
Definition: 3escolour.h:49
uint8_t rgba[4]
Indexed channels.
Definition: 3escolour.h:55
float rf() const
Get red channel in floating point form.
Definition: 3escolour.h:465
Definition: 3esbounds.h:13
Colour & operator=(const Colour &other)
Assignment operator.
Definition: 3escolour.h:525
void setf(float f, int index)
Set a channel in floating point form.
Definition: 3escolour.h:513
void setAf(float f)
Set alpha channel from a floating point value.
Definition: 3escolour.h:507
Predefined
Enumerates a set of predefined colours ("web safe" colours).
Definition: 3escolour.h:192
uint8_t b
Blue channel.
Definition: 3escolour.h:50
float gf() const
Get green channel in floating point form.
Definition: 3escolour.h:471
A colour set which attempts to cater for Protanomaly colour blindness.
Definition: 3escolour.h:374
A small grey scale colour set.
Definition: 3escolour.h:378
float bf() const
Get blue channel in floating point form.
Definition: 3escolour.h:477
uint32_t c
Encoded colour value.
Definition: 3escolour.h:39
uint8_t g
Green channel.
Definition: 3escolour.h:51
A colour set which attempts to cater for Deuteranomaly colour blindness.
Definition: 3escolour.h:372
bool operator!=(const Colour &other) const
Precise inequality operator.
Definition: 3escolour.h:538
float af() const
Get alpha channel in floating point form.
Definition: 3escolour.h:483
ColourCycle
Enumerates the various available colour cycles.
Definition: 3escolour.h:367
void setRf(float f)
Set red channel from a floating point value.
Definition: 3escolour.h:489
Colour darken() const
Darken the colour by 0.5.
Definition: 3escolour.h:146
A colour set which attempts to cater for Tritanomaly colour blindness.
Definition: 3escolour.h:376
Standard colour set.
Definition: 3escolour.h:370
void setBf(float f)
Set blue channel from a floating point value.
Definition: 3escolour.h:501