Skip to content

Commit

Permalink
gallery: Add config options for border and shadow
Browse files Browse the repository at this point in the history
Allows to set border and shadow colors for selected item.
Relates to #174.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Aug 5, 2024
1 parent cb206ca commit 9c33869
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
24 changes: 14 additions & 10 deletions extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ size = parent
# Viewer mode configuration
################################################################################
[viewer]
# Window background color (RGB/RGBA)
# Window background color (RGBA)
window = #00000000
# Background for transparent images (grid/RGB/RGBA)
# Background for transparent images (grid/RGBA)
transparency = grid
# Default image scale (optimal/fit/width/height/fill/real)
scale = optimal
Expand All @@ -54,12 +54,16 @@ preload = 1
size = 200
# Use anti-aliasing for thumbnails (yes/no)
antialiasing = no
# Background color of the window (RGB/RGBA)
# Background color of the window (RGBA)
window = #00000000
# Background color of the tile (RGB/RGBA)
background = #202020
# Background color of the selected tile (RGB/RGBA)
select = #404040
# Background color of the tile (RGBA)
background = #202020ff
# Background color of the selected tile (RGBA)
select = #404040ff
# Border color of the selected tile (RGBA)
border = #000000ff
# Shadow color of the selected tile (RGBA)
shadow = #000000ff

################################################################################
# Image list configuration
Expand All @@ -82,9 +86,9 @@ all = yes
name = monospace
# Font size (pt)
size = 14
# Font color (RGB/RGBA)
color = #cccccc
# Shadow color (RGB/RGBA)
# Font color (RGBA)
color = #ccccccff
# Shadow color (RGBA)
shadow = #000000a0

################################################################################
Expand Down
7 changes: 5 additions & 2 deletions extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ Background color of the window, default is \fI#00000000\fR.
.IP "\fBbackground\fR = \fI#COLOR\fR"
Background color of the tile, default is \fI#202020ff\fR.
.\" ----------------------------------------------------------------------------
.IP "\fBselect\fR = \fI#COLOR\fR"
Set window background of the selected tile, default is \fI#404040ff\fR.
.IP "\fBborder\fR = \fI#COLOR\fR"
Border color of the selected tile, default is \fI#000000ff\fR.
.\" ----------------------------------------------------------------------------
.IP "\fBshadow\fR = \fI#COLOR\fR"
Shadow color of the selected tile, default is \fI#000000ff\fR.
.\" ****************************************************************************
.\" Image list config section
.\" ****************************************************************************
Expand Down
54 changes: 37 additions & 17 deletions src/gallery.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ struct gallery {
argb_t clr_window; ///< Window background
argb_t clr_background; ///< Tile background
argb_t clr_select; ///< Selected tile background
argb_t clr_border; ///< Selected tile border
argb_t clr_shadow; ///< Selected tile shadow

size_t top; ///< Index of the first displayed image
size_t selected; ///< Index of the selected image
Expand Down Expand Up @@ -142,8 +144,6 @@ static void draw_thumbnail(struct pixmap* window, ssize_t x, ssize_t y,
// currently selected item
const size_t thumb_size = THUMB_SELECTED_SCALE * ctx.thumb_size;
const size_t thumb_offset = (thumb_size - ctx.thumb_size) / 2;
const size_t shadow_width = max(1, thumb_size / 15);
const size_t alpha_step = 0xff / shadow_width;

x = max(0, x - (ssize_t)thumb_offset);
y = max(0, y - (ssize_t)thumb_offset);
Expand All @@ -166,23 +166,33 @@ static void draw_thumbnail(struct pixmap* window, ssize_t x, ssize_t y,
}

// shadow
for (size_t i = 0; i < shadow_width; ++i) {
const ssize_t lx = i + x + thumb_size;
const ssize_t ly = y + shadow_width;
const ssize_t lh = thumb_size - (shadow_width - i);
const argb_t color = ARGB_SET_A(0xff - i * alpha_step);
pixmap_vline(window, lx, ly, lh, color);
}
for (size_t i = 0; i < shadow_width; ++i) {
const ssize_t lx = x + shadow_width;
const ssize_t ly = y + thumb_size + i;
const ssize_t lw = thumb_size - (shadow_width - i) + 1;
const argb_t color = ARGB_SET_A(0xff - i * alpha_step);
pixmap_hline(window, lx, ly, lw, color);
if (ARGB_GET_A(ctx.clr_shadow)) {
const argb_t base = ctx.clr_shadow & 0x00ffffff;
const uint8_t alpha = ARGB_GET_A(ctx.clr_shadow);
const size_t width =
max(1, (double)thumb_size / 15.0 * ((double)alpha / 255.0));
const size_t alpha_step = alpha / width;

for (size_t i = 0; i < width; ++i) {
const ssize_t lx = i + x + thumb_size;
const ssize_t ly = y + width;
const ssize_t lh = thumb_size - (width - i);
const argb_t color = base | ARGB_SET_A(alpha - i * alpha_step);
pixmap_vline(window, lx, ly, lh, color);
}
for (size_t i = 0; i < width; ++i) {
const ssize_t lx = x + width;
const ssize_t ly = y + thumb_size + i;
const ssize_t lw = thumb_size - (width - i) + 1;
const argb_t color = base | ARGB_SET_A(alpha - i * alpha_step);
pixmap_hline(window, lx, ly, lw, color);
}
}

// frame
pixmap_rect(window, x, y, thumb_size, thumb_size, ARGB_SET_A(0xff));
// border
if (ARGB_GET_A(ctx.clr_border)) {
pixmap_rect(window, x, y, thumb_size, thumb_size, ctx.clr_border);
}
}
}

Expand Down Expand Up @@ -534,6 +544,14 @@ static enum config_status load_config(const char* key, const char* value)
if (config_to_color(value, &ctx.clr_select)) {
status = cfgst_ok;
}
} else if (strcmp(key, "border") == 0) {
if (config_to_color(value, &ctx.clr_border)) {
status = cfgst_ok;
}
} else if (strcmp(key, "shadow") == 0) {
if (config_to_color(value, &ctx.clr_shadow)) {
status = cfgst_ok;
}
} else if (strcmp(key, "antialiasing") == 0) {
if (config_to_bool(value, &ctx.thumb_aa)) {
status = cfgst_ok;
Expand All @@ -552,6 +570,8 @@ void gallery_create(void)
ctx.selected = IMGLIST_INVALID;
ctx.clr_background = 0xff202020;
ctx.clr_select = 0xff404040;
ctx.clr_border = 0xff000000;
ctx.clr_shadow = 0xff000000;

// register configuration loader
config_add_loader("gallery", load_config);
Expand Down

0 comments on commit 9c33869

Please sign in to comment.