libxputty  0.1
A damn tiny abstraction Layer to create X11 window/widgets with cairo surfaces
xcolor.c
Go to the documentation of this file.
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 }
use_shadow_color_scheme
void use_shadow_color_scheme(Widget_t *w, Color_state st)
use_shadow_color_scheme - use shadow Colors to paint on Widget_t
Definition: xcolor.c:206
XColor_t::active
Colors active
Definition: xcolor.h:92
use_light_color_scheme
void use_light_color_scheme(Widget_t *w, Color_state st)
use_light_color_scheme - use light Colors to paint on Widget_t
Definition: xcolor.c:220
Xputty
Xputty - the main struct. It should be declared before any other call to a Xputty function....
Definition: xputty.h:177
Colors::text
double text[4]
Definition: xcolor.h:77
FRAME_
@ FRAME_
Definition: xcolor.h:57
Colors::bg
double bg[4]
Definition: xcolor.h:75
SHADOW_
@ SHADOW_
Definition: xcolor.h:56
use_base_color_scheme
void use_base_color_scheme(Widget_t *w, Color_state st)
use_base_color_scheme - use base Colors to paint on Widget_t
Definition: xcolor.c:192
INSENSITIVE_
@ INSENSITIVE_
Definition: xcolor.h:43
Colors::base
double base[4]
Definition: xcolor.h:76
BASE_
@ BASE_
Definition: xcolor.h:54
FORGROUND_
@ FORGROUND_
Definition: xcolor.h:52
XColor_t::insensitive
Colors insensitive
Definition: xcolor.h:93
Widget_t::crb
cairo_t * crb
Definition: xwidget.h:318
use_frame_color_scheme
void use_frame_color_scheme(Widget_t *w, Color_state st)
use_frame_color_scheme - use frame Colors to paint on Widget_t
Definition: xcolor.c:213
set_pattern
void set_pattern(Widget_t *w, Colors *from, Colors *to, Color_mod mod)
set_pattern - set pattern for the selected Colors
Definition: xcolor.c:227
Widget_t::cr
cairo_t * cr
Definition: xwidget.h:314
BACKGROUND_
@ BACKGROUND_
Definition: xcolor.h:53
use_text_color_scheme
void use_text_color_scheme(Widget_t *w, Color_state st)
use_text_color_scheme - use text Colors to paint on Widget_t
Definition: xcolor.c:199
TEXT_
@ TEXT_
Definition: xcolor.h:55
XColor_t::prelight
Colors prelight
Definition: xcolor.h:90
LIGHT_
@ LIGHT_
Definition: xcolor.h:58
Widget_t::app
Xputty * app
Definition: xwidget.h:300
Xputty::dpy
Display * dpy
Definition: xputty.h:181
Widget_t::state
int state
Definition: xwidget.h:342
Colors
Color_t - struct used to set cairo color for Widget_t.
Definition: xcolor.h:73
ACTIVE_
@ ACTIVE_
Definition: xcolor.h:42
Widget_t::widget
Window widget
Definition: xwidget.h:302
XColor_t::selected
Colors selected
Definition: xcolor.h:91
Widget_t
Widget_t - struct to hold the basic Widget_t info.
Definition: xwidget.h:298
get_color_scheme
Colors * get_color_scheme(Xputty *main, Color_state st)
get_color_scheme - get pointer to the Colors struct to use in relation to the Color_state
Definition: xcolor.c:130
NORMAL_
@ NORMAL_
Definition: xcolor.h:39
PRELIGHT_
@ PRELIGHT_
Definition: xcolor.h:40
set_dark_theme
void set_dark_theme(Xputty *main)
set_dark_theme - init the XColor_t struct to the default dark theme
Definition: xcolor.c:24
set_light_theme
void set_light_theme(Xputty *main)
set_light_theme - init the XColor_t struct to the default light theme
Definition: xcolor.c:77
use_fg_color_scheme
void use_fg_color_scheme(Widget_t *w, Color_state st)
use_fg_color_scheme - use forground Colors to paint on Widget_t
Definition: xcolor.c:178
Colors::frame
double frame[4]
Definition: xcolor.h:79
Color_mod
Color_mod
Color_set - select color set to use on draw.
Definition: xcolor.h:51
use_bg_color_scheme
void use_bg_color_scheme(Widget_t *w, Color_state st)
use_bg_color_scheme - use background Colors to paint on Widget_t
Definition: xcolor.c:185
Xputty::color_scheme
XColor_t * color_scheme
Definition: xputty.h:183
SELECTED_
@ SELECTED_
Definition: xcolor.h:41
Colors::light
double light[4]
Definition: xcolor.h:80
xcolor.h
Color_state
Color_state
Color_state - select color mode to use on Widget_t.
Definition: xcolor.h:38
XColor_t::normal
Colors normal
Definition: xcolor.h:89
Colors::shadow
double shadow[4]
Definition: xcolor.h:78
get_color_state
Color_state get_color_state(Widget_t *wid)
get_color_state - get the Color_state to use in relation to the Widget_t state
Definition: xcolor.c:155
Colors::fg
double fg[4]
Definition: xcolor.h:74