Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_color.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_COLOR_H
7 #define TB_COLOR_H
8 
9 #include "tb_types.h"
10 #include "tb_str.h"
11 #include "tb_id.h"
12 #include <map>
13 
14 namespace tb {
15 
16 class TBNode;
17 class TBSkin;
18 
21 class TBColor
22 {
23 public:
24  TBColor() : b(0), g(0), r(0), a(255) {}
25  TBColor(uint8_t r_, uint8_t g_, uint8_t b_, uint8_t a_ = 255) : b(b_), g(g_), r(r_), a(a_) {}
26  TBColor(uint32_t bgra_) : bgra(bgra_) {}
27 
28  union {
29  struct { uint8_t b, g, r, a; };
30  uint32_t bgra;
31  };
32 
33  void Set(const TBColor &color) { *this = color; }
34 
37  void SetFromString(const TBStr & str);
38 
40  void GetString(TBStr & str) const;
41 
42  operator uint32_t () const { return bgra; }
43  //bool operator == (const TBColor &c) const { return bgra == (uint32_t)c; }
44  //bool operator != (const TBColor &c) const { return bgra != (uint32_t)c; }
45 };
46 
50 {
51 public:
52 
54  void Load(TBNode *n, TBSkin *skin);
55 
57  bool Define(const TBStr & cid, TBColor color);
58 
60  bool IsDefined(const TBStr & cid) { return 0 != _id2color.count(cid); }
61 
63  bool IsDefined(const TBColor & color) { return 0 != _color2id.count(color); }
64 
66  void ReDefine(const TBStr & cid, TBColor color);
67 
69  void Clear();
70 
74  TBColor GetColor(const TBStr & cid) const;
75 
79  TBStr GetColorID(const TBColor & color) const;
80 
84  const std::map<TBStr, TBColor> GetColorMap() const { return _id2color; }
85 
87  void Dump(const TBStr & filename);
88 
89 private:
90  std::map<TBStr, TBColor> _id2color;
91  std::map<TBColor, TBStr> _color2id;
92 };
93 
94 } // namespace tb
95 
96 #endif // TB_COLOR_H
bool Define(const TBStr &cid, TBColor color)
Define a color, if not already defined.
Definition: tb_color.cpp:56
void Load(TBNode *n, TBSkin *skin)
Load a list of colors from a node.
Definition: tb_color.cpp:45
TBSkin contains a list of TBSkinElement.
Definition: tb_skin.h:287
void ReDefine(const TBStr &cid, TBColor color)
(Re)Define a color, no matter what.
Definition: tb_color.cpp:67
void Clear()
Clear the list of colors.
Definition: tb_color.cpp:74
TBStr GetColorID(const TBColor &color) const
Return the id of the given color, or 0.
Definition: tb_color.cpp:87
TBColor GetColor(const TBStr &cid) const
Return the color with the given id.
Definition: tb_color.cpp:80
bool IsDefined(const TBStr &cid)
Is the color defined?
Definition: tb_color.h:60
bool IsDefined(const TBColor &color)
Is the color defined?
Definition: tb_color.h:63
TBColorManager contains a map of global color names.
Definition: tb_color.h:49
void SetFromString(const TBStr &str)
Set the color from string in any of the following formats: &quot;#rrggbbaa&quot;, &quot;#rrggbb&quot;, &quot;#rgba&quot;, &quot;#rgb&quot;.
Definition: tb_color.cpp:15
TBStr is a simple string class.
Definition: tb_str.h:62
TBNode is a tree node with a string name and a value (TBValue).
Definition: tb_node_tree.h:30
void Dump(const TBStr &filename)
Dump the current color map.
Definition: tb_color.cpp:94
TBColor contains a 32bit color.
Definition: tb_color.h:21
const std::map< TBStr, TBColor > GetColorMap() const
Return the cid of the given color, or 0.
Definition: tb_color.h:84
void GetString(TBStr &str) const
Write color to string with format #rrggbbaa.
Definition: tb_color.cpp:38