Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export(invert_sublists)
export(is_blank)
export(is_boolean)
export(is_curie)
export(is_hex_color)
export(is_invariant)
export(is_missing)
export(is_negative)
Expand Down
98 changes: 94 additions & 4 deletions R/googlesheets.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,98 @@ range_add_dropdown <- function(ss, range, values, msg = "Choose a valid value",
}


# microbenchmark(
# cur_cols %>% purrr::map_dfc(setNames, object = list(logical())),
# cur_cols %>% purrr::map_dfc(~ tibble::tibble(!!.x := logical())),
#' Set Google Sheets Range Fill Color
#'
#' Sets the fill color for one or more ranges in a Google Sheet.
#'
#' @inheritParams curation_template
#' @param ranges A character vector of ranges to set the background color for,
#' as recognized by [googlesheets4::range_flood()].
#'
#' @section NOTE:
#' This function relies on internal functions from the `googlesheets4` package
#' and may break if the package is updated. See examples of
#' [googlesheets4::range_flood()] for reference.
#'
#' @family Google Sheets Formatting functions
#'
#' @keywords internal
set_gs_fill <- function(ss, sheet, ranges, colors) {
stopifnot(
"`ranges` and `colors` must be same length" =
length(ranges) == length(colors),
)
color_mat <- gs_col2rgb(colors)

gs_fill <- purrr::map(
seq_along(ranges),
function(.i) {
googlesheets4:::CellData(
userEnteredFormat = googlesheets4:::new(
"CellFormat",
backgroundColor = googlesheets4:::new(
"Color",
red = color_mat[1, .i] / 255,
green = color_mat[2, .i] / 255,
blue = color_mat[3, .i] / 255
)
)
)
}
)

purrr::walk2(
ranges,
gs_fill,
~ googlesheets4::range_flood(ss, sheet, range = .x, cell = .y)
)
}


#' Convert Google Sheets Colors to RGB
#'
#' Converts one or more Google Sheets color names or hex codes to RGB values.
#'
#' @param colors A character vector of hex codes or one of the following color
#' names from Google Sheets `r paste0(names(gs_color), collapse = ", ")`.
#' @return A 3 x `length(colors)` matrix with `Red`, `Green`, & `Blue` rows in
#' 1 column for each color in `colors`.
#'
#' @family Google Sheets Formatting functions
#'
#' @keywords internal
gs_col2rgb <- function(colors) {
hex <- is_hex_color(colors)
stopifnot(
"`colors` must be one or more hex code(s) or recognized DO.utils:::gs_color name(s)" =
all(hex | colors %in% names(gs_color))
)
color_data <- ifelse(hex, colors, gs_color[colors])

col2rgb(color_data)
}


# )
# Google Sheets colors defined in DO.utils
gs_color <- c(
"red" = "#ff0000",
"orange" = "#ff9900",
"yellow" = "#ffff00",
"green" = "#00ff00",
"cyan" = "#00ffff",
"blue" = "#0000ff",
"purple" = "#9900ff",
"magenta" = "#ff00ff",
"light red 3" = "#f4cccc",
"light orange 3" = "#fce5cd",
"light yellow 3" = "#fff2cc",
"light green 3" = "#d9ead3",
"light cyan 3" = "#d0e0e3",
"light blue 3" = "#cfe2f3",
"light purple 3" = "#d9d2e9",
"light magenta 3" = "#ead1dc",
"white" = "#ffffff",
"black" = "#000000",
"dark grey 1" = "#b7b7b7",
"light grey 2" = "#efefef"
)
15 changes: 15 additions & 0 deletions R/predicates.R
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,21 @@ iff_all_vals <- function(x, values) {
}


#' Test for Hexadecimal Colors
#'
#' Tests whether values in a character vector are valid hexadecimal color codes.
#' Will _NOT_ recognize [abbreviated hex codes](https://en.wikipedia.org/wiki/Web_colors#Shorthand_hexadecimal_form)
#' (e.g. `#fff`).
#'
#' @param x A character vector.
#'
#' @family predicates
#' @export
is_hex_color <- function(x) {
stringr::str_detect(x, "^#([0-9a-fA-F]{2}){3,4}$")
}


# Type tests for internal use only ----------------------------------------

is_vctr_or_df <- function(x) {
Expand Down
1 change: 1 addition & 0 deletions man/all_duplicated.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/char_val_predicates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/gs_col2rgb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/iff_all_vals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/is_curie.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions man/is_hex_color.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/is_invariant.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/is_uri.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/lgl_predicates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/num_val_predicates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/obo_ID_predicates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions man/set_gs_fill.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.