diff options
| author | katalx | 2026-01-29 02:50:19 -0500 |
|---|---|---|
| committer | katalx | 2026-01-29 02:50:19 -0500 |
| commit | 4173408110e55deb44644d9a8e15a8bb698f913d (patch) | |
| tree | 27030d9e47bd866d686fe257467a923f09c2f254 | |
| parent | 1b60f12d71688ec4b0c1429d6c6283cb0f6335ac (diff) | |
nicer colors
| -rw-r--r-- | ui.c | 81 |
1 files changed, 61 insertions, 20 deletions
@@ -13,12 +13,34 @@ static Display *dsp; static Window win; static int scr; + static unsigned long fg, bg; +static unsigned long selfg, selbg; +static unsigned long inpfg, inpbg; + static XSizeHints szhint; static GC gc; static Atom wm_delete_window; -static XFontStruct *freg, *fital; +static XFontStruct *ft_opt, *ft_inp; + +static inline unsigned long +color(const char *name) +{ + XColor exact, screen; + Colormap cmap = DefaultColormap(dsp, scr); + Status ok = XLookupColor(dsp, cmap, name, &exact, &screen); + /* this is probably non-portable but the pixel fields in + * both exact and screen are absolute bunk, seemingly only + * have one channel + */ + unsigned long px = ((screen.red << 8) & 0xff0000) + | (screen.green & 0xff00) + | (screen.blue >> 8); + if (!ok) + fprintf(stderr, "bad color %s!\n", name); + return px; +} void ui_init(int argc, char **argv, UiOpts opt) @@ -26,7 +48,13 @@ ui_init(int argc, char **argv, UiOpts opt) dsp = XOpenDisplay(NULL); scr = DefaultScreen(dsp); fg = BlackPixel(dsp, scr); - bg = WhitePixel(dsp, scr); + bg = color("lavender"); + + selfg = bg; + selbg = color("steelblue"); + + inpfg = color("firebrick"); + inpbg = bg; int scr_width = XWidthOfScreen(ScreenOfDisplay(dsp, scr)); int scr_height = XHeightOfScreen(ScreenOfDisplay(dsp, scr)); @@ -59,15 +87,15 @@ ui_init(int argc, char **argv, UiOpts opt) XSetWMProtocols(dsp, win, &wm_delete_window, 1); /* TODO: error checking */ - freg = XLoadQueryFont(dsp, "-*-new century schoolbook-medium-r-*-*-24-*-*-*-*-*-*-*"); - fital = XLoadQueryFont(dsp, "-*-new century schoolbook-medium-i-*-*-24-*-*-*-*-*-*-*"); + ft_opt = XLoadQueryFont(dsp, "-*-new century schoolbook-medium-r-*-*-24-*-*-*-*-*-*-*"); + ft_inp = XLoadQueryFont(dsp, "-*-new century schoolbook-medium-i-*-*-24-*-*-*-*-*-*-*"); - if (!freg) freg = XLoadQueryFont(dsp, "fixed"); - if (!fital) fital = XLoadQueryFont(dsp, "fixed"); + if (!ft_opt) ft_opt = XLoadQueryFont(dsp, "fixed"); + if (!ft_inp) ft_inp = XLoadQueryFont(dsp, "fixed"); int w = 0; for (int i = 0; i < opt.n; i++) { - int ow = XTextWidth(freg, opt.v[i].s, opt.v[i].n); + int ow = XTextWidth(ft_opt, opt.v[i].s, opt.v[i].n); if (ow > w) w = ow; } @@ -148,17 +176,29 @@ ui_draw(Str input, int inpi, int seli, UiOpts o) /* draw input */ - XSetFont(dsp, gc, fital->fid); - XDrawString(dsp, win, gc, 16, 32, input.s, input.n); - int w = XTextWidth(fital, input.s, inpi); + int w = XTextWidth(ft_inp, input.s, inpi); + int y = 32; + + XSetForeground(dsp, gc, inpbg); + XFillRectangle(dsp, win, gc, + 12, y - ft_opt->ascent, + w + 8, ft_opt->descent + ft_opt->ascent); + + XSetForeground(dsp, gc, inpfg); + + XSetFont(dsp, gc, ft_inp->fid); XDrawLine(dsp, win, gc, - 16 + w, 32 - fital->ascent, - 16 + w, 32 + fital->descent); + 16 + w, y - ft_inp->ascent, + 16 + w, y + ft_inp->descent); + + XDrawString(dsp, win, gc, 16, y, input.s, input.n); + XSetForeground(dsp, gc, fg); + + y += 24; /* draw options */ - XSetFont(dsp, gc, freg->fid); - int y = 64; + XSetFont(dsp, gc, ft_opt->fid); int lines = (attr.height - 32) / 24; int tline = lines / 2; @@ -173,11 +213,12 @@ ui_draw(Str input, int inpi, int seli, UiOpts o) for (int i = top; i < bot; i++) { if (i == seli) { - int w = XTextWidth(freg, o.v[i].s, o.v[i].n); + int w = XTextWidth(ft_opt, o.v[i].s, o.v[i].n); + XSetForeground(dsp, gc, selbg); XFillRectangle(dsp, win, gc, - 12, y - freg->ascent, - w + 8, freg->descent + freg->ascent); - XSetForeground(dsp, gc, bg); + 12, y - ft_opt->ascent, + w + 8, ft_opt->descent + ft_opt->ascent); + XSetForeground(dsp, gc, selfg); XDrawString(dsp, win, gc, 16, y, o.v[i].s, o.v[i].n); XSetForeground(dsp, gc, fg); @@ -193,8 +234,8 @@ ui_draw(Str input, int inpi, int seli, UiOpts o) void ui_fini(void) { - XFreeFont(dsp, freg); - XFreeFont(dsp, fital); + XFreeFont(dsp, ft_opt); + XFreeFont(dsp, ft_inp); XFreeGC(dsp, gc); XDestroyWindow(dsp, win); XCloseDisplay(dsp); |
