Skip to content

Commit 681befb

Browse files
committed
cleaning some internals
nothing of substance changes
1 parent 4e77f1a commit 681befb

File tree

2 files changed

+65
-59
lines changed

2 files changed

+65
-59
lines changed

R/bayesplot-colors.R

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,29 @@ plot_scheme <- function(scheme) {
227227
)
228228
}
229229

230+
# Color scheme level names
231+
scheme_level_names <- function() {
232+
c("light",
233+
"light_highlight",
234+
"mid",
235+
"mid_highlight",
236+
"dark",
237+
"dark_highlight")
238+
}
230239

231-
# @param scheme A string (length 1) naming a scheme
240+
#' Return a color scheme based on `scheme` argument specified as a string
241+
#'
242+
#' @noRd
243+
#' @param scheme A string (length 1) naming a scheme
232244
scheme_from_string <- function(scheme) {
233245
stopifnot(length(scheme) == 1)
234246
if (identical(substr(scheme, 1, 4), "mix-")) {
247+
# user specified a mixed scheme (e.g., "mix-blue-red")
235248
to_mix <- unlist(strsplit(scheme, split = "-"))[2:3]
236249
x <- setNames(mixed_scheme(to_mix[1], to_mix[2]), scheme_level_names())
237250
return(structure(x, mixed = TRUE, scheme_name = scheme))
238251
} else if (identical(substr(scheme, 1, 7), "brewer-")) {
252+
# user specified a ColorBrewer scheme (e.g., "brewer-Blues")
239253
if (!requireNamespace("RColorBrewer", quietly = TRUE)) {
240254
stop("Please install the 'RColorBrewer' package to use a ColorBrewer scheme.",
241255
call.=FALSE)
@@ -244,23 +258,41 @@ scheme_from_string <- function(scheme) {
244258
x <- setNames(as.list(clrs), scheme_level_names())
245259
return(structure(x, mixed = FALSE, scheme_name = scheme))
246260
} else {
261+
# check for scheme in master_color_list
247262
scheme <- match.arg(scheme, choices = names(master_color_list))
248-
x <- prepare_colors(scheme)
263+
x <- setNames(master_color_list[[scheme]], scheme_level_names())
249264
return(structure(x, mixed = FALSE, scheme_name = scheme))
250265
}
251266
}
252267

253-
# check if object returned by color_scheme_get is a mixed scheme
254-
# @param x object returned by color_scheme_get
268+
# create mixed scheme from two existing schemes
269+
mixed_scheme <- function(scheme1, scheme2) {
270+
scheme1 <- color_scheme_get(scheme1)
271+
scheme2 <- color_scheme_get(scheme2)
272+
scheme <- unname(list(
273+
scheme1$light,
274+
scheme2$light_highlight,
275+
scheme2$mid,
276+
scheme1$mid_highlight,
277+
scheme1$dark,
278+
scheme2$dark_highlight
279+
))
280+
attr(scheme, "mixed") <- TRUE
281+
return(scheme)
282+
}
283+
284+
#' Check if object returned by color_scheme_get() is a mixed scheme
285+
#' @noRd
286+
#' @param x object returned by color_scheme_get()
287+
#' @return T/F
255288
is_mixed_scheme <- function(x) {
256289
stopifnot(is.list(x))
257290
isTRUE(attr(x, "mixed"))
258291
}
259292

260-
#' Access a subset of the scheme colors
261-
#'
293+
#' Access a subset of the current scheme colors
262294
#' @noRd
263-
#' @param level A character vector of level names (see `scheme_level_names()`).
295+
#' @param level A character vector of level names scheme_level_names().
264296
#' The abbreviations "l", "lh", "m", "mh", "d", and "dh" can also be used
265297
#' instead of the full names.
266298
#' @return A character vector of color values.
@@ -273,6 +305,7 @@ get_color <- function(levels) {
273305
color_vals <- color_scheme_get()[levels]
274306
unlist(color_vals, use.names = FALSE)
275307
}
308+
276309
full_level_name <- function(x) {
277310
switch(x,
278311
l = "light", lh = "light_highlight",
@@ -281,37 +314,28 @@ full_level_name <- function(x) {
281314
)
282315
}
283316

284-
285-
# Color scheme level names
286-
scheme_level_names <- function() {
287-
c("light",
288-
"light_highlight",
289-
"mid",
290-
"mid_highlight",
291-
"dark",
292-
"dark_highlight")
293-
}
294-
295-
prepare_colors <- function(scheme) {
296-
setNames(
297-
master_color_list[[scheme]],
298-
scheme_level_names()
299-
)
300-
}
301-
317+
# Custom color scheme if 6 colors specified
302318
prepare_custom_colors <- function(scheme) {
303-
if (length(scheme) != 6)
319+
if (length(scheme) != 6) {
304320
stop("Custom color schemes must contain exactly 6 colors.",
305321
call. = FALSE)
322+
}
306323

307324
not_found <- character(0)
308325
for (j in seq_along(scheme)) {
309326
clr <- scheme[j]
310327
if (!is_hex_color(clr) && !clr %in% grDevices::colors())
311328
not_found <- c(not_found, clr)
312329
}
313-
if (length(not_found))
314-
STOP_bad_colors(not_found)
330+
if (length(not_found)) {
331+
stop(
332+
"Each color must specified as either a hexidecimal color value ",
333+
"(e.g. '#C79999') or the name of a color (e.g. 'blue'). ",
334+
"The following provided colors were not found: ",
335+
paste(unlist(not_found), collapse = ", "),
336+
call. = FALSE
337+
)
338+
}
315339

316340
x <- setNames(as.list(scheme), scheme_level_names())
317341
attr(x, "scheme_name") <- "custom"
@@ -324,34 +348,8 @@ is_hex_color <- function(x) {
324348
isTRUE(nchar(x) == 7)
325349
}
326350

327-
# @param x character vector of bad color names
328-
STOP_bad_colors <- function(x) {
329-
stop(
330-
"Each color must specified as either a hexidecimal color value ",
331-
"(e.g. '#C79999') or the name of a color (e.g. 'blue'). ",
332-
"The following provided colors were not found: ",
333-
paste(unlist(x), collapse = ", "),
334-
call. = FALSE
335-
)
336-
}
337351

338352
# master color list -------------------------------------------------------
339-
# create mixed scheme
340-
mixed_scheme <- function(scheme1, scheme2) {
341-
scheme1 <- color_scheme_get(scheme1)
342-
scheme2 <- color_scheme_get(scheme2)
343-
scheme <- unname(list(
344-
scheme1$light,
345-
scheme2$light_highlight,
346-
scheme2$mid,
347-
scheme1$mid_highlight,
348-
scheme1$dark,
349-
scheme2$dark_highlight
350-
))
351-
attr(scheme, "mixed") <- TRUE
352-
return(scheme)
353-
}
354-
355353
master_color_list <- list(
356354
blue =
357355
list("#d1e1ec", "#b3cde0", "#6497b1", "#005b96", "#03396c", "#011f4b"),

tests/testthat/test-aesthetics.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ context("Aesthetics")
33

44

55
# color scheme stuff ------------------------------------------------------
6+
7+
prepare_colors_for_test <- function(scheme) {
8+
setNames(
9+
bayesplot:::master_color_list[[scheme]],
10+
bayesplot:::scheme_level_names()
11+
)
12+
}
13+
614
test_that("getting and setting the color scheme works", {
715
color_scheme_set("red")
8-
expect_equivalent(color_scheme_get(), prepare_colors("red"))
9-
expect_named(prepare_colors("blue"), scheme_level_names())
16+
expect_equivalent(color_scheme_get(), prepare_colors_for_test("red"))
17+
expect_named(prepare_colors_for_test("blue"), scheme_level_names())
1018
expect_named(color_scheme_get(), scheme_level_names())
1119
for (clr in names(master_color_list)) {
1220
color_scheme_set(clr)
13-
expect_equivalent(color_scheme_get(), prepare_colors(clr),
21+
expect_equivalent(color_scheme_get(), prepare_colors_for_test(clr),
1422
info = clr)
1523
expect_named(color_scheme_get(), scheme_level_names())
1624
}
@@ -20,7 +28,7 @@ test_that("getting and setting the color scheme works", {
2028
expect_gg(plot(color_scheme_get("mix-blue-green")))
2129

2230
color_scheme_set("blue")
23-
expect_equivalent(color_scheme_get("teal"), prepare_colors("teal"))
31+
expect_equivalent(color_scheme_get("teal"), prepare_colors_for_test("teal"))
2432
})
2533

2634
test_that("color_scheme_get with i argument works", {
@@ -99,7 +107,7 @@ test_that("get_color returns correct color values", {
99107
scheme <- color_scheme_set("green")
100108
levs <- scheme_level_names()
101109

102-
ans <- unlist(prepare_colors("green")[levs], use.names = FALSE)
110+
ans <- unlist(prepare_colors_for_test("green")[levs], use.names = FALSE)
103111
expect_identical(get_color(levs), ans)
104112
for (lev in levs)
105113
expect_identical(get_color(lev), scheme[[lev]], info = lev)

0 commit comments

Comments
 (0)