Skip to content

Commit

Permalink
Speeding up adjustcolor alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Jul 26, 2023
1 parent ce490b4 commit cc0a070
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
11 changes: 11 additions & 0 deletions R/color-paletts.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ colorRamp2 <- function(x, alpha = TRUE, thresholds=NULL) {

}

alphacolor <- function(col, alpha.f) {
val <- strtoi(substring(col, 8L), base = 16L)
substring(col, 8) <- sprintf("%02X", floor(val * alpha.f))
return(col)
}

# microbenchmark::microbenchmark(
# alphacolor("#0000FF80", .5),
# adjustcolor("#0000FF80", .5), times = 1e3, check = "identical"
# )

#' Draw segments colored by gradients
#' @param x,y Coordinates passed to [grDevices::xy.coords].
#' @param col Color ramp function (see [grDevices::colorRamp]).
Expand Down
10 changes: 5 additions & 5 deletions R/coloring.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ new_edge_coloring <- function(
# Applying alpha levels and getting mix
col <- colorRamp2(
x = c(
grDevices::adjustcolor(col = col_i, alpha.f = alpha_i),
grDevices::adjustcolor(col = col_j, alpha.f = alpha_j)
alphacolor(col = col_i, alpha.f = alpha_i),
alphacolor(col = col_j, alpha.f = alpha_j)
)
)(seq(0, 1, length.out = n))

Expand All @@ -47,13 +47,13 @@ new_edge_coloring <- function(
alpha_j <- vertex_alpha_j[j]

# Edge level params
col_ij <- grDevices::adjustcolor(col = edge_color[e], alpha.f = edge_alpha[e])
col_ij <- alphacolor(col = edge_color[e], alpha.f = edge_alpha[e])

# Applying alpha levels and getting mix
col <- colorRamp2(x=
c(
grDevices::adjustcolor(col = col_ij, alpha.f = alpha_i),
grDevices::adjustcolor(col = col_ij, alpha.f = alpha_j)
alphacolor(col = col_ij, alpha.f = alpha_i),
alphacolor(col = col_ij, alpha.f = alpha_j)
)
)(seq(0, 1, length.out = n))

Expand Down
12 changes: 8 additions & 4 deletions R/netplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
#' @noRd
edge_color_mixer <- function(i, j, vcols, p = .5, alpha = .15) {

grDevices::adjustcolor(grDevices::rgb(
colorRamp2(vcols[c(i,j)], alpha = FALSE)(p),
maxColorValue = 255
), alpha = alpha)
alphacolor(
sprintf(
"%sFF", grDevices::rgb(
colorRamp2(vcols[c(i,j)], alpha = FALSE)(p),
maxColorValue = 255
)
), alpha.f = alpha
)

}

Expand Down

0 comments on commit cc0a070

Please sign in to comment.