ubit
ucolor.hpp
1 /* ***********************************************************************
2  *
3  * ucolor.hpp: Color Attribute
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 #ifndef _ucolor_hpp_
17 #define _ucolor_hpp_ 1
18 #include <string.h>
19 #include <ubit/uattr.hpp>
20 namespace ubit {
21 
24  class URgba {
25  public:
26  URgba() {comps[0] = comps[1] = comps[2] = 0; comps[3] = 255u;}
27 
28  URgba(unsigned int r, unsigned int g, unsigned int b, unsigned int a = 255u)
29  {comps[0] = r; comps[1] = g; comps[2] = b; comps[3] = a;}
30 
31  URgba(float r, float g, float b, float a = 1.0f) {setRgbaF(r,g,b,a);}
32 
33  void setRgbaI(unsigned int r, unsigned int g, unsigned int b, unsigned int a = 255u)
34  {comps[0] = r; comps[1] = g; comps[2] = b; comps[3] = a;}
35 
36  void setRgbaF(float r, float g, float b, float a = 1.0f);
37 
38  bool operator==(const URgba& c) const {
39  return (comps[0]==c.comps[0] && comps[1]==c.comps[1]
40  && comps[2]==c.comps[2] && comps[3]==c.comps[3]);
41  }
42 
43  //bool equals(unsigned int r, unsigned int g, unsigned int b, unsigned int a) const {
44  //return components[0]==r && components[1]==g && components[2]==b && components[3]==a;}
45 
46  //bool equals(float r, float g, float b, float a) const;
47 
48  unsigned int getRedI() const {return comps[0];}
49  unsigned int getGreenI() const {return comps[1];}
50  unsigned int getBlueI() const {return comps[2];}
51  unsigned int getAlphaI() const {return comps[3];}
52 
53  void setRedI(unsigned int c) {comps[0] = c;}
54  void setGreenI(unsigned int c) {comps[1] = c;}
55  void setBlueI(unsigned int c) {comps[2] = c;}
56  void setAlphaI(unsigned int c) {comps[3] = c;}
57 
58  unsigned char comps[4]; // must be compatible with GLubyte
59  };
60 
61 
73  class UColor : public UAttr {
74  public:
75  UCLASS(UColor)
76 
77  static UColor none, inherit, white, black, grey, lightgrey, darkgrey,
78  navy, lightblue, blue, red, green, yellow, orange, wheat, teal, disabled;
84  UColor();
86 
87  UColor(const UColor& color);
89 
90  UColor(const UColor& color, float a);
92 
93  UColor(const UColor& color, unsigned int a);
95 
96  UColor(const URgba&);
98 
99  UColor(float r, float g, float b, float a = 1.0f);
105  UColor(unsigned int r, unsigned int g, unsigned int b, unsigned int a = 255u);
111  UColor(const UStr& color_name, float a = 1.);
113 
114  ~UColor();
115 
116  UColor& operator=(const UColor& c);
118 
119  UColor& setRgba(const URgba&);
121 
122  UColor& setRgbaF(float r, float g, float b, float a = 1.0f);
124 
125  UColor& setRgbaI(unsigned int r, unsigned int g, unsigned int b, unsigned int a=255u);
127 
128  UColor& setNamedColor(const UStr& color_name, float a = 1.);
137  const URgba& getRgba() const {return rgba;}
138 
139  bool operator==(const UColor& c) const {return equals(c);}
140  bool operator!=(const UColor& c) const {return !equals(c);}
141 
142  virtual bool equals(const UColor&) const;
143  virtual bool equals(const URgba&) const;
144  virtual bool equals(float r, float g, float b, float a) const;
145  virtual bool equals(unsigned int r, unsigned int g, unsigned int b, unsigned int a) const;
146 
147  virtual void update(); // redefinition
148 
149  virtual void putProp(UUpdateContext*, UElem&); // redefinition
150 
151  static void addNamedColor(const char* name, const URgba&);
153 
154  static bool parseColor(const char* name, URgba&);
156 
157  // - - - impl - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158 #ifndef NO_DOC
159  protected:
160  friend class UGraph;
161  URgba rgba;
162 #if WITH_2D_GRAPHICS
163  mutable std::vector<unsigned long> pixels;
164  void unsetPixels();
165  unsigned long getPixel(UDisp*) const;
166 #endif
167  UColor(unsigned int r, unsigned int g, unsigned int b, unsigned int a, UConst);
168  //[Impl] creates a new CONSTANT color with a RGB value; @see UObject::UCONST.
169 
170  UColor(unsigned char special, UConst);
171  //[Impl] creates a new CONSTANT color with a special value; @see UObject::UCONST.
172 
173 #endif
174  };
175 
176 
177  inline UColor& ucolor() {return *new UColor();}
179 
180  inline UColor& ucolor(float r, float g, float b, float a = 1.)
181  {return *new UColor(r,g,b,a);}
183 
184  inline UColor& ucolor(unsigned int r, unsigned int g, unsigned int b, unsigned int a = 255)
185  {return *new UColor(r,g,b,a);}
187 
188  inline UColor& ucolor(const UStr& color_name, float alpha = 1.)
189  {return *new UColor(color_name, alpha);}
191 
192 }
193 #endif
Base class for attributes.
Definition: uattr.hpp:97
Definition: uobject.hpp:282
class for drawing on widgets.
Definition: ugraph.hpp:44
Display Context.
Definition: udisp.hpp:44
Color attribute of an element or a widget.
Definition: ucolor.hpp:73
Specifies a RGBA value for defining UColor(s).
Definition: ucolor.hpp:24
lightweight general purpose container.
Definition: uelem.hpp:44
Definition: uupdatecontext.hpp:32
Definition: uhardfont.hpp:31
Ubit String.
Definition: ustr.hpp:72