Просмотр исходного кода

more informative error messages when using non-hex CSS colors

tags/v0.3.0
Garrick Aden-Buie 6 лет назад
Родитель
Сommit
139fc4b6a2
2 измененных файлов: 26 добавлений и 2 удалений
  1. +11
    -2
      R/color.R
  2. +15
    -0
      tests/testthat/test-ggplot2.R

+ 11
- 2
R/color.R Просмотреть файл

} }


full_length_hex <- function(x) { full_length_hex <- function(x) {
varname <- substitute(x)
stop_not_hex <- function() {
stop(str_wrap(
"`", deparse(varname), "` is not a hexadecimal color: ", x, ". ",
"If you used valid CSS colors in your xaringan theme, please convert ",
"these colors to hexadecimal form, as this is the format required by ",
"ggplot2."
), call. = FALSE)
}
if (!grepl("^#", x) || grepl("[^#0-9a-fA-F]", x)) { if (!grepl("^#", x) || grepl("[^#0-9a-fA-F]", x)) {
stop(paste0('"', x, '" is not a hexadecimal color'))
stop_not_hex()
} }
x <- sub("^#", "", x) x <- sub("^#", "", x)
if (nchar(x) == 3) { if (nchar(x) == 3) {
x <- rep(x, each = 2) x <- rep(x, each = 2)
x <- paste(x, collapse = "") x <- paste(x, collapse = "")
} else if (nchar(x) != 6) { } else if (nchar(x) != 6) {
stop(paste0('"', x, '" is not a hexadecimal color'))
stop_not_hex()
} }
paste0("#", x) paste0("#", x)
} }

+ 15
- 0
tests/testthat/test-ggplot2.R Просмотреть файл

expect_equal(theme$plot.background$colour, "#000022") expect_equal(theme$plot.background$colour, "#000022")
expect_equal(theme$plot.background$fill, "#000022") expect_equal(theme$plot.background$fill, "#000022")
}) })

it("throws error for non-hex color functions", {
expect_error(
with_clean_session(function() {
xaringanthemer::style_xaringan(
text_color = "rgb(0, 0, 0)",
outfile = NULL
)
xaringanthemer::theme_xaringan(
text_font_use_google = FALSE,
title_font_use_google = FALSE
)
})
)
})
}) })


describe("theme_xaringan_inverse()", { describe("theme_xaringan_inverse()", {

Загрузка…
Отмена
Сохранить