Make the draw_*() functions take a color_t*

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2009-04-17 18:40:43 +02:00 committed by Julien Danjou
parent 293f2a312d
commit 37862c83c0
7 changed files with 111 additions and 62 deletions

66
draw.c
View File

@ -284,8 +284,8 @@ draw_text(draw_context_t *ctx, draw_text_context_t *data,
*/
static cairo_pattern_t *
draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
const xcolor_t *pcolor, const xcolor_t *pcolor_center,
const xcolor_t *pcolor_end)
const color_t *pcolor, const color_t *pcolor_center,
const color_t *pcolor_end)
{
cairo_pattern_t *pat = NULL;
bool has_center = pcolor_center->initialized;
@ -294,10 +294,10 @@ draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
/* no need for a real pattern: */
if(!has_end && !has_center)
cairo_set_source_rgba(ctx->cr,
pcolor->red / 65535.0,
pcolor->green / 65535.0,
pcolor->blue / 65535.0,
pcolor->alpha / 65535.0);
pcolor->red / 255.0,
pcolor->green / 255.0,
pcolor->blue / 255.0,
pcolor->alpha / 255.0);
else
{
pat = cairo_pattern_create_linear(gradient_vector.x,
@ -307,30 +307,30 @@ draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
/* pcolor is always set (so far in awesome) */
cairo_pattern_add_color_stop_rgba(pat, 0.0,
pcolor->red / 65535.0,
pcolor->green / 65535.0,
pcolor->blue / 65535.0,
pcolor->alpha / 65535.0);
pcolor->red / 255.0,
pcolor->green / 255.0,
pcolor->blue / 255.0,
pcolor->alpha / 255.0);
if(has_center)
cairo_pattern_add_color_stop_rgba(pat, 0.5,
pcolor_center->red / 65535.0,
pcolor_center->green / 65535.0,
pcolor_center->blue / 65535.0,
pcolor_center->alpha / 65535.0);
pcolor_center->red / 255.0,
pcolor_center->green / 255.0,
pcolor_center->blue / 255.0,
pcolor_center->alpha / 255.0);
if(has_end)
cairo_pattern_add_color_stop_rgba(pat, 1.0,
pcolor_end->red / 65535.0,
pcolor_end->green / 65535.0,
pcolor_end->blue / 65535.0,
pcolor_end->alpha / 65535.0);
pcolor_end->red / 255.0,
pcolor_end->green / 255.0,
pcolor_end->blue / 255.0,
pcolor_end->alpha / 255.0);
else
cairo_pattern_add_color_stop_rgba(pat, 1.0,
pcolor->red / 65535.0,
pcolor->green / 65535.0,
pcolor->blue / 65535.0,
pcolor->alpha / 65535.0);
pcolor->red / 255.0,
pcolor->green / 255.0,
pcolor->blue / 255.0,
pcolor->alpha / 255.0);
cairo_set_source(ctx->cr, pat);
}
return pat;
@ -345,17 +345,17 @@ draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
*/
void
draw_rectangle(draw_context_t *ctx, area_t geometry,
float line_width, bool filled, const xcolor_t *color)
float line_width, bool filled, const color_t *color)
{
cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
cairo_set_line_width(ctx->cr, line_width);
cairo_set_miter_limit(ctx->cr, 10.0);
cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
cairo_set_source_rgba(ctx->cr,
color->red / 65535.0,
color->green / 65535.0,
color->blue / 65535.0,
color->alpha / 65535.0);
color->red / 255.0,
color->green / 255.0,
color->blue / 255.0,
color->alpha / 255.0);
if(filled)
{
cairo_rectangle(ctx->cr, geometry.x, geometry.y,
@ -382,8 +382,8 @@ draw_rectangle(draw_context_t *ctx, area_t geometry,
*/
void
draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
vector_t gradient_vector, const xcolor_t *pcolor,
const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
vector_t gradient_vector, const color_t *pcolor,
const color_t *pcolor_center, const color_t *pcolor_end)
{
cairo_pattern_t *pat;
@ -437,8 +437,8 @@ draw_graph_setup(draw_context_t *ctx)
*/
void
draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
position_t grow, vector_t gradient_vector, const color_t *pcolor,
const color_t *pcolor_center, const color_t *pcolor_end)
{
int i = -1;
float x = rect.x + 0.5; /* middle of a pixel */
@ -490,8 +490,8 @@ draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
*/
void
draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
position_t grow, vector_t gradient_vector, const color_t *pcolor,
const color_t *pcolor_center, const color_t *pcolor_end)
{
int i, w;
float x, y;

8
draw.h
View File

@ -166,15 +166,15 @@ typedef struct
bool draw_text_context_init(draw_text_context_t *, const char *, ssize_t);
void draw_text(draw_context_t *, draw_text_context_t *, PangoEllipsizeMode, PangoWrapMode, alignment_t, padding_t *, area_t, area_t *);
void draw_rectangle(draw_context_t *, area_t, float, bool, const xcolor_t *);
void draw_rectangle(draw_context_t *, area_t, float, bool, const color_t *);
void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t,
const xcolor_t *, const xcolor_t *, const xcolor_t *);
const color_t *, const color_t *, const color_t *);
void draw_graph_setup(draw_context_t *);
void draw_graph(draw_context_t *, area_t, int *, int *, int, position_t, vector_t,
const xcolor_t *, const xcolor_t *, const xcolor_t *);
const color_t *, const color_t *, const color_t *);
void draw_graph_line(draw_context_t *, area_t, int *, int, position_t, vector_t,
const xcolor_t *, const xcolor_t *, const xcolor_t *);
const color_t *, const color_t *, const color_t *);
void draw_image(draw_context_t *, int, int, double, image_t *);
void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int);
area_t draw_text_extents(draw_text_context_t *);

