SGL
gcolor.h
1 /*
2  * File: gcolor.h
3  * --------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/09
7  * - added sgl namespace
8  * @version 2021/04/03
9  * - removed dependency on custom collections
10  * @version 2019/05/05
11  * - added getLuminance
12  * @version 2018/09/16
13  * - added splitRGB/ARGB, hasAlpha; better ARGB support
14  * @version 2018/09/07
15  * - added doc comments for new documentation generation
16  * @version 2018/08/23
17  * - renamed to gcolor.h to replace Java version
18  * @version 2018/06/30
19  * - initial version
20  */
21 
22 
23 #ifndef _gcolor_h
24 #define _gcolor_h
25 
26 #include <map>
27 #include <string>
28 #include <QColor>
29 
30 namespace sgl {
31 
74 class GColor {
75 public:
79  enum {
80  BLACK = 0x000000,
81  BLUE = 0x0000FF,
82  BROWN = 0x926239,
83  CYAN = 0x00FFFF,
84  DARKGRAY = 0x595959,
85  GRAY = 0x999999,
86  GREEN = 0x00FF00,
87  LIGHTGRAY = 0xBFBFBF,
88  MAGENTA = 0xFF00FF,
89  ORANGE = 0xFFC800,
90  PINK = 0xFFAFAF,
91  PURPLE = 0xFF00FF,
92  RED = 0xFF0000,
93  WHITE = 0xFFFFFF,
94  YELLOW = 0xFFFF00
95  } Color;
96 
104  static string convertARGBToColor(int a, int r, int g, int b);
105 
110  static string convertARGBToColor(int argb);
111 
119  static int convertARGBToARGB(int a, int r, int g, int b);
120 
125  static int convertColorToARGB(const string& colorName);
126 
132  static int convertColorToRGB(const string& colorName);
133 
138  static string convertQColorToColor(const QColor& color);
139 
144  static int convertQColorToRGB(const QColor& color);
145 
151  static string convertRGBToColor(int rgb);
152 
160  static string convertRGBToColor(int r, int g, int b);
161 
168  static int convertRGBToRGB(int r, int g, int b);
169 
175  static int fixAlpha(int argb);
176 
183  static double getLuminance(int rgb);
184 
191  static double getLuminance(const string& color);
192 
198  static bool hasAlpha(const string& color);
199 
206  static void splitARGB(int argb, int& a, int& r, int& g, int& b);
207 
214  static void splitRGB(int rgb, int& r, int& g, int& b);
215 
220  static QColor toQColor(const string& color);
221 
226  static QColor toQColorARGB(int argb);
227 
228 private:
229  GColor(); // forbid construction
230 
234  static string canonicalColorName(const string& str);
235 
240  static const std::map<string, int>& colorTable();
241 
246  static const std::map<string, string>& colorNameTable();
247 
248  // internal color tables
249  static std::map<string, int> _colorTable;
250  static std::map<string, string> _colorNameTable;
251 };
252 
253 } // namespace sgl
254 
255 #endif // _gcolor_h
static int convertARGBToARGB(int a, int r, int g, int b)
Converts four integer RGB values from 0-255 into an ARGB integer of the form 0xaarrggbb.
Definition: gcolor.cpp:103
static bool hasAlpha(string color)
Returns true if the given color string is of the 8-hex-character form that contains an alpha channel ...
Definition: gcolor.cpp:214
static int fixAlpha(int argb)
Sets the &#39;alpha&#39; (high order bits) of the given integer to ff.
Definition: gcolor.cpp:195
static void splitRGB(int rgb, int &r, int &g, int &b)
Splits the given RGB integer into three integer RGB values from 0-255.
Definition: gcolor.cpp:226
Definition: gcolor.h:87
Definition: gcolor.h:90
This class provides static methods for dealing with colors.
Definition: gcolor.h:74
static int convertRGBToRGB(int r, int g, int b)
Converts three integer RGB values from 0-255 into a single RGB integer.
Definition: gcolor.cpp:191
Definition: gcolor.h:93
Definition: gcolor.h:88
static QColor toQColor(string color)
Converts a color string into a Qt color object.
Definition: gcolor.cpp:232
Definition: gcolor.h:83
Definition: console.h:45
Definition: gcolor.h:85
static int convertQColorToRGB(const QColor &color)
Converts a Qt color object into an RGB integer.
Definition: gcolor.cpp:154
Definition: gcolor.h:84
static int convertColorToARGB(string colorName)
Converts a color name into an ARGB integer that encodes the alpha (opacity), red, green...
Definition: gcolor.cpp:126
static string convertQColorToColor(const QColor &color)
Converts a Qt RGB color object into a color string.
Definition: gcolor.cpp:150
static string convertRGBToColor(int rgb)
Converts an RGB integer value into a color name in the form "#rrggbb".
Definition: gcolor.cpp:158
Definition: gcolor.h:82
static double getLuminance(int rgb)
Returns the photometric luminance of the given RGB integer, which is a measure of how bright the colo...
Definition: gcolor.cpp:203
Definition: gcolor.h:92
Definition: gcolor.h:94
Definition: gcolor.h:81
enum sgl::GColor::@0 Color
Constants representing common system color names.
Definition: gcolor.h:91
Definition: gcolor.h:80
static int convertColorToRGB(string colorName)
Converts a color name into an integer that encodes the red, green, and blue components of the color...
Definition: gcolor.cpp:130
static QColor toQColorARGB(int argb)
Converts an ARGB integer into a Qt color object.
Definition: gcolor.cpp:244
Definition: gcolor.h:86
static string convertARGBToColor(int a, int r, int g, int b)
Converts four integer RGB values from 0-255 into a color name in the form "#aarrggbb".
Definition: gcolor.cpp:107
Definition: gcolor.h:89
static void splitARGB(int argb, int &a, int &r, int &g, int &b)
Splits the given ARGB integer into four integer RGB values from 0-255.
Definition: gcolor.cpp:219