|
| 1 | +#' |
| 2 | +#' Create new palettes based on organisational palettes |
| 3 | +#' |
| 4 | +#' These functions apply a similar approach used and demonstrated by |
| 5 | +#' [ColorBrewer](https://colorbrewer2.org) and has been patterned after the |
| 6 | +#' syntax of the `RColorBrewer` package |
| 7 | +#' |
| 8 | +#' @param org Name of organisation. Currently supports only *"acdc"* for the |
| 9 | +#' Africa CDC colour palettes. |
| 10 | +#' @param name Name of the organisational palette to use |
| 11 | +#' @param n Number of colours desired/required. Organisational palettes should |
| 12 | +#' have at least 3 colours and up to 9 colours maximum. All colour schemes are |
| 13 | +#' derived from an organisation's brand/style guidelines. |
| 14 | +#' @param type A character value for type of palette to use. Can be either |
| 15 | +#' *"sequential"*, *"divergent"*, or *"qualitative"*. |
| 16 | +#' |
| 17 | +#' @returns A character vector of desired/required colours with length |
| 18 | +#' equivalent to `n` |
| 19 | +#' |
| 20 | +#' @examples |
| 21 | +#' paleta_create_sequential(n = 5, org = "acdc", name = "blues") |
| 22 | +#' |
| 23 | +#' @rdname create_paleta |
| 24 | +#' @export |
| 25 | +#' |
| 26 | + |
| 27 | +paleta_create_sequential <- function(n, org, name) { |
| 28 | + ## Check if specified palette is found in specified org palette ---- |
| 29 | + paleta_check_colour(name = name, org = org) |
| 30 | + |
| 31 | + ## Check if specified palette is sequential ---- |
| 32 | + paleta_check_type(name = name, pal_type = "sequential") |
| 33 | + |
| 34 | + ## Check if number of colours is compatible with sequential ---- |
| 35 | + if (n < 3) { |
| 36 | + cli::cli_bullets( |
| 37 | + "!" = "Sequential palettes have minimum 3 colours", |
| 38 | + "i" = "Returning 3 colours" |
| 39 | + ) |
| 40 | + |
| 41 | + n <- 3 |
| 42 | + } |
| 43 | + |
| 44 | + if (n > 9) { |
| 45 | + cli::cli_bullets( |
| 46 | + "!" = "Sequential palettes have maximum 9 colours", |
| 47 | + "i" = "Returning 9 colours" |
| 48 | + ) |
| 49 | + |
| 50 | + n <- 9 |
| 51 | + } |
| 52 | + |
| 53 | + ## Get base palette ---- |
| 54 | + pal <- get(paste0(org, "_brewer_palettes"))[[name]] |
| 55 | + |
| 56 | + ## Update palette to n ---- |
| 57 | + pal <- grDevices::colorRampPalette(pal)(n) |
| 58 | + |
| 59 | + ## Create palette class ---- |
| 60 | + class(pal) <- "palette" |
| 61 | + |
| 62 | + ## Return palette ---- |
| 63 | + pal |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +#' |
| 68 | +#' @rdname create_paleta |
| 69 | +#' @export |
| 70 | +#' |
| 71 | +paleta_create_divergent <- function(n, name, org) { |
| 72 | + ## Check if specified palette is found in specified org palette ---- |
| 73 | + paleta_check_colour(name = name, org = org) |
| 74 | + |
| 75 | + ## Check if specified palette is divergent ---- |
| 76 | + paleta_check_type(name = name, pal_type = "divergent") |
| 77 | + |
| 78 | + ## Check if number of colours is compatible with divergent ---- |
| 79 | + if (n < 3) { |
| 80 | + cli::cli_bullets( |
| 81 | + "!" = "Divergent palettes have minimum 3 colours", |
| 82 | + "i" = "Returning 3 colours" |
| 83 | + ) |
| 84 | + |
| 85 | + n <- 3 |
| 86 | + } |
| 87 | + |
| 88 | + if (n > 11) { |
| 89 | + cli::cli_bullets( |
| 90 | + "!" = "Divergent palettes have maximum 11 colours", |
| 91 | + "i" = "Returning 11 colours" |
| 92 | + ) |
| 93 | + |
| 94 | + n <- 11 |
| 95 | + } |
| 96 | + |
| 97 | + ## Get base palette ---- |
| 98 | + pal <- get(paste0(org, "_brewer_palettes"))[[name]] |
| 99 | + |
| 100 | + ## Update palette to n ---- |
| 101 | + pal <- grDevices::colorRampPalette(pal)(n) |
| 102 | + |
| 103 | + ## Create palette class ---- |
| 104 | + class(pal) <- "palette" |
| 105 | + |
| 106 | + ## Return palette ---- |
| 107 | + pal |
| 108 | +} |
| 109 | + |
| 110 | +#' |
| 111 | +#' @rdname create_paleta |
| 112 | +#' @export |
| 113 | +#' |
| 114 | +paleta_create_qualitative <- function(n, name, org) { |
| 115 | + ## Check if specified palette is found in specified org palette ---- |
| 116 | + paleta_check_colour(name = name, org = org) |
| 117 | + |
| 118 | + ## Check if specified palette is divergent ---- |
| 119 | + paleta_check_type(name = name, pal_type = "qualitative") |
| 120 | + |
| 121 | + ## Get base palette ---- |
| 122 | + pal <- get(paste0(org, "_brewer_palettes"))[[name]] |
| 123 | + |
| 124 | + ## Check that n is not more than length(pal) ---- |
| 125 | + if (n > length(pal)) { |
| 126 | + cli::cli_bullets( |
| 127 | + "!" = "{.code n = {n}} is greater than available colours in {name} palette", |
| 128 | + "i" = "Returning all colours in {name} colour palette" |
| 129 | + ) |
| 130 | + |
| 131 | + n <- length(pal) |
| 132 | + } |
| 133 | + |
| 134 | + ## Update palette to n ---- |
| 135 | + pal <- pal[seq_len(n)] |
| 136 | + |
| 137 | + ## Create palette class ---- |
| 138 | + class(pal) <- "palette" |
| 139 | + |
| 140 | + ## Return palette ---- |
| 141 | + pal |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +#' |
| 146 | +#' @rdname create_paleta |
| 147 | +#' @export |
| 148 | +#' |
| 149 | +paleta_create_brewer <- function(n, name, org, |
| 150 | + type = c("sequential", |
| 151 | + "divergent", |
| 152 | + "qualitative")) { |
| 153 | + ## Determine type of palette ---- |
| 154 | + type <- match.arg(type) |
| 155 | + |
| 156 | + pal <- parse( |
| 157 | + text = paste0("paleta_create_", type, "(n = n, name = name, org = org)") |
| 158 | + ) |> |
| 159 | + eval() |
| 160 | + |
| 161 | + ## Return palette ---- |
| 162 | + pal |
| 163 | +} |
| 164 | + |
| 165 | +#' |
| 166 | +#' Palette types |
| 167 | +#' |
| 168 | +#' @keywords internal |
| 169 | +#' |
| 170 | + |
| 171 | +paleta_brewer_types <- list( |
| 172 | + sequential = c( |
| 173 | + "blues", "bugn", "bupu", "gnbu", "greens", "greys", "pubu", "pubugn", |
| 174 | + "purd", "rdpu", "reds", "ylgn", "ylgnbu", "ylorbr", "ylorrd" |
| 175 | + ), |
| 176 | + divergent = c( |
| 177 | + "brbg", "piylgn", "prgn", "puor", "rdbu", "rdgy","rdylbu", "rdylgn" |
| 178 | + ), |
| 179 | + qualitative = c( |
| 180 | + "pastel1", "pastel2", "pastel3", "dark", "light", "bright" |
| 181 | + ) |
| 182 | +) |
| 183 | + |
| 184 | +#' |
| 185 | +#' Check if a colour palette name is from a specified organisation |
| 186 | +#' |
| 187 | +#' @keywords internal |
| 188 | +#' |
| 189 | + |
| 190 | +paleta_check_colour <- function(name, org) { |
| 191 | + x <- get(paste0(org, "_brewer_palettes"))[[name]] |
| 192 | + |
| 193 | + if (is.null(x)) { |
| 194 | + cli::cli_abort( |
| 195 | + "Colour palette {.val {name}} is not a {org} colour palette" |
| 196 | + ) |
| 197 | + } else { |
| 198 | + cli::cli_alert_success( |
| 199 | + "Colour palette {.val {name}} is a {org} colour palette" |
| 200 | + ) |
| 201 | + } |
| 202 | + |
| 203 | + ## Return colour palette ---- |
| 204 | + x |
| 205 | +} |
| 206 | + |
| 207 | +#' |
| 208 | +#' Check if a colour palette is divergent, sequential, or qualitative |
| 209 | +#' |
| 210 | +#' @keywords internal |
| 211 | +#' |
| 212 | + |
| 213 | +paleta_check_type <- function(name, |
| 214 | + pal_type = c("sequential", |
| 215 | + "divergent", |
| 216 | + "qualitative")) { |
| 217 | + pal_type <- match.arg(pal_type) |
| 218 | + |
| 219 | + type_check <- name %in% paleta_brewer_types[[pal_type]] |
| 220 | + |
| 221 | + if (!type_check) { |
| 222 | + cli::cli_abort( |
| 223 | + "{name} is not a {pal_type} colour palette" |
| 224 | + ) |
| 225 | + |
| 226 | + FALSE |
| 227 | + } else { |
| 228 | + cli::cli_alert_success( |
| 229 | + "{name} is a {pal_type} colour palette" |
| 230 | + ) |
| 231 | + |
| 232 | + TRUE |
| 233 | + } |
| 234 | +} |
0 commit comments