1 /*
2  *                           0BSD
3  *
4  *                    BSD Zero Clause License
5  *
6  *  Copyright (c) 2019 Hermann Meyer
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted.
10 
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  *
19  */
20 
21 #include "xcolor.h"
22 
23 
set_dark_theme(Xputty * main)24 void set_dark_theme(Xputty *main) {
25     main->color_scheme->normal = (Colors) {
26          /* cairo    / r  / g  / b  / a  /  */
27         .fg =       { 0.85, 0.85, 0.85, 1.0},
28         .bg =       { 0.1, 0.1, 0.1, 1.0},
29         .base =     { 0.0, 0.0, 0.0, 1.0},
30         .text =     { 0.9, 0.9, 0.9, 1.0},
31         .shadow =   { 0.0, 0.0, 0.0, 0.2},
32         .frame =    { 0.0, 0.0, 0.0, 1.0},
33         .light =    { 0.1, 0.1, 0.1, 1.0}
34     };
35 
36     main->color_scheme->prelight = (Colors) {
37         .fg =       { 1.0, 0.0, 1.0, 1.0},
38         .bg =       { 0.25, 0.25, 0.25, 1.0},
39         .base =     { 0.3, 0.3, 0.3, 1.0},
40         .text =     { 1.0, 1.0, 1.0, 1.0},
41         .shadow =   { 0.1, 0.1, 0.1, 0.4},
42         .frame =    { 0.3, 0.3, 0.3, 1.0},
43         .light =    { 0.3, 0.3, 0.3, 1.0}
44     };
45 
46     main->color_scheme->selected = (Colors) {
47         .fg =       { 0.9, 0.9, 0.9, 1.0},
48         .bg =       { 0.2, 0.2, 0.2, 1.0},
49         .base =     { 0.5, 0.18, 0.18, 1.0},
50         .text =     { 1.0, 1.0, 1.0, 1.0},
51         .shadow =   { 0.8, 0.18, 0.18, 0.2},
52         .frame =    { 0.5, 0.18, 0.18, 1.0},
53         .light =    { 0.5, 0.18, 0.18, 1.0}
54     };
55 
56     main->color_scheme->active = (Colors) {
57         .fg =       { 0.0, 1.0, 1.0, 1.0},
58         .bg =       { 0.0, 0.0, 0.0, 1.0},
59         .base =     { 0.18, 0.38, 0.38, 1.0},
60         .text =     { 0.75, 0.75, 0.75, 1.0},
61         .shadow =   { 0.18, 0.38, 0.38, 0.5},
62         .frame =    { 0.18, 0.38, 0.38, 1.0},
63         .light =    { 0.18, 0.38, 0.38, 1.0}
64     };
65 
66     main->color_scheme->insensitive = (Colors) {
67         .fg =       { 0.85, 0.85, 0.85, 0.5},
68         .bg =       { 0.1, 0.1, 0.1, 0.5},
69         .base =     { 0.0, 0.0, 0.0, 0.5},
70         .text =     { 0.9, 0.9, 0.9, 0.5},
71         .shadow =   { 0.0, 0.0, 0.0, 0.1},
72         .frame =    { 0.0, 0.0, 0.0, 0.5},
73         .light =    { 0.1, 0.1, 0.1, 0.5}
74     };
75 }
76 
set_light_theme(Xputty * main)77 void set_light_theme(Xputty *main) {
78     main->color_scheme->normal = (Colors) {
79          /* cairo    / r  / g  / b  / a  /  */
80         .fg =       { 0.15, 0.15, 0.15, 1.0},
81         .bg =       { 0.85, 0.85, 0.85, 1.0},
82         .base =     { 0.9, 0.9, 0.9, 1.0},
83         .text =     { 0.25, 0.25, 0.25, 1.0},
84         .shadow =   { 0.0, 0.0, 0.0, 0.2},
85         .frame =    { 0.2, 0.2, 0.2, 1.0},
86         .light =    { 0.9, 0.9, 0.9, 1.0}
87     };
88 
89     main->color_scheme->prelight = (Colors) {
90         .fg =       { 0.25, 0.25, 0.25, 1.0},
91         .bg =       { 1.0, 1.0, 1.0, 1.0},
92         .base =     { 0.75, 0.75, 0.75, 1.0},
93         .text =     { 0.15, 0.15, 0.15, 1.0},
94         .shadow =   { 0.1, 0.1, 0.1, 0.4},
95         .frame =    { 0.3, 0.3, 0.3, 1.0},
96         .light =    { 0.75, 0.75, 0.75, 1.0}
97     };
98 
99     main->color_scheme->selected = (Colors) {
100         .fg =       { 0.2, 0.2, 0.2, 1.0},
101         .bg =       { 0.9, 0.9, 0.9, 1.0},
102         .base =     { 0.0, 0.5, 0.65, 1.0},
103         .text =     { 0.8, 0.8, 0.8, 1.0},
104         .shadow =   { 0.8, 0.18, 0.18, 0.2},
105         .frame =    { 0.5, 0.18, 0.18, 1.0},
106         .light =    { 0.5, 0.5, 0.5, 1.0}
107     };
108 
109     main->color_scheme->active = (Colors) {
110         .fg =       { 0.0, 0.0, 0.0, 1.0},
111         .bg =       { 1.0, 1.0, 1.0, 1.0},
112         .base =     { 0.0, 0.3, 0.65, 1.0},
113         .text =     { 0.8, 0.8, 0.8, 1.0},
114         .shadow =   { 0.18, 0.38, 0.38, 0.5},
115         .frame =    { 0.18, 0.38, 0.38, 1.0},
116         .light =    { 0.3, 0.3, 0.3, 1.0}
117     };
118 
119     main->color_scheme->insensitive = (Colors) {
120         .fg =       { 0.15, 0.15, 0.15, 0.5},
121         .bg =       { 0.85, 0.85, 0.85, 0.5},
122         .base =     { 0.9, 0.9, 0.9, 0.5},
123         .text =     { 0.25, 0.25, 0.25, 0.5},
124         .shadow =   { 0.0, 0.0, 0.0, 0.1},
125         .frame =    { 0.2, 0.2, 0.2, 0.5},
126         .light =    { 0.9, 0.9, 0.9, 0.5}
127     };
128 }
129 
get_color_scheme(Xputty * main,Color_state st)130 Colors *get_color_scheme(Xputty *main, Color_state st) {
131     switch(st) {
132         case NORMAL_:
133             return &main->color_scheme->normal;
134         break;
135         case PRELIGHT_:
136             return &main->color_scheme->prelight;
137         break;
138         case SELECTED_:
139             return &main->color_scheme->selected;
140         break;
141         case ACTIVE_:
142             return &main->color_scheme->active;
143         break;
144         case INSENSITIVE_:
145             return &main->color_scheme->insensitive;
146         break;
147         default:
148             return &main->color_scheme->normal;
149         break;
150     }
151     return NULL;
152 
153 }
154 
get_color_state(Widget_t * wid)155 Color_state get_color_state(Widget_t *wid) {
156     switch(wid->state) {
157         case 0:
158             return NORMAL_;
159         break;
160         case 1:
161             return PRELIGHT_;
162         break;
163         case 2:
164             return SELECTED_;
165         break;
166         case 3:
167             return ACTIVE_;
168         break;
169         case 4:
170             return INSENSITIVE_;
171         break;
172         default:
173             return NORMAL_;
174         break;
175     }
176 }
177 
use_fg_color_scheme(Widget_t * w,Color_state st)178 void use_fg_color_scheme(Widget_t *w, Color_state st) {
179     Colors *c = get_color_scheme(w->app, st);
180     if (!c) return;
181     cairo_set_source_rgba(w->cr, c->fg[0],  c->fg[1], c->fg[2],  c->fg[3]);
182     cairo_set_source_rgba(w->crb, c->fg[0],  c->fg[1], c->fg[2],  c->fg[3]);
183 }
184 
use_bg_color_scheme(Widget_t * w,Color_state st)185 void use_bg_color_scheme(Widget_t *w, Color_state st) {
186     Colors *c = get_color_scheme(w->app, st);
187     if (!c) return;
188     cairo_set_source_rgba(w->cr, c->bg[0],  c->bg[1], c->bg[2],  c->bg[3]);
189     cairo_set_source_rgba(w->crb, c->bg[0],  c->bg[1], c->bg[2],  c->bg[3]);
190 }
191 
use_base_color_scheme(Widget_t * w,Color_state st)192 void use_base_color_scheme(Widget_t *w, Color_state st) {
193     Colors *c = get_color_scheme(w->app, st);
194     if (!c) return;
195     cairo_set_source_rgba(w->cr, c->base[0],  c->base[1], c->base[2],  c->base[3]);
196     cairo_set_source_rgba(w->crb, c->base[0],  c->base[1], c->base[2],  c->base[3]);
197 }
198 
use_text_color_scheme(Widget_t * w,Color_state st)199 void use_text_color_scheme(Widget_t *w, Color_state st) {
200     Colors *c = get_color_scheme(w->app, st);
201     if (!c) return;
202     cairo_set_source_rgba(w->cr, c->text[0],  c->text[1], c->text[2],  c->text[3]);
203     cairo_set_source_rgba(w->crb, c->text[0],  c->text[1], c->text[2],  c->text[3]);
204 }
205 
use_shadow_color_scheme(Widget_t * w,Color_state st)206 void use_shadow_color_scheme(Widget_t *w, Color_state st) {
207     Colors *c = get_color_scheme(w->app, st);
208     if (!c) return;
209     cairo_set_source_rgba(w->cr, c->shadow[0],  c->shadow[1], c->shadow[2],  c->shadow[3]);
210     cairo_set_source_rgba(w->crb, c->shadow[0],  c->shadow[1], c->shadow[2],  c->shadow[3]);
211 }
212 
use_frame_color_scheme(Widget_t * w,Color_state st)213 void use_frame_color_scheme(Widget_t *w, Color_state st) {
214     Colors *c = get_color_scheme(w->app, st);
215     if (!c) return;
216     cairo_set_source_rgba(w->cr, c->frame[0],  c->frame[1], c->frame[2],  c->frame[3]);
217     cairo_set_source_rgba(w->crb, c->frame[0],  c->frame[1], c->frame[2],  c->frame[3]);
218 }
219 
use_light_color_scheme(Widget_t * w,Color_state st)220 void use_light_color_scheme(Widget_t *w, Color_state st) {
221     Colors *c = get_color_scheme(w->app, st);
222     if (!c) return;
223     cairo_set_source_rgba(w->cr, c->light[0],  c->light[1], c->light[2],  c->light[3]);
224     cairo_set_source_rgba(w->crb, c->light[0],  c->light[1], c->light[2],  c->light[3]);
225 }
226 
set_pattern(Widget_t * w,Colors * from,Colors * to,Color_mod mod)227 void set_pattern(Widget_t *w, Colors *from, Colors *to, Color_mod mod) {
228     double *col_from = NULL;
229     double *col_to = NULL;
230     switch (mod) {
231         case FORGROUND_:
232             col_from = from->fg;
233             col_to = to->fg;
234         break;
235         case BACKGROUND_:
236             col_from = from->bg;
237             col_to = to->bg;
238         break;
239         case BASE_:
240             col_from = from->base;
241             col_to = to->base;
242         break;
243         case TEXT_:
244             col_from = from->text;
245             col_to = to->text;
246         break;
247         case SHADOW_:
248             col_from = from->shadow;
249             col_to = to->shadow;
250         break;
251         case FRAME_:
252             col_from = from->frame;
253             col_to = to->frame;
254         break;
255         case LIGHT_:
256             col_from = from->light;
257             col_to = to->light;
258         break;
259    }
260     XWindowAttributes attrs;
261     XGetWindowAttributes(w->app->dpy, (Window)w->widget, &attrs);
262     int width = attrs.width;
263     int height = attrs.height;
264     cairo_pattern_t *pat = cairo_pattern_create_linear (0, 0, width, height);
265     cairo_pattern_add_color_stop_rgba(pat, 0.0, col_from[0], col_from[1], col_from[2], col_from[3]);
266     cairo_pattern_add_color_stop_rgba(pat, 1.0, col_to[0], col_to[1], col_to[2], col_to[3]);
267     cairo_set_source(w->crb, pat);
268     cairo_set_source(w->cr, pat);
269     cairo_pattern_destroy (pat);
270 }
271