Skip to content

Commit 956e693

Browse files
committed
ensure alpha can be applied recursively in toRGB
1 parent f53e52a commit 956e693

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

R/test-toRGB.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
context("toRGB")
2+
3+
test_that("Can apply alpha recursively with toRGB()", {
4+
col <- toRGB(toRGB("black", 0.9), 0.9)
5+
expect_match(col, "rgba\\(0,0,0,0\\.80")
6+
})

R/toRGB.R

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,35 @@
44
#' @return hexadecimal colour value (if is.na(x), return "transparent" for compatibility with Plotly)
55
#' @export
66
toRGB <- function(x, alpha = 1) {
7+
if (is.null(x)) return(x)
78
# add alpha to already converted "rgb(x,y,z)" codes
8-
idx <- grepl("^rgb\\(", x) & alpha < 1 & 0 < alpha
9+
idx <- grepl("^rgba\\(", x) & alpha <= 1 & 0 <= alpha
910
if (any(idx)) {
10-
x[idx] <- sub("^rgb", "rgba", x[idx])
11-
x[idx] <- paste0(sub("\\)", ",", x[idx]), alpha, ")")
11+
x[idx] <- rgb2hex(x[idx])
1212
}
13-
# return code if
14-
if (any(is.null(x) || grepl("^rgb[a]?\\(", x))) return(x)
1513
# for some reason ggplot2 has "NA" in some place (instead of NA)
1614
if (is.character(x)) {
1715
x[x == "NA"] <- NA
1816
}
1917
# as of ggplot2 version 1.1, an NA alpha is treated as though it's 1
2018
alpha[is.na(alpha)] <- 1
21-
rgb_matrix <- col2rgb(x, alpha = TRUE)
19+
rgb_matrix <- grDevices::col2rgb(x, alpha = TRUE)
2220
# multiply the existing alpha with specified alpha (both on 0-1 scale)
2321
rgb_matrix["alpha", ] <- alpha * scales::rescale(
2422
rgb_matrix["alpha", ], from = c(0, 255)
2523
)
26-
container <- ifelse(rgb_matrix["alpha", ] == 1, "rgb(%s)", "rgba(%s)")
27-
rgba <- sprintf(container, apply(rgb_matrix, 2, paste, collapse = ","))
28-
rgba <- sub(",1\\)", ")", rgba)
24+
rgba <- sprintf("rgba(%s)", apply(rgb_matrix, 2, paste, collapse = ","))
2925
rgba[is.na(x)] <- "transparent"
3026
rgba
3127
}
28+
29+
# take a 'plotly color' and produce a hex code
30+
rgb2hex <- function(string = "rgba(255,255,255,1)") {
31+
vals <- sub("rgba\\(", "", sub("\\)", "", string))
32+
valz <- strsplit(vals, ",")
33+
sapply(valz, function(x) {
34+
x <- setNames(as.numeric(x), c("red", "green", "blue", "alpha"))
35+
x[["alpha"]] <- x[["alpha"]] * 255
36+
do.call(grDevices::rgb, c(x, list(maxColorValue = 255)))
37+
})
38+
}

tests/testthat/test-ggplot-rect.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,14 @@ test_that('rect aes(fill) with constant color', {
145145
expect_false(traces.by.name[[1]]$fillcolor ==
146146
traces.by.name[[2]]$fillcolor)
147147
})
148+
149+
150+
p <- ggplot(data = data.frame(x1 = 1, x2 = 2, y1 = 1, y2 = 2)) +
151+
geom_rect(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2),
152+
fill = "#00000011", color = "black")
153+
154+
test_that('Specifying alpha in hex color code works', {
155+
info <- expect_traces(p, 1, "fill-hex-alpha")
156+
expect_match(l$data[[1]]$fillcolor, "rgba\\(0,0,0,0\\.0[6]+")
157+
})
158+

0 commit comments

Comments
 (0)