Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/emilhvitfeldt/hex-stickers

My personal hex sticker
https://github.com/emilhvitfeldt/hex-stickers

hex-stickers r rstats

Last synced: 4 days ago
JSON representation

My personal hex sticker

Awesome Lists containing this project

README

        

---
output: github_document
---

# hex-stickers

[![emilverse](https://img.shields.io/badge/emilverse-packages-blue.svg)](http://github.com/emilhvitfeldt/emilverse)

```{r hexwall, echo=FALSE, message=FALSE, warning=FALSE}
# Rewritten from
# https://github.com/mitchelloharawild/hexwall
# Dependencies
library(magick)
library(purrr)

# path: The path to a folder of hexagon stickers
# sticker_row_size: The number of stickers in the longest row
# sticker_width: The width of each sticker in pixels
# remove_small: Should hexagons smaller than the sticker_width be removed?
# coords: A data.frame of coordinates defining the placement of hexagons
# scale_coords: Should the coordinates be scaled to the hexagon size?
# remove_size: Should hexagons of an abnormal size be removed?
# sort_mode: How should the files be sorted?
library(dplyr)

hexwall <- function(path,
sticker_row_size = 4,
sticker_width = 500,
remove_small = TRUE,
total_stickers = NULL,
remove_size = TRUE,
coords = NULL,
scale_coords = TRUE,
sort_mode = c("filename", "random", "color", "colour")) {
sort_mode <- match.arg(sort_mode)

# Load stickers
sticker_files <- list.files(path)
stickers <- file.path(path, sticker_files) %>%
map(image_read) %>%
map(image_transparent, "white") %>%
map(image_trim) %>%
set_names(sticker_files)

# Low resolution stickers
low_resolution <- function(x) {
remove_small && image_info(x)$width < (sticker_width - 1) / 2
}

low_res <- map_lgl(stickers, low_resolution)

stickers <- map(stickers, image_scale, sticker_width)

# Incorrectly sized stickers
incorrectly_sized <- function(x) {
height <- image_info(x)$height

remove_size &&
height < (median(height) - 2) ||
height > (median(height) + 2)
}

bad_size <- map_lgl(stickers, incorrectly_sized)

# Remove bad stickers
sticker_rm <- low_res | bad_size
stickers <- stickers[!sticker_rm]

if (any(sticker_rm)) {
n_removed <- sum(sticker_rm)
removed <- names(stickers)[n_removed]
cli::cli_warn(
"Automatically removed {n_removed} incompatible stickers: {removed}."
)
}

if (is.null(total_stickers)) {
if (!is.null(coords)) {
total_stickers <- NROW(coords)
} else {
total_stickers <- length(stickers)
}
}

# Coerce sticker sizes
sticker_height <- stickers %>%
map(image_info) %>%
map_dbl("height") %>%
median()
stickers <- stickers %>%
map(image_resize, glue::glue("{sticker_width}x{sticker_height}!"))

# Repeat stickers sorted by file name
stickers <- rep_len(stickers, total_stickers)

if (sort_mode == "random") {
# Randomly arrange stickers
stickers <- sample(c(stickers, sample(stickers, total_stickers - length(stickers), replace = TRUE)))
} else if (sort_mode %in% c("color", "colour")) {
# Sort stickers by colour
sticker_col <- stickers %>%
map(image_resize, "1x1!") %>%
map(image_data) %>%
map(~ paste0("#", paste0(.[, , 1], collapse = ""))) %>%
map(colorspace::hex2RGB) %>%
map(as, "HSV") %>%
map_dbl(~ .@coords[, 1]) %>%
sort(index.return = TRUE) %>%
.$ix

stickers <- stickers[sticker_col]
}

if (is.null(coords)) {
# Arrange rows of stickers into images
n_stickers <- length(stickers)

sticker_col_size <- ceiling(n_stickers / (sticker_row_size - 0.5))

row_lens <- c()

repeat {
if (length(row_lens) %% 2 == 0) {
row_lens <- c(row_lens, sticker_row_size)
} else {
row_lens <- c(row_lens, sticker_row_size - 1)
}

if (sum(row_lens) == n_stickers) {
break
}
if (sum(row_lens) > n_stickers) {
diff <- sum(row_lens) - n_stickers
row_lens[length(row_lens)] <- row_lens[length(row_lens)] - diff
break
}
}

sticker_rows <- map2(
row_lens, cumsum(row_lens),
~ seq(.y - .x + 1, by = 1, length.out = .x)
) %>%
map(~ stickers[.x] %>%
invoke(c, .) %>%
image_append())

# Add stickers to canvas
canvas <- image_blank(
sticker_row_size * sticker_width,
sticker_height + (sticker_col_size - 1) * sticker_height / 1.33526, "white"
)
reduce2(sticker_rows, seq_along(sticker_rows),
~ image_composite(
..1, ..2,
offset = paste0("+", ((..3 - 1) %% 2) * sticker_width / 2, "+", round((..3 - 1) * sticker_height / 1.33526))
),
.init = canvas
)
} else {
sticker_pos <- coords
if (scale_coords) {
sticker_pos <- sticker_pos %>%
as_tibble() %>%
mutate_all(function(x) {
x <- x - min(x)
dx <- diff(sort(abs(x)))
x / min(dx[dx != 0])
}) %>%
mutate(y = y / min(diff(y)[diff(y) != 0])) %>%
mutate(
x = x * sticker_width / 2,
y = abs(y - max(y)) * sticker_height / 1.33526
)
}

# Add stickers to canvas
canvas <- image_blank(
max(sticker_pos$x) + sticker_width,
max(sticker_pos$y) + sticker_height, "white"
)
reduce2(stickers, sticker_pos %>% split(1:NROW(.)),
~ image_composite(
..1, ..2,
offset = paste0("+", ..3$x, "+", ..3$y)
),
.init = canvas
)
}
}

hexwall("PNG")
```

All the hex logos as either [PNG](PNG/) or [SVG](SVG/).

All stickers are under with [CC0](LICENSE.md).