Krolyxon
Commit 919ada1

Branches

Refs →

nsxiv919ada1

Align compile-time color options with X resource colors

Two colors are more than enough!
Author
Bert Münnich
Date
2019-01-23 21:18 +0100
Committer
Bert Münnich · 2019-01-23 21:21 +0100
Parent
9d244da
Commit
919ada11232781e3624363d834a6b9851c3a2bcb

+37 −32

5 files changed

  1. M config.def.h +2 −5
  2. M image.c +1 −1
  3. M sxiv.h +4 −5
  4. M thumbs.c +4 −4
  5. M window.c +26 −17
M config.def.h View
@@ -14,11 +14,8 @@ static const char * const BAR_FONT = "monospace:size=8";
1414 /* colors:
1515 * (see X(7) section "COLOR NAMES" for valid values)
1616 */
17−static const char * const WIN_BG_COLOR = "#555555";
18−static const char * const WIN_FS_COLOR = "#000000";
19−static const char * const SEL_COLOR = "#EEEEEE";
20−static const char * const BAR_BG_COLOR = "#222222";
21−static const char * const BAR_FG_COLOR = "#EEEEEE";
17+static const char * const BG_COLOR = "#555555";
18+static const char * const FG_COLOR = "#EEEEEE";
2219
2320 #endif
2421 #ifdef _IMAGE_CONFIG
M image.c View
@@ -497,7 +497,7 @@ void img_render(img_t *img)
497497 }
498498 imlib_image_put_back_data(data);
499499 } else {
500− c = win->fullscreen ? win->fscol.pixel : win->bgcol.pixel;
500+ c = win->fullscreen ? win->black.pixel : win->bg.pixel;
501501 imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF);
502502 imlib_image_fill_rectangle(0, 0, dw, dh);
503503 }
M sxiv.h View
@@ -408,9 +408,10 @@ struct win {
408408 Window xwin;
409409 win_env_t env;
410410
411− XftColor bgcol;
412− XftColor fscol;
413− XftColor selcol;
411+ bool light; /* bg is lighter than fg */
412+ XftColor bg;
413+ XftColor fg;
414+ XftColor black;
414415
415416 int x;
416417 int y;
@@ -430,8 +431,6 @@ struct win {
430431 unsigned int h;
431432 win_bar_t l;
432433 win_bar_t r;
433− XftColor bgcol;
434− XftColor fgcol;
435434 } bar;
436435 };
437436
M thumbs.c View
@@ -468,14 +468,14 @@ void tns_mark(tns_t *tns, int n, bool mark)
468468 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
469469 win_t *win = tns->win;
470470 thumb_t *t = &tns->thumbs[n];
471− unsigned long col = win->fullscreen ? win->fscol.pixel : win->bgcol.pixel;
471+ unsigned long col = win->fullscreen ? win->black.pixel : win->bg.pixel;
472472 int x = t->x + t->w, y = t->y + t->h;
473473
474474 win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
475475 win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
476476
477477 if (mark)
478− col = win->selcol.pixel;
478+ col = win->fullscreen && win->light ? win->bg.pixel : win->fg.pixel;
479479
480480 win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
481481
@@ -493,9 +493,9 @@ void tns_highlight(tns_t *tns, int n, bool hl)
493493 int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
494494
495495 if (hl)
496− col = win->selcol.pixel;
496+ col = win->fullscreen && win->light ? win->bg.pixel : win->fg.pixel;
497497 else
498− col = win->fullscreen ? win->fscol.pixel : win->bgcol.pixel;
498+ col = win->fullscreen ? win->black.pixel : win->bg.pixel;
499499
500500 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
501501 false, tns->bw, col);
M window.c View
@@ -121,12 +121,18 @@ const char* win_res(Display *dpy, const char *name, const char *def)
121121 }
122122 }
123123
124+unsigned int win_luminance(const XftColor *col)
125+{
126+ return (col->color.red + col->color.green + col->color.blue) / 3;
127+}
128+
124129 #define INIT_ATOM_(atom) \
125130 atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
126131
127132 void win_init(win_t *win)
128133 {
129134 win_env_t *e;
135+ const char *bg, *fg;
130136
131137 memset(win, 0, sizeof(win_t));
132138
@@ -146,15 +152,12 @@ void win_init(win_t *win)
146152
147153 win_init_font(e, BAR_FONT);
148154
149− win_alloc_color(e, win_res(e->dpy, RES_CLASS ".background", WIN_BG_COLOR),
150− &win->bgcol);
151− win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
152− win_alloc_color(e, win_res(e->dpy, RES_CLASS ".foreground", SEL_COLOR),
153− &win->selcol);
154− win_alloc_color(e, win_res(e->dpy, RES_CLASS ".foreground", BAR_BG_COLOR),
155− &win->bar.bgcol);
156− win_alloc_color(e, win_res(e->dpy, RES_CLASS ".background", BAR_FG_COLOR),
157− &win->bar.fgcol);
155+ bg = win_res(e->dpy, RES_CLASS ".background", BG_COLOR);
156+ fg = win_res(e->dpy, RES_CLASS ".foreground", FG_COLOR);
157+ win_alloc_color(e, bg, &win->bg);
158+ win_alloc_color(e, fg, &win->fg);
159+ win_alloc_color(e, "black", &win->black);
160+ win->light = win_luminance(&win->bg) > win_luminance(&win->fg);
158161
159162 win->bar.l.size = BAR_L_LEN;
160163 win->bar.r.size = BAR_R_LEN;
@@ -294,7 +297,7 @@ void win_open(win_t *win)
294297 win->buf.h = e->scrh;
295298 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
296299 win->buf.w, win->buf.h, e->depth);
297− XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
300+ XSetForeground(e->dpy, gc, fullscreen ? win->black.pixel : win->bg.pixel);
298301 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
299302 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
300303
@@ -386,14 +389,14 @@ void win_clear(win_t *win)
386389 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
387390 win->buf.w, win->buf.h, e->depth);
388391 }
389− XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
392+ XSetForeground(e->dpy, gc, win->fullscreen ? win->black.pixel : win->bg.pixel);
390393 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
391394 }
392395
393396 #define TEXTWIDTH(win, text, len) \
394397 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
395398
396−int win_draw_text(win_t *win, XftDraw *d, XftColor *color, int x, int y,
399+int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
397400 char *text, int len, int w)
398401 {
399402 int err, tw = 0;
@@ -432,6 +435,7 @@ void win_draw_bar(win_t *win)
432435 win_env_t *e;
433436 win_bar_t *l, *r;
434437 XftDraw *d;
438+ const XftColor *bg, *fg;
435439
436440 if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
437441 return;
@@ -442,23 +446,28 @@ void win_draw_bar(win_t *win)
442446 d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
443447 DefaultColormap(e->dpy, e->scr));
444448
445− XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
449+ if (win->fullscreen && !win->light)
450+ bg = &win->bg, fg = &win->fg;
451+ else
452+ bg = &win->fg, fg = &win->bg;
453+
454+ XSetForeground(e->dpy, gc, bg->pixel);
446455 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
447456
448− XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
449− XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
457+ XSetForeground(e->dpy, gc, fg->pixel);
458+ XSetBackground(e->dpy, gc, bg->pixel);
450459
451460 if ((len = strlen(r->buf)) > 0) {
452461 if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
453462 return;
454463 x = win->w - tw - H_TEXT_PAD;
455464 w -= tw;
456− win_draw_text(win, d, &win->bar.fgcol, x, y, r->buf, len, tw);
465+ win_draw_text(win, d, fg, x, y, r->buf, len, tw);
457466 }
458467 if ((len = strlen(l->buf)) > 0) {
459468 x = H_TEXT_PAD;
460469 w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
461− win_draw_text(win, d, &win->bar.fgcol, x, y, l->buf, len, w);
470+ win_draw_text(win, d, fg, x, y, l->buf, len, w);
462471 }
463472 XftDrawDestroy(d);
464473 }