Skip to content

Commit dacdbdd

Browse files
committed
Clean up apply_node_color.R, extract color-related code.
* Add `get_color_variants()`, `get_palette()`, and `get_colors()` functions Signed-off-by: Tao Wang <twang2218@gmail.com>
1 parent 8423941 commit dacdbdd

3 files changed

Lines changed: 98 additions & 33 deletions

File tree

R/apply_node_color.R

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,19 @@
1010
#' @importFrom grDevices col2rgb
1111
#' @importFrom grDevices rgb
1212
apply_node_color <- function(p) {
13-
# define material design palette base colors
14-
material_colors <- c(
15-
"blue", "red", "green", "yellow", "brown",
16-
"indigo", "pink", "teal", "amber", "orange",
17-
"purple", "deep-orange", "light-green", "lime", "grey",
18-
"deep-purple", "cyan", "blue-grey", "light-blue"
19-
)
20-
2113
# Make `R CMD check` happy
2214
type <- NULL
2315

2416
types <- get_node_df(p) %>%
2517
distinct(type) %>%
2618
arrange(type)
2719

28-
types <- types$"type"
20+
types <- types$type
2921

30-
# print(paste(types, ":", material_colors[1:length(types)]))
31-
pal <- lapply(
32-
sapply(
33-
sapply(
34-
material_colors[1:length(types)],
35-
pal_material,
36-
reverse = TRUE
37-
),
38-
function(x) { x(1) }
39-
),
40-
function(name, alpha = 1) {
41-
color <- col2rgb(name, alpha = TRUE) / 255
42-
arg <- split(color, rownames(color))
43-
arg$alpha <- alpha
44-
do.call(rgb, arg)
45-
},
46-
# Get 3 transparency level for the given color
47-
c(0.3, 0.5, 1)
48-
)
49-
names(pal) <- types
22+
colors <- get_colors(types)
5023

5124
for (t in types) {
52-
color <- pal[[t]]
25+
color <- colors[[t]]
5326
p <- select_nodes(p, conditions = type == t)
5427
if (!any(is.na(get_selection(p)))) {
5528
# print(paste("type:", t))
@@ -62,4 +35,4 @@ apply_node_color <- function(p) {
6235
}
6336

6437
return(p)
65-
}
38+
}

R/utils.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,53 @@ projection <- function(x, min = 0, max = 1) {
1010

1111
((x - x_min)/(x_max - x_min)) * (max - min) + min
1212
}
13+
14+
#' @importFrom grDevices col2rgb
15+
#' @importFrom grDevices rgb
16+
get_color_variants <- function(name) {
17+
color <- col2rgb(name, alpha = TRUE) / 255
18+
arg <- split(color, rownames(color))
19+
20+
alphas <- c(0.3, 0.5, 1)
21+
result <- c()
22+
for (alpha in alphas) {
23+
arg$alpha <- alpha
24+
result[length(result)+1] <- do.call(rgb, arg)
25+
}
26+
return(result)
27+
}
28+
29+
# define material design palette base colors
30+
material_colors <- c(
31+
"blue", "red", "green", "yellow", "brown",
32+
"indigo", "pink", "teal", "amber", "orange",
33+
"purple", "deep-orange", "light-green", "lime", "grey",
34+
"deep-purple", "cyan", "blue-grey", "light-blue"
35+
)
36+
37+
#' @importFrom ggsci pal_material
38+
get_palette <- function(size) {
39+
# limit the size to the length of `material_colors`
40+
if (size > length(material_colors)) {
41+
size <- length(material_colors)
42+
} else if (size < 1) {
43+
return()
44+
}
45+
46+
# calculate the colors
47+
sapply(
48+
sapply(material_colors[1:size], pal_material, reverse = TRUE),
49+
function(x) { x(1) }
50+
)
51+
}
52+
53+
get_colors <- function(types) {
54+
if (length(types) < 1) {
55+
return(list())
56+
}
57+
58+
colors <- lapply(get_palette(length(types)), get_color_variants)
59+
names(colors) <- types
60+
61+
return(colors)
62+
}

tests/testthat/test_utils.R

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
11
context("utils.R")
22

3-
test_that("projection() should be working correctly", {
3+
test_that("projection()", {
44
expect_equal(projection(1, 10, 20), 15)
55
expect_equal(projection(c(0, 0, 0), 10, 20), 15)
66
# expect_equal(projection(NULL, 10, 20), 15)
77
# expect_equal(projection(NA, 10, 20), 15)
88
expect_equal(projection(c(1, 2), 10, 20), c(10, 20))
99
expect_equal(projection(c(0, 5, 10), 10, 20), c(10, 15, 20))
10-
})
10+
})
11+
12+
test_that("get_palette()", {
13+
expect_equal(length(get_palette(1)), 1)
14+
expect_equal(length(get_palette(5)), 5)
15+
expect_equal(length(get_palette(19)), 19)
16+
# only can get 19 colors, no more
17+
expect_equal(length(get_palette(50)), 19)
18+
19+
expect_equal(names(get_palette(4)), c("blue", "red", "green", "yellow"))
20+
expect_equal(
21+
get_palette(4),
22+
c(
23+
blue = "#0C46A0FF",
24+
red = "#B71B1BFF",
25+
green = "#1A5E1FFF",
26+
yellow = "#F47F17FF"
27+
)
28+
)
29+
})
30+
31+
test_that("get_color_variants()", {
32+
expect_equal(get_color_variants("#123456"), c("#1234564D", "#12345680", "#123456FF"))
33+
})
34+
35+
test_that("get_colors()", {
36+
expect_equal(
37+
get_colors(c("type1")),
38+
list(type1 = c("#0C46A04D", "#0C46A080", "#0C46A0FF"))
39+
)
40+
expect_equal(
41+
get_colors(c("a", "b", "c")),
42+
list(
43+
a = c("#0C46A04D", "#0C46A080", "#0C46A0FF"),
44+
b = c("#B71B1B4D", "#B71B1B80", "#B71B1BFF"),
45+
c = c("#1A5E1F4D", "#1A5E1F80", "#1A5E1FFF")
46+
)
47+
)
48+
49+
# return empty list for invalid input
50+
expect_equal(get_colors(c()), list())
51+
expect_equal(get_colors(NULL), list())
52+
})

0 commit comments

Comments
 (0)