View File

@ -141,6 +141,7 @@ widget_render(wibox_t *wibox)
draw_context_t *ctx = &wibox->sw.ctx;
int left = 0, right = 0;
area_t rectangle = { 0, 0, 0, 0 };
color_t col;
rectangle.width = ctx->width;
rectangle.height = ctx->height;
@ -265,7 +266,8 @@ widget_render(wibox_t *wibox)
draw_image(ctx, 0, 0, 1.0, wibox->bg_image);
/* draw background color */
draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
xcolor_to_color(&ctx->bg, &col);
draw_rectangle(ctx, rectangle, 1.0, true, &col);
/* draw everything! */
for(int i = 0; i < widgets->len; i++)

View File

@ -174,6 +174,8 @@ graph_draw(widget_t *widget, draw_context_t *ctx,
graph_data_t *d = widget->data;
area_t rectangle;
vector_t color_gradient;
color_t col;
color_t color_start, color_center, color_end;
if(!d->plots.len)
return;
@ -189,7 +191,8 @@ graph_draw(widget_t *widget, draw_context_t *ctx,
rectangle.y = margin_top + 1;
rectangle.width = d->size;
rectangle.height = d->box_height;
draw_rectangle(ctx, rectangle, 1.0, true, &d->bg);
xcolor_to_color(&d->bg, &col);
draw_rectangle(ctx, rectangle, 1.0, true, &col);
/* for plot drawing */
rectangle.y = margin_top + d->box_height + 1; /* bottom left corner as starting point */
@ -207,6 +210,11 @@ graph_draw(widget_t *widget, draw_context_t *ctx,
{
plot_t *plot = &d->plots.tab[i];
/* Prepare colors */
xcolor_to_color(&plot->color_start, &color_start);
xcolor_to_color(&plot->pcolor_center, &color_center);
xcolor_to_color(&plot->pcolor_end, &color_end);
switch(plot->draw_style)
{
case Top_Style:
@ -233,7 +241,7 @@ graph_draw(widget_t *widget, draw_context_t *ctx,
d->draw_to[y] = d->box_height - plot->lines[y]; /* i.e. on full plot -> 0 = bottom */
}
draw_graph(ctx, rectangle , d->draw_from, d->draw_to, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
&color_start, &color_center, &color_end);
break;
case Bottom_Style:
color_gradient.y = rectangle.y;
@ -254,7 +262,7 @@ graph_draw(widget_t *widget, draw_context_t *ctx,
p_clear(d->draw_from, d->size);
draw_graph(ctx, rectangle, d->draw_from, plot->lines, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
&color_start, &color_center, &color_end);
break;
case Line_Style:
color_gradient.y = rectangle.y;
@ -273,7 +281,7 @@ graph_draw(widget_t *widget, draw_context_t *ctx,
}
draw_graph_line(ctx, rectangle, plot->lines, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
&color_start, &color_center, &color_end);
break;
}
}
@ -283,7 +291,8 @@ graph_draw(widget_t *widget, draw_context_t *ctx,
rectangle.y = margin_top;
rectangle.width = d->size + 2;
rectangle.height = d->box_height + 2;
draw_rectangle(ctx, rectangle, 1.0, false, &d->border_color);
xcolor_to_color(&d->border_color, &col);
draw_rectangle(ctx, rectangle, 1.0, false, &col);
}
/** Set various plot graph properties.

View File

@ -85,8 +85,12 @@ imagebox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t *p
if(d->image && geometry.width && geometry.height)
{
if(d->bg.initialized)
draw_rectangle(ctx, geometry, 1.0, true, &d->bg);
color_t bg;
xcolor_to_color(&d->bg, &bg);
if(bg.initialized)
draw_rectangle(ctx, geometry, 1.0, true, &bg);
int y = geometry.y;
double ratio = d->resize ? (double) geometry.height / d->image->height : 1;

View File

@ -217,6 +217,20 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
for(int i = 0; i < d->bars.len; i++)
{
bar_t *bar = &d->bars.tab[i];
color_t bg;
color_t border_color;
color_t fg;
color_t fg_center;
color_t fg_end;
color_t fg_off;
xcolor_to_color(&bar->bg, &bg);
xcolor_to_color(&bar->border_color, &border_color);
xcolor_to_color(&bar->fg_off, &fg_off);
xcolor_to_color(&bar->fg, &fg);
xcolor_to_color(&bar->fg_center, &fg_center);
xcolor_to_color(&bar->fg_end, &fg_end);
if(d->ticks_count && d->ticks_gap)
{
values_ticks = (int)(d->ticks_count * (bar->value - bar->min_value)
@ -243,8 +257,8 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
rectangle.height = pb_height + 2 * (d->border_padding + d->border_width);
if(d->border_padding)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg);
draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color);
draw_rectangle(ctx, rectangle, 1.0, true, &bg);
draw_rectangle(ctx, rectangle, d->border_width, false, &border_color);
}
color_gradient.x = pb_x;
@ -275,10 +289,10 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
/* fg color */
if(bar->reverse)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
draw_rectangle(ctx, rectangle, 1.0, true, &fg_off);
else
draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end);
&fg, &fg_center, &fg_end);
}
/* top part */
@ -292,9 +306,9 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
/* bg color */
if(bar->reverse)
draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end);
&fg, &fg_center, &fg_end);
else
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
draw_rectangle(ctx, rectangle, 1.0, true, &fg_off);
}
/* draw gaps TODO: improve e.g all in one */
if(d->ticks_count && d->ticks_gap)
@ -305,7 +319,7 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
for(rectangle.y = pb_y + (unit - d->ticks_gap);
pb_y + pb_height - d->ticks_gap >= rectangle.y;
rectangle.y += unit)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg);
draw_rectangle(ctx, rectangle, 1.0, true, &bg);
}
pb_offset += pb_width + d->gap + 2 * (d->border_width + d->border_padding);
}
@ -329,6 +343,21 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
for(int i = 0; i < d->bars.len; i++)
{
bar_t *bar = &d->bars.tab[i];
color_t bg;
color_t fg;
color_t fg_off;
color_t fg_center;
color_t fg_end;
color_t border_color;
xcolor_to_color(&bar->bg, &bg);
xcolor_to_color(&bar->fg, &fg);
xcolor_to_color(&bar->fg_off, &fg_off);
xcolor_to_color(&bar->fg_center, &fg_center);
xcolor_to_color(&bar->fg_end, &fg_end);
xcolor_to_color(&bar->border_color, &border_color);
if(d->ticks_count && d->ticks_gap)
{
/* +0.5 rounds up ticks -> turn on a tick when half of it is reached */
@ -352,8 +381,8 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
rectangle.height = pb_height + 2 * (d->border_padding + d->border_width);
if(d->border_padding)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg);
draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color);
draw_rectangle(ctx, rectangle, 1.0, true, &bg);
draw_rectangle(ctx, rectangle, d->border_width, false, &border_color);
}
color_gradient.y = pb_y;
@ -382,10 +411,10 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
/* fg color */
if(bar->reverse)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
draw_rectangle(ctx, rectangle, 1.0, true, &fg_off);
else
draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end);
&fg, &fg_center, &fg_end);
}
/* right part */
@ -399,9 +428,9 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
/* bg color */
if(bar->reverse)
draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end);
&fg, &fg_center, &fg_end);
else
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
draw_rectangle(ctx, rectangle, 1.0, true, &fg_off);
}
/* draw gaps TODO: improve e.g all in one */
if(d->ticks_count && d->ticks_gap)
@ -412,7 +441,7 @@ progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t
for(rectangle.x = pb_x + (unit - d->ticks_gap);
pb_x + pb_width - d->ticks_gap >= rectangle.x;
rectangle.x += unit)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg);
draw_rectangle(ctx, rectangle, 1.0, true, &bg);
}
pb_offset += pb_height + d->gap + 2 * (d->border_width + d->border_padding);

View File

@ -84,12 +84,17 @@ static void
textbox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t *p)
{
textbox_data_t *d = widget->data;
color_t bg;
color_t border_color;
if(d->bg.initialized)
draw_rectangle(ctx, geometry, 1.0, true, &d->bg);
xcolor_to_color(&d->bg, &bg);
xcolor_to_color(&d->border.color, &border_color);
if(bg.initialized)
draw_rectangle(ctx, geometry, 1.0, true, &bg);
if(d->border.width > 0)
draw_rectangle(ctx, geometry, d->border.width, false, &d->border.color);
draw_rectangle(ctx, geometry, d->border.width, false, &border_color);
if(d->bg_image)
{