guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gcolor.hpp
1 //
2 // Color and Background Properties.
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_Color_hpp
9 #define Guit_Color_hpp
10 #include <gvarprop.hpp>
11 namespace guit {
12 
15 class GRgba {
16 public:
17  GRgba() : red{}, green{}, blue{}, alpha{1.f} {}
18  GRgba(float red, float green, float blue, float alpha = 1.f);
19  GRgba(GString const& colorname);
20 
21  void set(float red, float green, float blue, float alpha = 1.f);
22  bool set(GString const& colorname);
23 
25  static GRgba blend(const GRgba& color1, const GRgba& color2, float alpha);
26 
27  GString stringValue() const;
28 
29  bool operator==(const GRgba&) const;
30  bool operator!=(const GRgba&) const;
31 
32  friend std::ostream& operator<<(std::ostream& out, const GRgba&);
33  friend std::istream& operator>>(std::istream& in, GRgba&);
34 
35  float red, green, blue, alpha;
36 };
37 
38 inline bool gconvert(const GRgba& from, GRgba& to) {to = from; return true;}
39 bool gconvert(GString const& from, GRgba& to);
40 bool gconvert(GRgba const& from, GString& to);
41 
42 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43 
46 class GColor : public GVarProp_<GRgba> {
47 public:
48  using Super = GVarProp_<GRgba>;
49 
51  static GColor none, white, black, gray, lightgray, dimgray,
52  red, green, blue, lightblue, guitblue, cyan, magenta, yellow, orange;
53 
54  GColor();
55 
58  GColor(float red, float green, float blue, float alpha = 1.f);
59 
62  GColor(GRgba const& color);
63 
65  GColor(GColor const& color);
66 
68  GColor(GColor const& color, float alpha);
69 
76  GColor(GString const& value);
77 
78  GColor* clone(bool copy_value) const override;
79 
80  GColor& operator=(GRgba const& value);
81  GColor& operator=(GColor const& color);
82  GColor& operator=(GString const& value);
83 
85  void set(float red, float green, float blue, float alpha = 1.0f);
86 
87  void blend(const GColor& a, const GColor& b, float mix);
88  void blend(const GProp& a, const GProp& b, float mix) override;
89 
90  void setRed(float);
91  void setGreen(float);
92  void setBlue(float);
93  void setAlpha(float);
94 
95  float redValue() const {return value_.red;}
96  float blueValue() const {return value_.blue;}
97  float greenValue() const {return value_.green;}
98  float alphavalue() const {return value_.alpha;}
99 
101  GColor& operator<<=(class GTextExpr&);
102 
105  auto operator~() {return GPropRef_<GColor>(*this);}
106 
108  using Field = GNumPropField<GColor,float>;
109 
114  Field redField() {return Field(*this, value_.red);}
115  Field greenField() {return Field(*this, value_.green);}
116  Field blueField() {return Field(*this, value_.blue);}
117  Field alphaField() {return Field(*this, value_.alpha);}
119 
120  using GType = GPropType_<GColor,GProp>;
121  static GType& Type();
122  GType& type() const override {return Type();}
123 
126  bool isValid(GRgba const&) const override;
127  bool doChange(GRgba const&) override;
128  void apply(Gadget*, GRender&, Specif) override;
129  void updateGadget(Gadget&) override;
131 };
132 
135 inline GColor& Color() {return *new GColor();}
136 inline GColor& Color(float r, float g, float b, float a = 1.f) {return *new GColor(r,g,b,a);}
137 inline GColor& Color(GString const& value) {return *new GColor(value);}
139 
140 
141 using BackgroundAttr = GRgba;
142 
144 class GBackground : public GColor {
145 public:
146  using Super = GColor;
147 
149  static GBackground none, white, black, gray, lightgray, dimgray,
150  red, green, blue, lightblue, guitblue, cyan, magenta, yellow, orange;
151 
152  GBackground();
153  GBackground(float red, float green, float blue, float alpha = 1.f);
154  GBackground(GRgba const& color);
155  GBackground(GColor const& color);
156  GBackground(GColor const& color, float alpha);
157  GBackground(GString const& name);
158 
159  GBackground* clone(bool copy_value) const override;
160 
161  GBackground& operator=(GRgba const& value);
162  GBackground& operator=(GColor const& value);
163  GBackground& operator=(GString const& value);
164 
166  static GType& Type();
167  GType& type() const override {return Type();}
168 
169  void apply(Gadget*, GRender&, Specif) override;
170 };
171 
174 inline GBackground& Background() {return *new GBackground();}
175 inline GBackground& Background(float r, float g, float b, float a = 1.f) {return *new GBackground(r,g,b,a);}
176 inline GBackground& Background(GString const& value) {return *new GBackground(value);}
178 
179 }
180 #endif