drw: fixup color schemes

This commit is contained in:
Markus Teich 2015-04-10 23:12:50 +02:00
parent de1dc79944
commit bd3cfd17e3
2 changed files with 31 additions and 34 deletions

41
drw.c
View File

@ -171,35 +171,36 @@ drw_font_free(Fnt *font) {
free(font); free(font);
} }
Clr * Scm *
drw_clr_create(Drw *drw, const char *clrname) { drw_scm_create(Drw *drw, const char *fgname, const char *bgname) {
Clr *clr; Scm *scm;
Colormap cmap; Colormap cmap;
Visual *vis; Visual *vis;
if(!drw) if (!drw || !(scm = (Scm *)calloc(1, sizeof(Scm))))
return NULL;
clr = (Clr *)calloc(1, sizeof(Clr));
if(!clr)
return NULL; return NULL;
cmap = DefaultColormap(drw->dpy, drw->screen); cmap = DefaultColormap(drw->dpy, drw->screen);
vis = DefaultVisual(drw->dpy, drw->screen); vis = DefaultVisual(drw->dpy, drw->screen);
if(!XftColorAllocName(drw->dpy, vis, cmap, clrname, &clr->rgb)) if (!XftColorAllocName(drw->dpy, vis, cmap, fgname, &scm->fg.rgb))
die("error, cannot allocate color '%s'\n", clrname); die("error, cannot allocate color '%s'\n", fgname);
clr->pix = clr->rgb.pixel; if (!XftColorAllocName(drw->dpy, vis, cmap, bgname, &scm->bg.rgb))
return clr; die("error, cannot allocate color '%s'\n", bgname);
scm->fg.pix = scm->fg.rgb.pixel;
scm->bg.pix = scm->bg.rgb.pixel;
return scm;
} }
void void
drw_clr_free(Clr *clr) { drw_scm_free(Scm *scm) {
if(clr) if (scm)
free(clr); free(scm);
} }
void void
drw_setscheme(Drw *drw, ClrScheme *scheme) { drw_setscheme(Drw *drw, Scm *scm) {
if(drw && scheme) if (drw && scm)
drw->scheme = scheme; drw->scheme = scm;
} }
void void
@ -208,7 +209,7 @@ drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int
if(!drw || !drw->fontcount || !drw->scheme) if(!drw || !drw->fontcount || !drw->scheme)
return; return;
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix); XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg.pix : drw->scheme->fg.pix);
dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4; dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
if(filled) if(filled)
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1); XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1);
@ -242,7 +243,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
if (!drw || !drw->scheme) { if (!drw || !drw->scheme) {
return 0; return 0;
} else if (render) { } else if (render) {
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg->pix : drw->scheme->bg->pix); XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg.pix : drw->scheme->bg.pix);
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
} }
@ -297,7 +298,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
th = curfont->ascent + curfont->descent; th = curfont->ascent + curfont->descent;
ty = y + (h / 2) - (th / 2) + curfont->ascent; ty = y + (h / 2) - (th / 2) + curfont->ascent;
tx = x + (h / 2); tx = x + (h / 2);
XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len); XftDrawStringUtf8(d, invert ? &drw->scheme->bg.rgb : &drw->scheme->fg.rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
} }
x += ew; x += ew;

24
drw.h
View File

@ -1,11 +1,6 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#define DRW_FONT_CACHE_SIZE 32 #define DRW_FONT_CACHE_SIZE 32
typedef struct {
unsigned long pix;
XftColor rgb;
} Clr;
typedef struct { typedef struct {
Cursor cursor; Cursor cursor;
} Cur; } Cur;
@ -20,10 +15,11 @@ typedef struct {
} Fnt; } Fnt;
typedef struct { typedef struct {
Clr *fg; struct {
Clr *bg; unsigned long pix;
Clr *border; XftColor rgb;
} ClrScheme; } fg, bg;
} Scm;
typedef struct { typedef struct {
unsigned int w, h; unsigned int w, h;
@ -32,7 +28,7 @@ typedef struct {
Window root; Window root;
Drawable drawable; Drawable drawable;
GC gc; GC gc;
ClrScheme *scheme; Scm *scheme;
size_t fontcount; size_t fontcount;
Fnt *fonts[DRW_FONT_CACHE_SIZE]; Fnt *fonts[DRW_FONT_CACHE_SIZE];
} Drw; } Drw;
@ -48,9 +44,9 @@ void drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount);
void drw_font_free(Fnt *font); void drw_font_free(Fnt *font);
void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h);
/* Colour abstraction */ /* Colorscheme abstraction */
Clr *drw_clr_create(Drw *drw, const char *clrname); Scm *drw_scm_create(Drw *drw, const char *fgname, const char *bgname);
void drw_clr_free(Clr *clr); void drw_scm_free(Scm *scm);
/* Cursor abstraction */ /* Cursor abstraction */
Cur *drw_cur_create(Drw *drw, int shape); Cur *drw_cur_create(Drw *drw, int shape);
@ -58,7 +54,7 @@ void drw_cur_free(Drw *drw, Cur *cursor);
/* Drawing context manipulation */ /* Drawing context manipulation */
void drw_setfont(Drw *drw, Fnt *font); void drw_setfont(Drw *drw, Fnt *font);
void drw_setscheme(Drw *drw, ClrScheme *scheme); void drw_setscheme(Drw *drw, Scm *scm);
/* Drawing functions */ /* Drawing functions */
void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert); void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert);