library("colorspace")
library("jsonlite")
library("purrr")
# usage: writeLines(cvd_json(), filename)
# references:
# - colorspace package:
# - Machado et al.:
# given a colorspace matrix-list, return array (of matrices)
cvd_array <- function(cvd_list) {
# we need to transpose because R is column-based,
# we will serialize as rowbased
result <- purrr::reduce(cvd_list, ~c(.x, t(.y)))
result <- array(result, c(3, 3, length(cvd_list)))
result
}
# return a list of arrays (of matrices)
cvd_object <- function() {
obj <- list(
protan = colorspace::protanomaly_cvd,
deuteran = colorspace::deutanomaly_cvd,
tritan = colorspace::tritanomaly_cvd
)
purrr::map(obj, cvd_array)
}
# return a JSON-serialized object
cvd_json <- function(...) {
jsonlite::toJSON(
cvd_object(),
matrix = "columnmajor",
pretty = TRUE,
digits = 6,
...
)
}