You.i Engine
YiColor.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_COLOR_H_
3 #define _YI_COLOR_H_
4 
5 #include "framework/YiPredef.h"
6 #include "utility/YiString.h"
7 #include "utility/YiTypeTraits.h"
8 
9 #include <glm/vec4.hpp>
10 
11 #include <ostream>
12 
13 class CYINamedColors;
14 
31 class CYIColor
32 {
33  friend bool operator==(const CYIColor &lhs, const CYIColor &rhs);
34  friend bool operator!=(const CYIColor &lhs, const CYIColor &rhs);
35  friend bool operator<(const CYIColor &lhs, const CYIColor &rhs);
36  friend bool operator<=(const CYIColor &lhs, const CYIColor &rhs);
37  friend bool operator>(const CYIColor &lhs, const CYIColor &rhs);
38  friend bool operator>=(const CYIColor &lhs, const CYIColor &rhs);
39 
40 public:
44  enum class ByteOrder
45  {
47  RGBA = 0,
49  ARGB,
51  BGRA,
53  ABGR
54  };
55 
61  CYIColor();
62 
68  CYIColor(const CYIColor &color, float alpha);
69 
81  CYIColor(double red, double green, double blue, double alpha = 1.0);
82 
90  CYIColor(float red, float green, float blue, float alpha = 1.0f);
91 
98  CYIColor(const glm::vec3 &rgb);
99 
105  CYIColor(const glm::vec4 &rgba);
106 
114  explicit CYIColor(uint32_t packed, ByteOrder byteOrder = ByteOrder::ABGR);
115 
121  explicit CYIColor(CYIStringView name);
122 
127  CYIColor &operator=(const CYIColor &color);
128 
135  CYIColor &operator=(const glm::vec3 &rgb);
136 
142  CYIColor &operator=(const glm::vec4 &rgba);
143 
148  void SetColor(const CYIColor &color);
149 
157  void SetColor(float red, float green, float blue, float alpha = 1.0f);
158 
166  void SetColorInteger(int32_t red, int32_t green, int32_t blue, int32_t alpha = 255);
167 
174  void SetColor(const glm::vec3 &rgb);
175 
181  void SetColor(const glm::vec4 &rgba);
182 
190  void SetColor(uint32_t packed, ByteOrder byteOrder = ByteOrder::ABGR);
191 
199  void SetRed(double red);
200 
204  void SetRed(float red);
205 
209  void SetRedInteger(int32_t red);
210 
218  void SetGreen(double green);
219 
223  void SetGreen(float green);
224 
228  void SetGreenInteger(int32_t green);
229 
237  void SetBlue(double blue);
238 
242  void SetBlue(float blue);
243 
247  void SetBlueInteger(int32_t blue);
248 
256  void SetAlpha(double alpha);
257 
261  void SetAlpha(float alpha);
262 
266  void SetAlphaInteger(int32_t alpha);
267 
271  void SetHSLHue(float hue);
272 
276  void SetHSLHueInteger(int32_t hue);
277 
281  void SetHSLSaturation(float saturation);
282 
286  void SetHSLSaturationInteger(int32_t saturation);
287 
291  void SetHSLLightness(float lightness);
292 
296  void SetHSLLightnessInteger(int32_t lightness);
297 
301  float GetRed() const;
302 
306  float GetGreen() const;
307 
311  float GetBlue() const;
312 
316  float GetAlpha() const;
317 
323  float GetHSLHue() const;
324 
330  float GetHSLSaturation() const;
331 
337  float GetHSLLightness() const;
338 
342  bool IsVisible() const;
343 
347  uint8_t GetRedByte() const;
348 
352  uint8_t GetGreenByte() const;
353 
357  uint8_t GetBlueByte() const;
358 
362  uint8_t GetAlphaByte() const;
363 
370  uint32_t GetPackedColor(ByteOrder byteOrder = ByteOrder::ABGR) const;
371 
375  CYIColor GetInverted() const;
376 
382  CYIColor GetPremultiplied() const;
383 
387  CYIColor GetClamped() const;
388 
394  CYIString ToHexString(ByteOrder byteOrder = ByteOrder::RGBA) const;
395 
399  const glm::vec4 &GetColorVector() const;
400 
408  glm::vec4 GetHSLAColorVector() const;
409 
416  static const CYINamedColors &Named();
417 
418  /*
419  \copydox #Named()
420  */
421  static const CYINamedColors &GetNamedColors();
422 
428  static CYIColor GetColorByName(CYIStringView colorName, bool *pError = nullptr);
429 
457  static CYIColor FromString(CYIStringView colorName, bool *pError = nullptr);
458 
462  static CYIColor FromColor(const CYIColor &color, float alpha);
463 
467  static CYIColor FromRGB(float red, float green, float blue, float alpha = 1.0f);
468 
472  static CYIColor FromRGBInteger(int32_t red, int32_t green, int32_t blue, int32_t alpha = 255);
473 
477  static CYIColor FromRGB(const glm::vec3 &rgbVector);
478 
482  static CYIColor FromRGBA(const glm::vec4 &rgbaVector);
483 
489  static CYIColor FromPackedRGBA(uint32_t packed, ByteOrder byteOrder = ByteOrder::ABGR);
490 
496  static CYIColor FromHSL(float hue, float saturation, float luminance, float alpha = 1.0f);
497 
505  static CYIColor FromHSL(const glm::vec3 &hslVector);
506 
514  static CYIColor FromHSLA(const glm::vec4 &hslaVector);
515 
519  static inline uint32_t GetPackedColor(uint8_t red, uint32_t green, uint32_t blue, uint32_t alpha, ByteOrder byteOrder = ByteOrder::ABGR);
520 
521  // The following functions have been removed (but that still need to be present in this class to avoid runtime errors)
522 
526  template<typename T = bool>
527  CYIColor(int32_t r, int32_t g, int32_t b, int32_t a = 255);
528 
532  template<typename T = bool>
533  CYIColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
534 
538  template<typename T = bool>
539  void SetColor(int32_t r, int32_t g, int32_t b, int32_t a = 255);
540 
544  template<typename T = bool>
545  void SetRed(int32_t r);
546 
550  template<typename T = bool>
551  void SetRed(uint8_t r);
552 
556  template<typename T = bool>
557  void SetGreen(int32_t g);
558 
562  template<typename T = bool>
563  void SetGreen(uint8_t g);
564 
568  template<typename T = bool>
569  void SetBlue(int32_t b);
570 
574  template<typename T = bool>
575  void SetBlue(uint8_t b);
576 
580  template<typename T = bool>
581  void SetAlpha(int32_t a);
582 
586  template<typename T = bool>
587  void SetAlpha(uint8_t a);
588 
589 private:
590  glm::vec4 m_rgbaData;
591 };
592 
602 {
603 public:
604  // Transparent color
606 
607  // Pink colors
608  const CYIColor Pink;
614 
615  // Red colors
624  const CYIColor Red;
625 
626  // Orange colors
632 
633  // Yellow colors
644  const CYIColor Gold;
645 
646  // Brown colors
653  const CYIColor Tan;
658  const CYIColor Peru;
664 
665  // Green colors
671  const CYIColor Lime;
685 
686  // Cyan colors
688  const CYIColor Aqua;
689  const CYIColor Cyan;
699  const CYIColor Teal;
700 
701  // Blue colors
712  const CYIColor Blue;
715  const CYIColor Navy;
717 
718  // Purple/Violet/Magenta colors
721  const CYIColor Plum;
738 
739  // White colors
741  const CYIColor Snow;
757 
758  // Gray/Black colors
763  const CYIColor Gray;
769 
770 private:
771  friend class CYIColor;
772  CYINamedColors();
773 };
774 
775 bool operator==(const CYIColor &lhs, const CYIColor &rhs);
776 bool operator!=(const CYIColor &lhs, const CYIColor &rhs);
777 bool operator<(const CYIColor &lhs, const CYIColor &rhs);
778 
781 #include "utility/YiColor.inl"
782 
783 #endif // _YI_COLOR_H_
friend bool operator>=(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor Plum
Definition: YiColor.h:721
const CYIColor Thistle
Definition: YiColor.h:720
const CYIColor MediumSeaGreen
Definition: YiColor.h:680
friend bool operator==(const CYIColor &lhs, const CYIColor &rhs)
void SetRed(double red)
const CYIColor PowderBlue
Definition: YiColor.h:703
const CYIColor Wheat
Definition: YiColor.h:651
float GetAlpha() const
const CYIColor DarkGray
Definition: YiColor.h:762
const CYIColor Red
Definition: YiColor.h:624
const CYIColor DarkOrchid
Definition: YiColor.h:730
const CYIColor DarkMagenta
Definition: YiColor.h:731
void SetBlueInteger(int32_t blue)
static const CYINamedColors & GetNamedColors()
Definition: YiColor.h:601
const CYIColor White
Definition: YiColor.h:740
const CYIColor SlateGray
Definition: YiColor.h:766
const CYIColor YellowGreen
Definition: YiColor.h:669
const CYIColor BlanchedAlmond
Definition: YiColor.h:648
const CYIColor Silver
Definition: YiColor.h:761
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:36
const CYIColor Tomato
Definition: YiColor.h:628
const CYIColor LightSeaGreen
Definition: YiColor.h:696
const CYIColor Chocolate
Definition: YiColor.h:659
void SetHSLLightness(float lightness)
const CYIColor CornflowerBlue
Definition: YiColor.h:709
const CYIColor DarkSalmon
Definition: YiColor.h:618
const CYIColor CadetBlue
Definition: YiColor.h:697
const CYIColor Lavender
Definition: YiColor.h:719
const CYIColor DarkTurquoise
Definition: YiColor.h:695
const CYIColor IndianRed
Definition: YiColor.h:620
void SetGreenInteger(int32_t green)
bool IsVisible() const
const CYIColor LightSlateGray
Definition: YiColor.h:765
void SetHSLLightnessInteger(int32_t lightness)
float GetHSLSaturation() const
static CYIColor FromRGBA(const glm::vec4 &rgbaVector)
const CYIColor SandyBrown
Definition: YiColor.h:655
const CYIColor ForestGreen
Definition: YiColor.h:682
const CYIColor LightGreen
Definition: YiColor.h:677
const CYIColor MediumAquamarine
Definition: YiColor.h:687
ByteOrder
Definition: YiColor.h:44
const CYIColor FireBrick
Definition: YiColor.h:622
const CYIColor WhiteSmoke
Definition: YiColor.h:747
const CYIColor DarkCyan
Definition: YiColor.h:698
const CYIColor OldLace
Definition: YiColor.h:750
void SetHSLHue(float hue)
const CYIColor Khaki
Definition: YiColor.h:642
const CYIColor DarkViolet
Definition: YiColor.h:729
void SetRedInteger(int32_t red)
const CYIColor Honeydew
Definition: YiColor.h:742
const CYIColor GreenYellow
Definition: YiColor.h:674
const CYIColor Tan
Definition: YiColor.h:653
const CYIColor MediumPurple
Definition: YiColor.h:727
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:712
void SetAlpha(double alpha)
const glm::vec4 & GetColorVector() const
CYIColor GetInverted() const
const CYIColor MediumSlateBlue
Definition: YiColor.h:737
const CYIColor DarkRed
Definition: YiColor.h:623
const CYIColor PaleGoldenrod
Definition: YiColor.h:641
const CYIColor NavajoWhite
Definition: YiColor.h:650
static CYIColor GetColorByName(CYIStringView colorName, bool *pError=nullptr)
const CYIColor RosyBrown
Definition: YiColor.h:654
const CYIColor MistyRose
Definition: YiColor.h:756
const CYIColor Black
Definition: YiColor.h:768
const CYIColor DarkBlue
Definition: YiColor.h:714
const CYIColor Orange
Definition: YiColor.h:631
const CYIColor MintCream
Definition: YiColor.h:743
uint8_t GetRedByte() const
const CYIColor Beige
Definition: YiColor.h:749
const CYIColor Violet
Definition: YiColor.h:722
const CYIColor SeaGreen
Definition: YiColor.h:681
void SetHSLHueInteger(int32_t hue)
void SetAlphaInteger(int32_t alpha)
const CYIColor LightSalmon
Definition: YiColor.h:616
const CYIColor AntiqueWhite
Definition: YiColor.h:753
void SetGreen(double green)
const CYIColor Lime
Definition: YiColor.h:671
const CYIColor LightSkyBlue
Definition: YiColor.h:706
uint8_t GetAlphaByte() const
const CYIColor MediumTurquoise
Definition: YiColor.h:694
const CYIColor Olive
Definition: YiColor.h:667
CYIColor GetClamped() const
const CYIColor SteelBlue
Definition: YiColor.h:710
const CYIColor Gray
Definition: YiColor.h:763
const CYIColor DarkOliveGreen
Definition: YiColor.h:666
float GetHSLHue() const
const CYIColor Cornsilk
Definition: YiColor.h:647
const CYIColor LightSteelBlue
Definition: YiColor.h:702
const CYIColor BurlyWood
Definition: YiColor.h:652
uint32_t GetPackedColor(ByteOrder byteOrder=ByteOrder::ABGR) const
const CYIColor Orchid
Definition: YiColor.h:723
const CYIColor DarkSlateGray
Definition: YiColor.h:767
const CYIColor Gainsboro
Definition: YiColor.h:759
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:732
const CYIColor Chartreuse
Definition: YiColor.h:673
CYIColor & operator=(const CYIColor &color)
Copy assignment operator.
const CYIColor Yellow
Definition: YiColor.h:634
const CYIColor Coral
Definition: YiColor.h:629
const CYIColor OliveDrab
Definition: YiColor.h:668
friend bool operator<=(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor Ivory
Definition: YiColor.h:752
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:733
const CYIColor MediumSpringGreen
Definition: YiColor.h:676
const CYIColor Maroon
Definition: YiColor.h:663
const CYIColor HotPink
Definition: YiColor.h:610
CYIColor GetPremultiplied() const
const CYIColor SaddleBrown
Definition: YiColor.h:660
const CYIColor Crimson
Definition: YiColor.h:621
const CYIColor DarkGreen
Definition: YiColor.h:684
const CYIColor Aqua
Definition: YiColor.h:688
CYIColor()
Default Constructor.
const CYIColor Brown
Definition: YiColor.h:662
const CYIColor Moccasin
Definition: YiColor.h:639
float GetRed() const
const CYIColor LightGrey
Definition: YiColor.h:760
float GetHSLLightness() const
const CYIColor PapayaWhip
Definition: YiColor.h:638
friend bool operator>(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor DodgerBlue
Definition: YiColor.h:708
const CYIColor PaleTurquoise
Definition: YiColor.h:691
const CYIColor PeachPuff
Definition: YiColor.h:640
const CYIColor MediumBlue
Definition: YiColor.h:713
friend bool operator!=(const CYIColor &lhs, const CYIColor &rhs)
const CYIColor Navy
Definition: YiColor.h:715
const CYIColor DeepPink
Definition: YiColor.h:611
const CYIColor DarkOrange
Definition: YiColor.h:630
const CYIColor Fuchsia
Definition: YiColor.h:724
const CYIColor DimGray
Definition: YiColor.h:764
uint8_t GetBlueByte() const
const CYIColor LawnGreen
Definition: YiColor.h:672
const CYIColor Pink
Definition: YiColor.h:608
const CYIColor Green
Definition: YiColor.h:683
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:704
const CYIColor Azure
Definition: YiColor.h:744
const CYIColor LightYellow
Definition: YiColor.h:635
const CYIColor Cyan
Definition: YiColor.h:689
const CYIColor DarkKhaki
Definition: YiColor.h:643
const CYIColor LemonChiffon
Definition: YiColor.h:636
CYIString ToHexString(ByteOrder byteOrder=ByteOrder::RGBA) const
const CYIColor Aquamarine
Definition: YiColor.h:692
const CYIColor BlueViolet
Definition: YiColor.h:728
const CYIColor SlateBlue
Definition: YiColor.h:736
const CYIColor Sienna
Definition: YiColor.h:661
static CYIColor FromPackedRGBA(uint32_t packed, ByteOrder byteOrder=ByteOrder::ABGR)
const CYIColor Teal
Definition: YiColor.h:699
const CYIColor RebeccaPurple
Definition: YiColor.h:735
const CYIColor Seashell
Definition: YiColor.h:748
void SetColor(const CYIColor &color)
Sets the color to the same color of another RGB instance.
const CYIColor FloralWhite
Definition: YiColor.h:751
const CYIColor PaleGreen
Definition: YiColor.h:678
const CYIColor MidnightBlue
Definition: YiColor.h:716
const CYIColor SpringGreen
Definition: YiColor.h:675
const CYIColor Salmon
Definition: YiColor.h:617
const CYIColor RoyalBlue
Definition: YiColor.h:711
const CYIColor MediumOrchid
Definition: YiColor.h:726
const CYIColor LimeGreen
Definition: YiColor.h:670
void SetHSLSaturationInteger(int32_t saturation)
const CYIColor Linen
Definition: YiColor.h:754
A class used to represent a color value.
Definition: YiColor.h:31
const CYIColor DarkGoldenrod
Definition: YiColor.h:657
const CYIColor Magenta
Definition: YiColor.h:725
const CYIColor Gold
Definition: YiColor.h:644
const CYIColor AliceBlue
Definition: YiColor.h:745
const CYIColor LightCoral
Definition: YiColor.h:619
glm::vec4 GetHSLAColorVector() const
const CYIColor DeepSkyBlue
Definition: YiColor.h:707
const CYIColor Turquoise
Definition: YiColor.h:693
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:679
const CYIColor Peru
Definition: YiColor.h:658
float GetBlue() const
const CYIColor Transparent
Definition: YiColor.h:605
const CYIColor PaleVioletRed
Definition: YiColor.h:612
const CYIColor DarkSlateBlue
Definition: YiColor.h:734
const CYIColor LightCyan
Definition: YiColor.h:690
const CYIColor Snow
Definition: YiColor.h:741
const CYIColor SkyBlue
Definition: YiColor.h:705
const CYIColor LightGoldenrodYellow
Definition: YiColor.h:637
void SetHSLSaturation(float saturation)
const CYIColor Goldenrod
Definition: YiColor.h:656
const CYIColor OrangeRed
Definition: YiColor.h:627
const CYIColor LightPink
Definition: YiColor.h:609
const CYIColor GhostWhite
Definition: YiColor.h:746
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:57
const CYIColor MediumVioletRed
Definition: YiColor.h:613
const CYIColor Bisque
Definition: YiColor.h:649
const CYIColor LavenderBlush
Definition: YiColor.h:755