Changed name of set_text_color() to choose_text_color() to avoid confusion with the global option setter functions, e.g. set_text_size(). Updated tests to reflect modified threshold.pull/18/merge
| if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1) | if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1) | ||||
| if (!".textcolor" %in% names(d)) | if (!".textcolor" %in% names(d)) | ||||
| d <- d %>% mutate(.textcolor = set_text_color(.color)) | |||||
| d <- d %>% mutate(.textcolor = choose_text_color(.color)) | |||||
| if (".id_long" %in% names(d)) { | if (".id_long" %in% names(d)) { | ||||
| d <- d %>% mutate(.item_id = paste(.id_long, .col, sep = "-")) | d <- d %>% mutate(.item_id = paste(.id_long, .col, sep = "-")) |
| .textcolor = text_color) | .textcolor = text_color) | ||||
| if (is.na(text_color)) | if (is.na(text_color)) | ||||
| res <- res %>% mutate(.textcolor = set_text_color(.color)) | |||||
| res <- res %>% mutate(.textcolor = choose_text_color(.color)) | |||||
| return(res) | return(res) | ||||
| } | } |
| `%||%` <- function(x, y) if (is.null(x)) y else x | `%||%` <- function(x, y) if (is.null(x)) y else x | ||||
| set_text_color <- function(x, black = "#000000", white = "#FFFFFF") { | |||||
| choose_text_color <- function(x, black = "#000000", white = "#FFFFFF") { | |||||
| # x = color_hex | # x = color_hex | ||||
| color_rgb <- col2rgb(x) | color_rgb <- col2rgb(x) | ||||
| # modified from https://stackoverflow.com/a/3943023/2022615 | # modified from https://stackoverflow.com/a/3943023/2022615 |
| context("test-set_text_color") | |||||
| test_that("correct color selection", { | |||||
| colors <- c("#FFFFFF", scales::brewer_pal("seq", "Set1")(4), "#000000") | |||||
| expect_equal(set_text_color(colors), c("#000000", rep("#FFFFFF", 5))) | |||||
| }) |
| context("test-set_text_color") | |||||
| test_that("correct color selection", { | |||||
| colors <- c("#FFFFFF", "#9E788C", "#B679E5", "#4BB757", | |||||
| "#000000", "#0027D8", "#E6071B", "#495B3F") | |||||
| expect_equal(set_text_color(colors), c(rep("#000000", 4), rep("#FFFFFF", 4))) | |||||
| }) |