Skip to content

Commit

Permalink
Fix font shadow color
Browse files Browse the repository at this point in the history
Use the average alpha value for rendering text.
Resolves #150.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Jul 10, 2024
1 parent 2f33c4c commit c2ed948
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
6 changes: 3 additions & 3 deletions extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ preload = 1
name = monospace
# Font size (in pt)
size = 14
# Font color
# Font color (RGB or RGBA, e.g. #11223344)
color = #cccccc
# Drop shadow (none/RGB, e.g. #112233)
shadow = #000000
# Shadow color (RGB or RGBA, disable: #00000000)
shadow = #000000a0

################################################################################
# Image meta info scheme (format, size, EXIF, etc)
Expand Down
5 changes: 3 additions & 2 deletions extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ Set the font size (in pt), default is \fI14\fR.
.IP "\fBcolor\fR = \fI#COLOR\fR"
Set text color in RGB hex format, default is \fI#cccccc\fR.
.\" ----------------------------------------------------------------------------
.IP "\fBshadow\fR = \fI[none|COLOR]\fR"
Draw text shadow with specified color, default is \fI#000000\fR.
.IP "\fBshadow\fR = \fI#COLOR\fR"
Draw text shadow with specified color, default is \fI#000000a0\fR.
To disable shadow use fully transparent color \fI#00000000\fR.
.\" ****************************************************************************
.\" Text info config section
.\" ****************************************************************************
Expand Down
9 changes: 6 additions & 3 deletions src/pixmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ void pixmap_apply_mask(struct pixmap* dst, size_t x, size_t y,
const uint8_t* mask_line = &mask[mask_y * width];

for (size_t mask_x = 0; mask_x < mask_width; ++mask_x) {
const uint8_t alpha = mask_line[mask_x];
if (alpha != 0) {
alpha_blend(ARGB_SET_A(alpha) | color, &dst_line[mask_x]);
const uint8_t alpha_mask = mask_line[mask_x];
if (alpha_mask != 0) {
const uint8_t alpha_color = ARGB_GET_A(color);
const uint8_t alpha = ((uint32_t)alpha_mask + alpha_color) / 2;
const argb_t clr = ARGB_SET_A(alpha) | (color & 0x00ffffff);
alpha_blend(clr, &dst_line[mask_x]);
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

#include <string.h>

// Text rendering parameters
#define TEXT_COLOR 0x00cccccc
#define TEXT_SHADOW 0x00000000
#define TEXT_NO_SHADOW 0xff000000
#define TEXT_PADDING 10 // space between text layout and window edge
// Space between text layout and window edge
#define TEXT_PADDING 10

/** Text context. */
struct text {
Expand All @@ -22,8 +19,8 @@ struct text {
};

static struct text ctx = {
.color = TEXT_COLOR,
.shadow = TEXT_SHADOW,
.color = 0xffcccccc,
.shadow = 0x80000000,
};

/**
Expand All @@ -35,7 +32,7 @@ static struct text ctx = {
static void draw_text(struct pixmap* wnd, size_t x, size_t y,
const struct text_surface* text)
{
if (ctx.shadow != TEXT_NO_SHADOW) {
if (ARGB_GET_A(ctx.shadow)) {
size_t shadow_offset = text->height / 16;
if (shadow_offset < 1) {
shadow_offset = 1;
Expand All @@ -61,7 +58,7 @@ static enum config_status load_config(const char* key, const char* value)
}
} else if (strcmp(key, "shadow") == 0) {
if (strcmp(value, "none") == 0) {
ctx.shadow = TEXT_NO_SHADOW;
ctx.shadow = 0;
status = cfgst_ok;
} else if (config_to_color(value, &ctx.shadow)) {
status = cfgst_ok;
Expand Down

0 comments on commit c2ed948

Please sign in to comment.