Skip to content

Commit

Permalink
Merge pull request #9 from matt-dray/5-grid
Browse files Browse the repository at this point in the history
Use {grid} and support images
  • Loading branch information
matt-dray authored Jan 6, 2025
2 parents b70d744 + 6a92fea commit c15e4fd
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 447 deletions.
16 changes: 10 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
Package: hexbase
Title: Make a Hex Logo
Title: Create Simple Dependency-Free Hex Logos
Version: 0.0.0.9000
Authors@R:
person("Matt", "Dray", , "mwdray@gmail.com", role = c("aut", "cre"))
Description: A simple, dependency-free way to create a hexagon-shaped logo
to help promote your R package or other projects.
Description: A simple interface to create hexagon-shaped logos that help
promote your R package or other projects. Uses only base R.
License: MIT + file LICENSE
URL: https://github.com/matt-dray/hexbase
BugReports: https://github.com/matt-dray/hexbase/bugs
Imports:
grDevices,
grid
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Encoding: UTF-8
Issues: https://github.com/matt-dray/hexbase
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
37 changes: 37 additions & 0 deletions R/coords.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.get_hex_coords <- function(d = 1) {
a <- d / 2
b <- sqrt(3) * a
list(
x = c(-b, 0, b, b, 0, -b),
y = c(-a, -2 * a, -a, a, 2 * a, a)
)
}

.scale_outer_coords <- function(outer_coord_val) {
num <- outer_coord_val - min(outer_coord_val)
dem <- max(outer_coord_val) - min(outer_coord_val)
num / dem
}

.get_inner_hex_coords_scaled <- function(coords_inner, coords_outer) {

coords_inner_scaled <- vector("list", length = length(coords_inner))
names(coords_inner_scaled) <- c("x", "y")

for (i in seq_along(coords_inner)) {
coords_inner_scaled[[i]] <- .scale_inner_coords(
coords_inner[[i]],
min(coords_outer[[i]]),
max(coords_outer[[i]])
)
}

coords_inner_scaled

}

.scale_inner_coords <- function(inner_val, outer_val_min, outer_val_max) {
num <- inner_val - pmin(min(inner_val), outer_val_min)
dem <- pmax(max(inner_val), outer_val_max) - pmin(min(inner_val), outer_val_min)
num / dem
}
44 changes: 44 additions & 0 deletions R/grobs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.engrob_hex <- function(coords, fill_col) {
grid::polygonGrob(
coords[["x"]],
coords[["y"]],
gp = grid::gpar(fill = fill_col)
)
}

.engrob_text <- function(
text_string,
text_x,
text_y,
text_col,
text_size,
text_font
) {
grid::textGrob(
text_string,
x = grid::unit(text_x, "npc"),
y = grid::unit(text_y, "npc"),
gp = grid::gpar(
col = text_col,
fontsize = text_size,
fontfamily = text_font,
lineheight = 0.75
)
)
}

.engrob_img <- function(
img_object,
img_x,
img_y,
img_width,
img_height
) {
grid::rasterGrob(
img_object,
x = grid::unit(img_x, "npc"),
y = grid::unit(img_y, "npc"),
width = grid::unit(img_width, "npc"),
height = grid::unit(img_height, "npc")
)
}
216 changes: 0 additions & 216 deletions R/hex.R

This file was deleted.

Loading

0 comments on commit c15e4fd

Please # to comment.