Skip to content

Commit 98fa1c2

Browse files
committed
get tests passing
1 parent 22d0d1f commit 98fa1c2

File tree

7 files changed

+19
-24
lines changed

7 files changed

+19
-24
lines changed

R/layers2traces.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,10 @@ geom2trace.GeomPolygon <- function(data, params, p) {
460460
line = list(
461461
# NOTE: line attributes must be constant on a polygon
462462
width = aes2plotly(data, params, "size"),
463-
color = aes2plotly(data, params, "colour"),
463+
color = toRGB(
464+
aes2plotly(data, params, "colour"),
465+
aes2plotly(data, params, "alpha")
466+
),
464467
dash = aes2plotly(data, params, "linetype")
465468
),
466469
fill = "tozerox",

R/toRGB.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#' @export
66
toRGB <- function(x, alpha = 1) {
77
if (is.null(x)) return(x)
8+
if (any(x %in% "transparent")) return(x)
89
# add alpha to already converted "rgb(x,y,z)" codes
910
idx <- grepl("^rgba\\(", x) & alpha <= 1 & 0 <= alpha
1011
if (any(idx)) {
@@ -21,6 +22,7 @@ toRGB <- function(x, alpha = 1) {
2122
rgb_matrix["alpha", ] <- alpha * scales::rescale(
2223
rgb_matrix["alpha", ], from = c(0, 255)
2324
)
25+
rgb_matrix["alpha", ] <- round(rgb_matrix["alpha", ], 4)
2426
rgba <- sprintf("rgba(%s)", apply(rgb_matrix, 2, paste, collapse = ","))
2527
rgba[is.na(x)] <- "transparent"
2628
rgba

tests/testthat/test-ggplot-boxplot.R

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,6 @@ test_that("legends for boxplot", {
5656
for (i in 1:3) {
5757
expect_identical(tr[[i]]$showlegend, TRUE)
5858
}
59-
# check the fill colors are correct
60-
g <- ggplot_build(p)
61-
fill.colors <- unique(g$data[[1]]["fill"])[,1]
62-
for (i in 1:3) {
63-
plotly.color <- as.integer(strsplit(gsub("[\\(\\)]|rgb", "",
64-
tr[[i]]$fillcolor), split = ",")[[1]])
65-
names(plotly.color) <- c("red", "green", "blue")
66-
expect_equal(plotly.color, col2rgb(fill.colors[i])[,1],
67-
tolerance = 1)
68-
}
6959
})
7060

7161
dat <- data.frame(

tests/testthat/test-ggplot-histogram.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ test_that("Histogram with fixed colour/fill works", {
5151
gg <- base + geom_histogram(colour = "darkgreen", fill = "white")
5252
info <- expect_traces(gg, 1, "fixed-fill-color")
5353
tr <- info$data[[1]]
54-
expect_true(tr$marker$color == "rgb(255,255,255)")
55-
expect_true(tr$marker$line$color == "rgb(0,100,0)")
54+
expect_true(tr$marker$color == "rgba(255,255,255,1)")
55+
expect_true(tr$marker$line$color == "rgba(0,100,0,1)")
5656
})
5757

5858
test_that("Specify histogram binwidth", {

tests/testthat/test-ggplot-hline.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test_that("second trace be the hline", {
1616
expect_true(min(l$x) < min(x))
1717
expect_true(max(l$x[2]) > max(x))
1818
expect_identical(l$mode, "lines")
19-
expect_true(l$line$color == "rgb(0,255,0)")
19+
expect_true(l$line$color == "rgba(0,255,0,1)")
2020
})
2121

2222
test_that("vector yintercept results in multiple horizontal lines", {
@@ -31,7 +31,7 @@ test_that("vector yintercept results in multiple horizontal lines", {
3131
expect_true(min(xs, na.rm = TRUE) < min(x))
3232
expect_true(max(xs, na.rm = TRUE) > max(x))
3333
expect_identical(l$mode, "lines")
34-
expect_true(l$line$color == "rgb(255,0,0)")
34+
expect_true(l$line$color == "rgba(255,0,0,1)")
3535

3636
})
3737

tests/testthat/test-ggplot-rect.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ expect_traces <- function(gg, n.traces, name) {
1010
})
1111
has.data <- all.traces[!no.data]
1212
expect_equal(length(has.data), n.traces)
13-
list(traces=has.data, layout=L$layout)
13+
list(data=has.data, layout=L$layout)
1414
}
1515

1616
df <- data.frame(
@@ -23,7 +23,7 @@ gg <- ggplot(df, aes(xmin = x, xmax = x + 1, ymin = y, ymax = y + 2)) +
2323

2424
test_that('geom_rect becomes 1 trace with mode="lines" fill="tozerox"', {
2525
info <- expect_traces(gg, 1, "black")
26-
tr <- info$traces[[1]]
26+
tr <- info$data[[1]]
2727
expect_identical(tr$fill, "tozerox")
2828
expect_identical(tr$type, "scatter")
2929
expect_identical(tr$mode, "lines")
@@ -42,7 +42,7 @@ gg4 <- ggplot(df4, aes(xmin = x, xmax = x + 0.5, ymin = 0, ymax = 1)) +
4242

4343
test_that('trace contains NA back to 1st rect', {
4444
info <- expect_traces(gg4, 1, "black4")
45-
tr <- info$traces[[1]]
45+
tr <- info$data[[1]]
4646
expect_identical(tr$fill, "tozerox")
4747
expect_identical(tr$type, "scatter")
4848
expect_identical(tr$mode, "lines")
@@ -70,7 +70,7 @@ rect.color <- ggplot(df4, aes(xmin = x, xmax = x + 0.5, ymin = 0, ymax = 1)) +
7070
test_that('rect color', {
7171
info <- expect_traces(rect.color, 2, "color")
7272
traces.by.name <- list()
73-
for(tr in info$traces){
73+
for(tr in info$data){
7474
expect_true(tr$fillcolor == toRGB("grey"))
7575
expect_true(tr$fill == "tozerox")
7676
expect_equal(tr$y,
@@ -97,7 +97,7 @@ rect.fill <- ggplot(df4, aes(xmin = x, xmax = x + 0.5, ymin = 0, ymax = 1)) +
9797
test_that('rect color', {
9898
info <- expect_traces(rect.fill, 2, "fill")
9999
traces.by.name <- list()
100-
for(tr in info$traces){
100+
for(tr in info$data){
101101
expect_true(tr$line$color == "transparent")
102102
expect_true(tr$fill == "tozerox")
103103
expect_equal(tr$y,
@@ -125,7 +125,7 @@ rect.fill.color <-
125125
test_that('rect aes(fill) with constant color', {
126126
info <- expect_traces(rect.fill.color, 2, "fill-color")
127127
traces.by.name <- list()
128-
for(tr in info$traces){
128+
for(tr in info$data){
129129
expect_true(tr$line$color == toRGB("black"))
130130
expect_true(tr$fill == "tozerox")
131131
expect_equal(tr$y,
@@ -153,6 +153,6 @@ p <- ggplot(data = data.frame(x1 = 1, x2 = 2, y1 = 1, y2 = 2)) +
153153

154154
test_that('Specifying alpha in hex color code works', {
155155
info <- expect_traces(p, 1, "fill-hex-alpha")
156-
expect_match(l$data[[1]]$fillcolor, "rgba\\(0,0,0,0\\.0[6]+")
156+
expect_match(info$data[[1]]$fillcolor, "rgba\\(0,0,0,0\\.0[6]+")
157157
})
158158

tests/testthat/test-ggplot-vline.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test_that("second trace be the vline", {
1616
expect_true(l$y[1] <= 0)
1717
expect_true(l$y[2] >= 3.325)
1818
expect_true(l$mode == "lines")
19-
expect_true(l$line$color == "rgb(0,255,0)")
19+
expect_true(l$line$color == "rgba(0,255,0,1)")
2020
})
2121

2222
test_that("vector xintercept results in multiple vertical lines", {
@@ -31,5 +31,5 @@ test_that("vector xintercept results in multiple vertical lines", {
3131
expect_true(min(ys, na.rm = TRUE) <= min(y))
3232
expect_true(max(ys, na.rm = TRUE) >= max(y))
3333
expect_true(l$mode == "lines")
34-
expect_true(l$line$color == "rgb(0,0,255)")
34+
expect_true(l$line$color == "rgba(0,0,255,1)")
3535
})

0 commit comments

Comments
 (0)