JugiMap Framework
jmColor.h
1 #ifndef JUGIMAP_COLOR_H
2 #define JUGIMAP_COLOR_H
3 
4 #include <string>
5 #include <vector>
6 
7 
8 namespace jugimap {
9 
10 
13 
15 struct ColorRGBA
16 {
17  unsigned char r = 255;
18  unsigned char g = 255;
19  unsigned char b = 255;
20  unsigned char a = 255;
21 
22 
25 
27  ColorRGBA(unsigned char _r, unsigned char _g, unsigned char _b, unsigned char _a=255) : r(_r),g(_g),b(_b),a(_a){}
28 
30  ColorRGBA(int _rgba)
31  {
32  r = _rgba & 0xFF;
33  g = (_rgba >> 8) & 0xFF;
34  b = (_rgba >> 16) & 0xFF;
35  a = (_rgba >> 24) & 0xFF;
36  }
37 
38 
40  bool operator ==(ColorRGBA _colorRGBA)
41  {
42  return (r==_colorRGBA.r && g==_colorRGBA.g && b==_colorRGBA.b && a==_colorRGBA.a)? true : false;
43  }
44 
46  bool operator !=(ColorRGBA _ColorRGBA)
47  {
48  return (r==_ColorRGBA.r && g==_ColorRGBA.g && b==_ColorRGBA.b && a==_ColorRGBA.a)? false : true;
49  }
50 
51 
53  int GetRGBA()
54  {
55  return r | (g << 8) | (b << 16) | (a << 24);
56  }
57 
59  void Set(unsigned char _r, unsigned char _g, unsigned char _b, unsigned char _a)
60  {
61  r = _r;
62  g = _g;
63  b = _b;
64  a = _a;
65  }
66 
67 
69  void Set(int _rgba)
70  {
71  r = _rgba & 0xFF;
72  g = (_rgba >> 8) & 0xFF;
73  b = (_rgba >> 16) & 0xFF;
74  a = (_rgba >> 24) & 0xFF;
75  }
76 
77  static ColorRGBA ParseFromHexString(std::string _hexColor);
78 
79 };
80 
81 
83 struct ColorRGBAf
84 {
85  float r = 1.0;
86  float g = 1.0;
87  float b = 1.0;
88  float a = 1.0;
89 
90 
93 
95  ColorRGBAf(float _r, float _g, float _b, float _a=1.0) : r(_r), g(_g), b(_b), a(_a){}
96 
98  ColorRGBAf(const ColorRGBA &_colorRGBA): r(_colorRGBA.r/255.0), g(_colorRGBA.g/255.0), b(_colorRGBA.b/255.0), a(_colorRGBA.a/255.0){}
99 
100 
101  bool operator ==(ColorRGBAf _colorRGBA);
102  bool operator !=(ColorRGBAf _colorRGBA) { return !(operator ==(_colorRGBA)); }
103 
104 };
105 
106 
109 {
110 public:
111 
113  int Add(ColorRGBA _colorRGBA, const std::string &_name){ colors.push_back({_name, _colorRGBA}); return colors.size()-1; }
114 
115 
117  void Clear(){ colors.clear(); }
118 
119 
121  ColorRGBA Find(const std::string &_name);
122 
123 
125  ColorRGBA At(int _index);
126 
127 
129  int GetSize(){ return colors.size(); }
130 
131 
132 private:
133 
134  std::vector<std::pair<std::string, ColorRGBA>> colors;
135 
136 };
137 
138 
140 extern ColorsLibrary textColorsLibrary;
141 
142 
144 
145 
146 }
147 
148 
149 #endif
jugimap::ColorsLibrary
The ColorsLibrary defines storage for ColorRGBA objects.
Definition: jmColor.h:108
jugimap::ColorRGBA::Set
void Set(unsigned char _r, unsigned char _g, unsigned char _b, unsigned char _a)
Set the given **_r**, **_g**, **_b** and **_a**.
Definition: jmColor.h:59
jugimap::ColorRGBA
Color in RGBA color space - integer variant.
Definition: jmColor.h:15
jugimap::ColorRGBAf::ColorRGBAf
ColorRGBAf(const ColorRGBA &_colorRGBA)
Constructs a color from the given **_colorRGBA**.
Definition: jmColor.h:98
jugimap::ColorRGBAf::r
float r
The red component of the color in the range 0.0 - 1.0.
Definition: jmColor.h:85
jugimap::ColorRGBA::g
unsigned char g
Green component of the color in the range 0 - 255.
Definition: jmColor.h:18
jugimap::ColorRGBA::ColorRGBA
ColorRGBA(unsigned char _r, unsigned char _g, unsigned char _b, unsigned char _a=255)
Constructs a color with the given **_r**, **_g**, **_b** and **_a**.
Definition: jmColor.h:27
jugimap::ColorRGBAf::b
float b
The blue component of the color in the range 0.0 - 1.0.
Definition: jmColor.h:87
jugimap::ColorsLibrary::Clear
void Clear()
Clear all colors in this library.
Definition: jmColor.h:117
jugimap::ColorsLibrary::Find
ColorRGBA Find(const std::string &_name)
Find and returns a color with the given *_name*. If the color is not found the function returns white...
Definition: jmColor.cpp:55
jugimap::ColorRGBA::ColorRGBA
ColorRGBA(int _rgba)
Constructs a color from the given **_rgba**.
Definition: jmColor.h:30
jugimap::ColorsLibrary::GetSize
int GetSize()
Returns the number of stored colors.
Definition: jmColor.h:129
jugimap::ColorRGBA::operator==
bool operator==(ColorRGBA _colorRGBA)
Returns true if the given **_colorRGBA** is equal to this color; otherwise return false;.
Definition: jmColor.h:40
jugimap::ColorRGBA::Set
void Set(int _rgba)
Set the color from the given **_rgba**.
Definition: jmColor.h:69
jugimap::ColorRGBAf::ColorRGBAf
ColorRGBAf(float _r, float _g, float _b, float _a=1.0)
Constructs a color with the given **_r**, **_g**, **_b** and **_a**.
Definition: jmColor.h:95
jugimap::ColorRGBAf::a
float a
The alpha component of the color in the range 0.0 - 1.0.
Definition: jmColor.h:88
jugimap::ColorRGBA::ColorRGBA
ColorRGBA()
Constructs a color with r=0, g=0, b=0 and a=0.
Definition: jmColor.h:24
jugimap::ColorRGBA::b
unsigned char b
Red component of the color in the range 0 - 255.
Definition: jmColor.h:19
jugimap::ColorsLibrary::At
ColorRGBA At(int _index)
Returns the color at the given index. If the index is out of storage bounds the white color is return...
Definition: jmColor.cpp:68
jugimap::ColorRGBAf::g
float g
The green component of the color in the range 0.0 - 1.0.
Definition: jmColor.h:86
jugimap::ColorRGBAf
Color in RGBA color space - float variant.
Definition: jmColor.h:83
jugimap::ColorRGBA::r
unsigned char r
Red component of the color in the range 0 - 255.
Definition: jmColor.h:17
jugimap::ColorRGBAf::ColorRGBAf
ColorRGBAf()
Constructs a color with r=0.0, g=0.0, b=0.0 and a=0.0.
Definition: jmColor.h:92
jugimap::ColorRGBA::GetRGBA
int GetRGBA()
Returns this color as an rgba integer value.
Definition: jmColor.h:53
jugimap::ColorRGBA::a
unsigned char a
Alpha component of the color in the range 0 - 255.
Definition: jmColor.h:20
jugimap::textColorsLibrary
ColorsLibrary textColorsLibrary
A global library for text colors.
Definition: jmColor.cpp:81
jugimap::ColorRGBA::operator!=
bool operator!=(ColorRGBA _ColorRGBA)
Returns true if the given **_colorRGBA** is not equal to this color; otherwise return false;.
Definition: jmColor.h:46
jugimap::ColorsLibrary::Add
int Add(ColorRGBA _colorRGBA, const std::string &_name)
Add the given color *_colorRGBA* with identification name *_name* to this library....
Definition: jmColor.h:113