1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 5 нояб. 2017 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef UI_TK_SYS_LSPCOLOR_H_
23 #define UI_TK_SYS_LSPCOLOR_H_
24 
25 namespace lsp
26 {
27     namespace tk
28     {
29         class LSPWidget;
30         class LSPStyle;
31 
32         class LSPColor
33         {
34             protected:
35                 class Listener: public IStyleListener
36                 {
37                     private:
38                         LSPColor   *pColor;
39                         LSPStyle   *pStyle;
40                         ui_atom_t   aR, aG, aB, aRGB;
41                         ui_atom_t   aH, aS, aL, aHSL;
42                         ui_atom_t   aA, aRGBA, aHSLA;
43 
44                     public:
45                         explicit Listener(LSPColor *color);
46                         virtual ~Listener();
47 
48                     public:
49                         virtual void    notify(ui_atom_t property);
50                         void            sync();
51                         void            unbind();
52                         void            reset();
53                         status_t        bind(LSPDisplay *dpy, LSPStyle *style, const char *property);
54                 };
55 
56             protected:
57                 Color       sColor;
58                 LSPWidget  *pWidget;
59                 Listener    sListener;
60 
61             public:
62                 explicit LSPColor();
63                 explicit LSPColor(LSPWidget *widget);
64                 virtual ~LSPColor();
65 
66             public:
67                 status_t    bind(const char *property);
68                 status_t    bind(LSPStyle *style, const char *property);
69                 status_t    bind(LSPDisplay *dpy, LSPStyle *style, const char *property);
unbind()70                 inline void unbind() { sListener.unbind(); };
71 
72             public:
73                 void        set_default();
74 
75             protected:
76                 virtual void    color_changed();
77                 void            trigger_change();
78 
79             public:
red()80                 inline float red() const        { return sColor.red();      }
green()81                 inline float green() const      { return sColor.green();    }
blue()82                 inline float blue() const       { return sColor.blue();     }
alpha()83                 inline float alpha() const      { return sColor.alpha();    }
84 
get_rgb(float & r,float & g,float & b)85                 inline void get_rgb(float &r, float &g, float &b) const { sColor.get_rgb(r, g, b); }
get_rgba(float & r,float & g,float & b,float & a)86                 inline void get_rgba(float &r, float &g, float &b, float &a) const { sColor.get_rgba(r, g, b, a); }
87 
hue()88                 inline float hue() const        { return sColor.hue();          }
saturation()89                 inline float saturation() const { return sColor.saturation();   }
lightness()90                 inline float lightness() const  { return sColor.lightness();    }
91 
get_hsl(float & h,float & s,float & l)92                 inline void get_hsl(float &h, float &s, float &l) const { sColor.get_hsl(h, s, l); }
get_hsla(float & h,float & s,float & l,float & a)93                 inline void get_hsla(float &h, float &s, float &l, float &a) const { sColor.get_hsla(h, s, l, a); }
94 
95             public:
96                 void red(float r);
97                 void green(float g);
98                 void blue(float b);
99                 void alpha(float a);
100 
101                 void set_rgb(float r, float g, float b);
102                 void set_rgba(float r, float g, float b, float a);
103 
104                 void hue(float h);
105                 void saturation(float s);
106                 void lightness(float l);
107 
108                 void set_hsl(float h, float s, float l);
109                 void set_hsla(float h, float s, float l, float a);
110 
111             public:
blend(const Color & c,float alpha)112                 void blend(const Color &c, float alpha)     { sColor.blend(c, alpha); trigger_change(); }
blend(float r,float g,float b,float alpha)113                 void blend(float r, float g, float b, float alpha) { sColor.blend(r, g, b, alpha); trigger_change(); }
darken(float amount)114                 void darken(float amount) { sColor.darken(amount); trigger_change(); }
lighten(float amount)115                 void lighten(float amount) { sColor.lighten(amount); trigger_change(); }
116 
copy(const Color & c)117                 void copy(const Color &c) { sColor.copy(c); trigger_change(); }
copy(const Color * c)118                 void copy(const Color *c) { sColor.copy(c); trigger_change(); }
119 
copy(const LSPColor & c)120                 void copy(const LSPColor &c) { sColor.copy(c.sColor); trigger_change(); }
copy(const LSPColor * c)121                 void copy(const LSPColor *c) { sColor.copy(c->sColor); trigger_change(); }
122 
123                 inline int format_rgb(char *dst, size_t len, size_t tolerance = 2) const
124                 {
125                     return sColor.format_rgb(dst, len, tolerance);
126                 }
127 
128                 inline int format_rgba(char *dst, size_t len, size_t tolerance = 2) const
129                 {
130                     return sColor.format_rgba(dst, len, tolerance);
131                 }
132 
133                 inline int format_hsl(char *dst, size_t len, size_t tolerance = 2) const
134                 {
135                     return sColor.format_hsl(dst, len, tolerance);
136                 }
137 
138                 inline int format_hsla(char *dst, size_t len, size_t tolerance = 2) const
139                 {
140                     return sColor.format_hsla(dst, len, tolerance);
141                 }
142 
rgb24()143                 inline uint32_t rgb24() const { return sColor.rgb24(); }
144 
145             public:
color()146                 inline const Color *color() const { return &sColor; }
147 
148 //                operator const Color &() const { return sColor;  }
149                 operator const Color *() const { return &sColor; }
150         };
151 
152     } /* namespace tk */
153 } /* namespace lsp */
154 
155 #endif /* UI_TK_SYS_LSPCOLOR_H_ */
156