|
9 | 9 |
|
10 | 10 | # devtools::install_github("GuangchuangYu/hexSticker") |
11 | 11 |
|
| 12 | +library(magick) |
| 13 | + |
| 14 | +# 1. Load your background image |
| 15 | +img <- image_read("minpatch_balanced.png") |
| 16 | +info <- image_info(img) |
| 17 | +w <- info$width |
| 18 | +h <- info$height |
| 19 | + |
| 20 | +# 2. Open the magick drawing device |
| 21 | +mask <- image_blank(w, h, color = "transparent") |
| 22 | +img_mask <- image_draw(mask) |
| 23 | + |
| 24 | +# 3. Scale the Hexagon down (80% of image height) |
| 25 | +# This provides a 10% margin at the top and 10% at the bottom |
| 26 | +hex_h <- h * 0.8 |
| 27 | +hex_w <- hex_h * (sqrt(3)/2) |
| 28 | + |
| 29 | +# Calculate the start points to keep it perfectly centered |
| 30 | +start_y <- (h - hex_h) / 2 |
| 31 | +start_x <- (w - hex_w) / 2 |
| 32 | + |
| 33 | +# Precise "Pointy-Topped" coordinates with space around them |
| 34 | +x_coords <- c(w/2, w - start_x, w - start_x, w/2, start_x, start_x) |
| 35 | +y_coords <- c(start_y, start_y + (hex_h * 0.25), start_y + (hex_h * 0.75), h - start_y, start_y + (hex_h * 0.75), start_y + (hex_h * 0.25)) |
| 36 | + |
| 37 | +# 4. Draw the polygon |
| 38 | +polygon(x_coords, y_coords, col = "white", border = NA) |
| 39 | +dev.off() |
| 40 | + |
| 41 | +# 5. Clip and Save |
| 42 | +clipped_img <- image_composite(img_mask, img, operator = "In") |
| 43 | + |
| 44 | +# Trim the excess transparency so the hexSticker package |
| 45 | +# doesn't have to deal with a lot of empty space |
| 46 | +final_img <- image_trim(clipped_img) |
| 47 | +image_write(final_img, "minpatch_centered_hex.png") |
| 48 | + |
| 49 | + |
| 50 | +hexSticker::sticker( |
| 51 | + subplot = "minpatch_centered_hex.png", |
| 52 | + package = "minPatch", |
| 53 | + p_size = 86, |
| 54 | + p_y = 1.2, |
| 55 | + p_color = "#00441b", |
| 56 | + s_x = 1, |
| 57 | + s_y = 1, |
| 58 | + s_width = 0.8, |
| 59 | + s_height = 0.8, |
| 60 | + h_fill = "#FFFFFF", |
| 61 | + h_color = "#00441b", |
| 62 | + filename = "minpatch.png", |
| 63 | + dpi = 1000, |
| 64 | +) |
| 65 | + |
| 66 | +# First, load a nice font (requires sysfonts) |
| 67 | +sysfonts::font_add_google("Montserrat", "montserrat") |
| 68 | +showtext::showtext_auto() |
| 69 | + |
| 70 | +hexSticker::sticker( |
| 71 | + subplot = "minpatch_centered_hex.png", |
| 72 | + package = "minPatch", |
| 73 | + p_size = 86, # Adjusted for showtext/standard scaling |
| 74 | + p_y = 1.15, # Lowered slightly to feel more centered |
| 75 | + p_color = "#00441b", |
| 76 | + p_family = "sans", # Or "montserrat" if loaded |
| 77 | + p_fontface = "bold", # Bold is key for the 'pop' |
| 78 | + s_x = 1, |
| 79 | + s_y = 1, |
| 80 | + s_width = 0.8, # Since we pre-cropped, let's fill the hex |
| 81 | + s_height = 0.8, |
| 82 | + h_fill = "#FFFFFF", |
| 83 | + h_color = "#00441b", # Match border to text for a 'unified' look |
| 84 | + h_size = 1.5, # Slightly thicker border adds 'premium' feel |
| 85 | + spotlight = TRUE, # Adds a subtle radial gradient to the background |
| 86 | + l_alpha = 0.3, # Softness of the spotlight |
| 87 | + filename = "data-raw/minpatch.png", |
| 88 | + dpi = 1000 |
| 89 | +) |
| 90 | + |
| 91 | + |
| 92 | + |
12 | 93 |
|
13 | 94 |
|
14 | 95 |
|
|
0 commit comments