You.i Engine
YiColor.h
Go to the documentation of this file.
1 // © You i Labs Inc. All rights reserved.
2 #ifndef _YI_COLOR_H_
3 #define _YI_COLOR_H_
4 
5 #include "utility/YiEndianness.h"
6 #include "utility/YiTypeTraits.h"
7 
8 #include <glm/vec4.hpp>
9 
10 class CYINamedColors;
11 class CYIString;
12 class CYIStringView;
13 
35 class CYIColor
36 {
37  friend bool operator==(const CYIColor &lhs, const CYIColor &rhs);
38  friend bool operator!=(const CYIColor &lhs, const CYIColor &rhs);
39  friend bool operator<(const CYIColor &lhs, const CYIColor &rhs);
40  friend bool operator<=(const CYIColor &lhs, const CYIColor &rhs);
41  friend bool operator>(const CYIColor &lhs, const CYIColor &rhs);
42  friend bool operator>=(const CYIColor &lhs, const CYIColor &rhs);
43 
44 public:
48  enum class ByteOrder
49  {
51  RGBA = 0,
53  ARGB,
55  BGRA,
57  ABGR
58  };
59 
65  CYIColor();
66 
70  CYIColor(const CYIColor &other);
71 
75  CYIColor(CYIColor &&other) noexcept;
76 
82  CYIColor(const CYIColor &color, float alpha);
83 
95  CYIColor(double red, double green, double blue, double alpha = 1.0);
96 
104  CYIColor(float red, float green, float blue, float alpha = 1.0f);
105 
112  CYIColor(const glm::vec3 &rgb);
113 
119  CYIColor(const glm::vec4 &rgba);
120 
128  explicit CYIColor(uint32_t packed, ByteOrder byteOrder = ByteOrder::ABGR);
129 
135  explicit CYIColor(CYIStringView name);
136 
141  CYIColor &operator=(const CYIColor &color);
142 
147  CYIColor &operator=(CYIColor &&color);
148 
155  CYIColor &operator=(const glm::vec3 &rgb);
156 
162  CYIColor &operator=(const glm::vec4 &rgba);
163 
168  void SetColor(const CYIColor &color);
169 
177  void SetColor(float red, float green, float blue, float alpha = 1.0f);
178 
186  void SetColorInteger(int32_t red, int32_t green, int32_t blue, int32_t alpha = 255);
187 
194  void SetColor(const glm::vec3 &rgb);
195 
201  void SetColor(const glm::vec4 &rgba);
202 
210  void SetColor(uint32_t packed, ByteOrder byteOrder = ByteOrder::ABGR);
211 
219  void SetRed(double red);
220 
224  void SetRed(float red);
225 
229  void SetRedInteger(int32_t red);
230 
238  void SetGreen(double green);
239 
243  void SetGreen(float green);
244 
248  void SetGreenInteger(int32_t green);
249 
257  void SetBlue(double blue);
258 
262  void SetBlue(float blue);
263 
267  void SetBlueInteger(int32_t blue);
268 
276  void SetAlpha(double alpha);
277 
281  void SetAlpha(float alpha);
282 
286  void SetAlphaInteger(int32_t alpha);
287 
291  void SetHSLHue(float hue);
292 
296  void SetHSLHueInteger(int32_t hue);
297 
301  void SetHSLSaturation(float saturation);
302 
306  void SetHSLSaturationInteger(int32_t saturation);
307 
311  void SetHSLLightness(float lightness);
312 
316  void SetHSLLightnessInteger(int32_t lightness);
317 
321  float GetRed() const;
322 
326  float GetGreen() const;
327 
331  float GetBlue() const;
332 
336  float GetAlpha() const;
337 
343  float GetHSLHue() const;
344 
350  float GetHSLSaturation() const;
351 
357  float GetHSLLightness() const;
358 
362  bool IsVisible() const;
363 
367  uint8_t GetRedByte() const;
368 
372  uint8_t GetGreenByte() const;
373 
377  uint8_t GetBlueByte() const;
378 
382  uint8_t GetAlphaByte() const;
383 
390  uint32_t GetPackedColor(ByteOrder byteOrder = ByteOrder::ABGR) const;
391 
395  CYIColor GetInverted() const;
396 
402  CYIColor GetPremultiplied() const;
403 
407  CYIColor GetClamped() const;
408 
414  CYIString ToHexString(ByteOrder byteOrder = ByteOrder::RGBA) const;
415 
419  const glm::vec4 &GetColorVector() const;
420 
428  glm::vec4 GetHSLAColorVector() const;
429 
436  static const CYINamedColors &Named();
437 
438  /*
439  \copydox #Named()
440  */
441  static const CYINamedColors &GetNamedColors();
442 
448  static CYIColor GetColorByName(CYIStringView colorName, bool *pError = nullptr);
449 
477  static CYIColor FromString(CYIStringView colorName, bool *pError = nullptr);
478 
482  static CYIColor FromColor(const CYIColor &color, float alpha);
483 
487  static CYIColor FromRGB(float red, float green, float blue, float alpha = 1.0f);
488 
492  static CYIColor FromRGBInteger(int32_t red, int32_t green, int32_t blue, int32_t alpha = 255);
493 
497  static CYIColor FromRGB(const glm::vec3 &rgbVector);
498 
502  static CYIColor FromRGBA(const glm::vec4 &rgbaVector);
503 
509  static CYIColor FromPackedRGBA(uint32_t packed, ByteOrder byteOrder = ByteOrder::ABGR);
510 
516  static CYIColor FromHSL(float hue, float saturation, float luminance, float alpha = 1.0f);
517 
525  static CYIColor FromHSL(const glm::vec3 &hslVector);
526 
534  static CYIColor FromHSLA(const glm::vec4 &hslaVector);
535 
539  static inline uint32_t GetPackedColor(uint8_t red, uint32_t green, uint32_t blue, uint32_t alpha, ByteOrder byteOrder = ByteOrder::ABGR);
540 
541  // The following functions have been removed (but that still need to be present in this class to avoid runtime errors)
542 
546  template<typename T = bool>
547  CYIColor(int32_t r, int32_t g, int32_t b, int32_t a = 255);
548 
552  template<typename T = bool>
553  CYIColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
554 
558  template<typename T = bool>
559  void SetColor(int32_t r, int32_t g, int32_t b, int32_t a = 255);
560 
564  template<typename T = bool>
565  void SetRed(int32_t r);
566 
570  template<typename T = bool>
571  void SetRed(uint8_t r);
572 
576  template<typename T = bool>
577  void SetGreen(int32_t g);
578 
582  template<typename T = bool>
583  void SetGreen(uint8_t g);
584 
588  template<typename T = bool>
589  void SetBlue(int32_t b);
590 
594  template<typename T = bool>
595  void SetBlue(uint8_t b);
596 
600  template<typename T = bool>
601  void SetAlpha(int32_t a);
602 
606  template<typename T = bool>
607  void SetAlpha(uint8_t a);
608 
609 private:
610  glm::vec4 m_rgbaData;
611 };
612 
622 {
623 public:
624  // Transparent color
626 
627  // Pink colors
628  const CYIColor Pink;
634 
635  // Red colors
644  const CYIColor Red;
645 
646  // Orange colors
652 
653  // Yellow colors
664  const CYIColor Gold;
665 
666  // Brown colors
673  const CYIColor Tan;
678  const CYIColor Peru;
684 
685  // Green colors
691  const CYIColor Lime;
705 
706  // Cyan colors
708  const CYIColor Aqua;
709  const CYIColor Cyan;
719  const CYIColor Teal;
720 
721  // Blue colors
732  const CYIColor Blue;
735  const CYIColor Navy;
737 
738  // Purple/Violet/Magenta colors
741  const CYIColor Plum;
758 
759  // White colors
761  const CYIColor Snow;
777 
778  // Gray/Black colors
783  const CYIColor Gray;
789 
790 private:
791  friend class CYIColor;
792  CYINamedColors();
793 };
794 
795 bool operator==(const CYIColor &lhs, const CYIColor &rhs);
796 bool operator!=(const CYIColor &lhs, const CYIColor &rhs);
797 bool operator<(const CYIColor &lhs, const CYIColor &rhs);
798 
801 #include "utility/internal/YiColor.inl"
802 
803 #endif // _YI_COLOR_H_
friend bool operator>=(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor Plum
Definition: YiColor.h:741
const CYIColor Thistle
Definition: YiColor.h:740
const CYIColor MediumSeaGreen
Definition: YiColor.h:700
friend bool operator==(const CYIColor &lhs, const CYIColor &rhs)
void SetRed(double red)
const CYIColor PowderBlue
Definition: YiColor.h:723
const CYIColor Wheat
Definition: YiColor.h:671
float GetAlpha() const
const CYIColor DarkGray
Definition: YiColor.h:782
const CYIColor Red
Definition: YiColor.h:644
const CYIColor DarkOrchid
Definition: YiColor.h:750
const CYIColor DarkMagenta
Definition: YiColor.h:751
void SetBlueInteger(int32_t blue)
static const CYINamedColors & GetNamedColors()
Definition: YiColor.h:621
const CYIColor White
Definition: YiColor.h:760
const CYIColor SlateGray
Definition: YiColor.h:786
const CYIColor YellowGreen
Definition: YiColor.h:689
const CYIColor BlanchedAlmond
Definition: YiColor.h:668
const CYIColor Silver
Definition: YiColor.h:781
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:32
const CYIColor Tomato
Definition: YiColor.h:648
const CYIColor LightSeaGreen
Definition: YiColor.h:716
const CYIColor Chocolate
Definition: YiColor.h:679
void SetHSLLightness(float lightness)
const CYIColor CornflowerBlue
Definition: YiColor.h:729
const CYIColor DarkSalmon
Definition: YiColor.h:638
const CYIColor CadetBlue
Definition: YiColor.h:717
const CYIColor Lavender
Definition: YiColor.h:739
const CYIColor DarkTurquoise
Definition: YiColor.h:715
const CYIColor IndianRed
Definition: YiColor.h:640
void SetGreenInteger(int32_t green)
bool IsVisible() const
const CYIColor LightSlateGray
Definition: YiColor.h:785
void SetHSLLightnessInteger(int32_t lightness)
float GetHSLSaturation() const
static CYIColor FromRGBA(const glm::vec4 &rgbaVector)
const CYIColor SandyBrown
Definition: YiColor.h:675
const CYIColor ForestGreen
Definition: YiColor.h:702
const CYIColor LightGreen
Definition: YiColor.h:697
const CYIColor MediumAquamarine
Definition: YiColor.h:707
ByteOrder
Definition: YiColor.h:48
const CYIColor FireBrick
Definition: YiColor.h:642
const CYIColor WhiteSmoke
Definition: YiColor.h:767
const CYIColor DarkCyan
Definition: YiColor.h:718
const CYIColor OldLace
Definition: YiColor.h:770
void SetHSLHue(float hue)
const CYIColor Khaki
Definition: YiColor.h:662
const CYIColor DarkViolet
Definition: YiColor.h:749
void SetRedInteger(int32_t red)
const CYIColor Honeydew
Definition: YiColor.h:762
const CYIColor GreenYellow
Definition: YiColor.h:694
const CYIColor Tan
Definition: YiColor.h:673
const CYIColor MediumPurple
Definition: YiColor.h:747
static CYIColor FromString(CYIStringView colorName, bool *pError=nullptr)
static const CYINamedColors & Named()
friend bool operator<(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor Blue
Definition: YiColor.h:732
void SetAlpha(double alpha)
const glm::vec4 & GetColorVector() const
CYIColor GetInverted() const
const CYIColor MediumSlateBlue
Definition: YiColor.h:757
const CYIColor DarkRed
Definition: YiColor.h:643
const CYIColor PaleGoldenrod
Definition: YiColor.h:661
const CYIColor NavajoWhite
Definition: YiColor.h:670
static CYIColor GetColorByName(CYIStringView colorName, bool *pError=nullptr)
const CYIColor RosyBrown
Definition: YiColor.h:674
const CYIColor MistyRose
Definition: YiColor.h:776
const CYIColor Black
Definition: YiColor.h:788
const CYIColor DarkBlue
Definition: YiColor.h:734
const CYIColor Orange
Definition: YiColor.h:651
const CYIColor MintCream
Definition: YiColor.h:763
uint8_t GetRedByte() const
const CYIColor Beige
Definition: YiColor.h:769
const CYIColor Violet
Definition: YiColor.h:742
const CYIColor SeaGreen
Definition: YiColor.h:701
void SetHSLHueInteger(int32_t hue)
void SetAlphaInteger(int32_t alpha)
const CYIColor LightSalmon
Definition: YiColor.h:636
const CYIColor AntiqueWhite
Definition: YiColor.h:773
void SetGreen(double green)
const CYIColor Lime
Definition: YiColor.h:691
const CYIColor LightSkyBlue
Definition: YiColor.h:726
uint8_t GetAlphaByte() const
const CYIColor MediumTurquoise
Definition: YiColor.h:714
const CYIColor Olive
Definition: YiColor.h:687
CYIColor GetClamped() const
const CYIColor SteelBlue
Definition: YiColor.h:730
const CYIColor Gray
Definition: YiColor.h:783
const CYIColor DarkOliveGreen
Definition: YiColor.h:686
float GetHSLHue() const
const CYIColor Cornsilk
Definition: YiColor.h:667
const CYIColor LightSteelBlue
Definition: YiColor.h:722
const CYIColor BurlyWood
Definition: YiColor.h:672
uint32_t GetPackedColor(ByteOrder byteOrder=ByteOrder::ABGR) const
const CYIColor Orchid
Definition: YiColor.h:743
const CYIColor DarkSlateGray
Definition: YiColor.h:787
const CYIColor Gainsboro
Definition: YiColor.h:779
static CYIColor FromHSL(float hue, float saturation, float luminance, float alpha=1.0f)
float GetGreen() const
static CYIColor FromHSLA(const glm::vec4 &hslaVector)
const CYIColor Purple
Definition: YiColor.h:752
const CYIColor Chartreuse
Definition: YiColor.h:693
CYIColor & operator=(const CYIColor &color)
Copy assignment operator.
const CYIColor Yellow
Definition: YiColor.h:654
const CYIColor Coral
Definition: YiColor.h:649
const CYIColor OliveDrab
Definition: YiColor.h:688
friend bool operator<=(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor Ivory
Definition: YiColor.h:772
void SetColorInteger(int32_t red, int32_t green, int32_t blue, int32_t alpha=255)
Sets the color to a given set of signed integer values.
const CYIColor Indigo
Definition: YiColor.h:753
const CYIColor MediumSpringGreen
Definition: YiColor.h:696
const CYIColor Maroon
Definition: YiColor.h:683
const CYIColor HotPink
Definition: YiColor.h:630
CYIColor GetPremultiplied() const
const CYIColor SaddleBrown
Definition: YiColor.h:680
const CYIColor Crimson
Definition: YiColor.h:641
const CYIColor DarkGreen
Definition: YiColor.h:704
const CYIColor Aqua
Definition: YiColor.h:708
CYIColor()
Default constructor.
const CYIColor Brown
Definition: YiColor.h:682
const CYIColor Moccasin
Definition: YiColor.h:659
float GetRed() const
const CYIColor LightGrey
Definition: YiColor.h:780
float GetHSLLightness() const
const CYIColor PapayaWhip
Definition: YiColor.h:658
friend bool operator>(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor DodgerBlue
Definition: YiColor.h:728
const CYIColor PaleTurquoise
Definition: YiColor.h:711
const CYIColor PeachPuff
Definition: YiColor.h:660
const CYIColor MediumBlue
Definition: YiColor.h:733
friend bool operator!=(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor Navy
Definition: YiColor.h:735
const CYIColor DeepPink
Definition: YiColor.h:631
const CYIColor DarkOrange
Definition: YiColor.h:650
const CYIColor Fuchsia
Definition: YiColor.h:744
const CYIColor DimGray
Definition: YiColor.h:784
uint8_t GetBlueByte() const
const CYIColor LawnGreen
Definition: YiColor.h:692
const CYIColor Pink
Definition: YiColor.h:628
const CYIColor Green
Definition: YiColor.h:703
static CYIColor FromRGBInteger(int32_t red, int32_t green, int32_t blue, int32_t alpha=255)
void SetBlue(double blue)
uint8_t GetGreenByte() const
const CYIColor LightBlue
Definition: YiColor.h:724
const CYIColor Azure
Definition: YiColor.h:764
const CYIColor LightYellow
Definition: YiColor.h:655
const CYIColor Cyan
Definition: YiColor.h:709
const CYIColor DarkKhaki
Definition: YiColor.h:663
const CYIColor LemonChiffon
Definition: YiColor.h:656
CYIString ToHexString(ByteOrder byteOrder=ByteOrder::RGBA) const
const CYIColor Aquamarine
Definition: YiColor.h:712
const CYIColor BlueViolet
Definition: YiColor.h:748
const CYIColor SlateBlue
Definition: YiColor.h:756
const CYIColor Sienna
Definition: YiColor.h:681
static CYIColor FromPackedRGBA(uint32_t packed, ByteOrder byteOrder=ByteOrder::ABGR)
const CYIColor Teal
Definition: YiColor.h:719
const CYIColor RebeccaPurple
Definition: YiColor.h:755
const CYIColor Seashell
Definition: YiColor.h:768
void SetColor(const CYIColor &color)
Sets the color to the same color of another RGB instance.
const CYIColor FloralWhite
Definition: YiColor.h:771
const CYIColor PaleGreen
Definition: YiColor.h:698
const CYIColor MidnightBlue
Definition: YiColor.h:736
const CYIColor SpringGreen
Definition: YiColor.h:695
const CYIColor Salmon
Definition: YiColor.h:637
const CYIColor RoyalBlue
Definition: YiColor.h:731
const CYIColor MediumOrchid
Definition: YiColor.h:746
const CYIColor LimeGreen
Definition: YiColor.h:690
void SetHSLSaturationInteger(int32_t saturation)
const CYIColor Linen
Definition: YiColor.h:774
A class used to represent a color value.
Definition: YiColor.h:35
const CYIColor DarkGoldenrod
Definition: YiColor.h:677
const CYIColor Magenta
Definition: YiColor.h:745
const CYIColor Gold
Definition: YiColor.h:664
const CYIColor AliceBlue
Definition: YiColor.h:765
const CYIColor LightCoral
Definition: YiColor.h:639
glm::vec4 GetHSLAColorVector() const
const CYIColor DeepSkyBlue
Definition: YiColor.h:727
const CYIColor Turquoise
Definition: YiColor.h:713
static CYIColor FromColor(const CYIColor &color, float alpha)
static CYIColor FromRGB(float red, float green, float blue, float alpha=1.0f)
const CYIColor DarkSeaGreen
Definition: YiColor.h:699
const CYIColor Peru
Definition: YiColor.h:678
float GetBlue() const
const CYIColor Transparent
Definition: YiColor.h:625
const CYIColor PaleVioletRed
Definition: YiColor.h:632
const CYIColor DarkSlateBlue
Definition: YiColor.h:754
const CYIColor LightCyan
Definition: YiColor.h:710
const CYIColor Snow
Definition: YiColor.h:761
const CYIColor SkyBlue
Definition: YiColor.h:725
const CYIColor LightGoldenrodYellow
Definition: YiColor.h:657
void SetHSLSaturation(float saturation)
const CYIColor Goldenrod
Definition: YiColor.h:676
const CYIColor OrangeRed
Definition: YiColor.h:647
const CYIColor LightPink
Definition: YiColor.h:629
const CYIColor GhostWhite
Definition: YiColor.h:766
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:61
const CYIColor MediumVioletRed
Definition: YiColor.h:633
const CYIColor Bisque
Definition: YiColor.h:669
const CYIColor LavenderBlush
Definition: YiColor.h:775