Skip to content

Commit

Permalink
fix tint and shade functions; create brewer functions; fix #97; fix #99
Browse files Browse the repository at this point in the history
…; fix #100; fix #101; fix #102
  • Loading branch information
ernestguevarra committed Jan 1, 2025
1 parent 4268299 commit a8bc1dc
Show file tree
Hide file tree
Showing 18 changed files with 426 additions and 31 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ License: GPL (>= 3)
Depends:
R (>= 4.1.0)
Imports:
cli,
ggplot2,
stringr,
systemfonts,
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(print,palette)
export(acdc_amber)
export(acdc_blue)
export(acdc_blue_grey)
export(acdc_brewer_palettes)
export(acdc_corporate_green)
export(acdc_cyan)
export(acdc_deep_orange)
Expand Down Expand Up @@ -46,6 +47,10 @@ export(nhs_purple)
export(nhs_warm_yellow)
export(nhs_white)
export(nhs_yellow)
export(paleta_create_brewer)
export(paleta_create_divergent)
export(paleta_create_qualitative)
export(paleta_create_sequential)
export(paleta_fonts)
export(set_acdc_font)
export(set_nhs_font)
Expand Down Expand Up @@ -97,6 +102,9 @@ export(wb_light_aqua)
export(wb_light_orange)
export(wb_palettes)
export(wb_white)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert_success)
importFrom(cli,cli_bullets)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_line)
importFrom(ggplot2,element_rect)
Expand Down
1 change: 1 addition & 0 deletions R/paleta.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
#' @importFrom grid unit
#' @importFrom grDevices col2rgb rgb
#' @importFrom graphics rect par image text
#' @importFrom cli cli_abort cli_alert_success cli_bullets
#'
"_PACKAGE"
234 changes: 234 additions & 0 deletions R/paleta_brewer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
#'
#' Create new palettes based on organisational palettes
#'
#' These functions apply a similar approach used and demonstrated by
#' [ColorBrewer](https://colorbrewer2.org) and has been patterned after the
#' syntax of the `RColorBrewer` package
#'
#' @param org Name of organisation. Currently supports only *"acdc"* for the
#' Africa CDC colour palettes.
#' @param name Name of the organisational palette to use
#' @param n Number of colours desired/required. Organisational palettes should
#' have at least 3 colours and up to 9 colours maximum. All colour schemes are
#' derived from an organisation's brand/style guidelines.
#' @param type A character value for type of palette to use. Can be either
#' *"sequential"*, *"divergent"*, or *"qualitative"*.
#'
#' @returns A character vector of desired/required colours with length
#' equivalent to `n`
#'
#' @examples
#' paleta_create_sequential(n = 5, org = "acdc", name = "blues")
#'
#' @rdname create_paleta
#' @export
#'

paleta_create_sequential <- function(n, org, name) {
## Check if specified palette is found in specified org palette ----
paleta_check_colour(name = name, org = org)

Check warning on line 29 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L29

Added line #L29 was not covered by tests

## Check if specified palette is sequential ----
paleta_check_type(name = name, pal_type = "sequential")

Check warning on line 32 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L32

Added line #L32 was not covered by tests

## Check if number of colours is compatible with sequential ----
if (n < 3) {
cli::cli_bullets(
"!" = "Sequential palettes have minimum 3 colours",
"i" = "Returning 3 colours"
)

Check warning on line 39 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L35-L39

Added lines #L35 - L39 were not covered by tests

n <- 3

Check warning on line 41 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L41

Added line #L41 was not covered by tests
}

if (n > 9) {
cli::cli_bullets(
"!" = "Sequential palettes have maximum 9 colours",
"i" = "Returning 9 colours"
)

Check warning on line 48 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L44-L48

Added lines #L44 - L48 were not covered by tests

n <- 9

Check warning on line 50 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L50

Added line #L50 was not covered by tests
}

## Get base palette ----
pal <- get(paste0(org, "_brewer_palettes"))[[name]]

Check warning on line 54 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L54

Added line #L54 was not covered by tests

## Update palette to n ----
pal <- grDevices::colorRampPalette(pal)(n)

Check warning on line 57 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L57

Added line #L57 was not covered by tests

## Create palette class ----
class(pal) <- "palette"

Check warning on line 60 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L60

Added line #L60 was not covered by tests

## Return palette ----
pal

Check warning on line 63 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L63

Added line #L63 was not covered by tests
}


#'
#' @rdname create_paleta
#' @export
#'
paleta_create_divergent <- function(n, name, org) {
## Check if specified palette is found in specified org palette ----
paleta_check_colour(name = name, org = org)

Check warning on line 73 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L73

Added line #L73 was not covered by tests

## Check if specified palette is divergent ----
paleta_check_type(name = name, pal_type = "divergent")

Check warning on line 76 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L76

Added line #L76 was not covered by tests

## Check if number of colours is compatible with divergent ----
if (n < 3) {
cli::cli_bullets(
"!" = "Divergent palettes have minimum 3 colours",
"i" = "Returning 3 colours"
)

Check warning on line 83 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L79-L83

Added lines #L79 - L83 were not covered by tests

n <- 3

Check warning on line 85 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L85

Added line #L85 was not covered by tests
}

if (n > 11) {
cli::cli_bullets(
"!" = "Divergent palettes have maximum 11 colours",
"i" = "Returning 11 colours"
)

Check warning on line 92 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L88-L92

Added lines #L88 - L92 were not covered by tests

n <- 11

Check warning on line 94 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L94

Added line #L94 was not covered by tests
}

## Get base palette ----
pal <- get(paste0(org, "_brewer_palettes"))[[name]]

Check warning on line 98 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L98

Added line #L98 was not covered by tests

## Update palette to n ----
pal <- grDevices::colorRampPalette(pal)(n)

Check warning on line 101 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L101

Added line #L101 was not covered by tests

## Create palette class ----
class(pal) <- "palette"

Check warning on line 104 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L104

Added line #L104 was not covered by tests

## Return palette ----
pal

Check warning on line 107 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L107

Added line #L107 was not covered by tests
}

#'
#' @rdname create_paleta
#' @export
#'
paleta_create_qualitative <- function(n, name, org) {
## Check if specified palette is found in specified org palette ----
paleta_check_colour(name = name, org = org)

Check warning on line 116 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L116

Added line #L116 was not covered by tests

## Check if specified palette is divergent ----
paleta_check_type(name = name, pal_type = "qualitative")

Check warning on line 119 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L119

Added line #L119 was not covered by tests

## Get base palette ----
pal <- get(paste0(org, "_brewer_palettes"))[[name]]

Check warning on line 122 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L122

Added line #L122 was not covered by tests

## Check that n is not more than length(pal) ----
if (n > length(pal)) {
cli::cli_bullets(
"!" = "{.code n = {n}} is greater than available colours in {name} palette",
"i" = "Returning all colours in {name} colour palette"
)

Check warning on line 129 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L125-L129

Added lines #L125 - L129 were not covered by tests

n <- length(pal)

Check warning on line 131 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L131

Added line #L131 was not covered by tests
}

## Update palette to n ----
pal <- pal[seq_len(n)]

Check warning on line 135 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L135

Added line #L135 was not covered by tests

## Create palette class ----
class(pal) <- "palette"

Check warning on line 138 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L138

Added line #L138 was not covered by tests

## Return palette ----
pal

Check warning on line 141 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L141

Added line #L141 was not covered by tests
}


#'
#' @rdname create_paleta
#' @export
#'
paleta_create_brewer <- function(n, name, org,
type = c("sequential",
"divergent",
"qualitative")) {
## Determine type of palette ----
type <- match.arg(type)

Check warning on line 154 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L154

Added line #L154 was not covered by tests

pal <- parse(
text = paste0("paleta_create_", type, "(n = n, name = name, org = org)")
) |>
eval()

Check warning on line 159 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L156-L159

Added lines #L156 - L159 were not covered by tests

## Return palette ----
pal

Check warning on line 162 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L162

Added line #L162 was not covered by tests
}

#'
#' Palette types
#'
#' @keywords internal
#'

paleta_brewer_types <- list(
sequential = c(
"blues", "bugn", "bupu", "gnbu", "greens", "greys", "pubu", "pubugn",
"purd", "rdpu", "reds", "ylgn", "ylgnbu", "ylorbr", "ylorrd"
),
divergent = c(
"brbg", "piylgn", "prgn", "puor", "rdbu", "rdgy","rdylbu", "rdylgn"
),
qualitative = c(
"pastel1", "pastel2", "pastel3", "dark", "light", "bright"
)
)

#'
#' Check if a colour palette name is from a specified organisation
#'
#' @keywords internal
#'

paleta_check_colour <- function(name, org) {
x <- get(paste0(org, "_brewer_palettes"))[[name]]

Check warning on line 191 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L191

Added line #L191 was not covered by tests

if (is.null(x)) {
cli::cli_abort(
"Colour palette {.val {name}} is not a {org} colour palette"
)

Check warning on line 196 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L193-L196

Added lines #L193 - L196 were not covered by tests
} else {
cli::cli_alert_success(
"Colour palette {.val {name}} is a {org} colour palette"
)

Check warning on line 200 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L198-L200

Added lines #L198 - L200 were not covered by tests
}

## Return colour palette ----
x

Check warning on line 204 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L204

Added line #L204 was not covered by tests
}

#'
#' Check if a colour palette is divergent, sequential, or qualitative
#'
#' @keywords internal
#'

paleta_check_type <- function(name,
pal_type = c("sequential",
"divergent",
"qualitative")) {
pal_type <- match.arg(pal_type)

Check warning on line 217 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L217

Added line #L217 was not covered by tests

type_check <- name %in% paleta_brewer_types[[pal_type]]

Check warning on line 219 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L219

Added line #L219 was not covered by tests

if (!type_check) {
cli::cli_abort(
"{name} is not a {pal_type} colour palette"
)

Check warning on line 224 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L221-L224

Added lines #L221 - L224 were not covered by tests

FALSE

Check warning on line 226 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L226

Added line #L226 was not covered by tests
} else {
cli::cli_alert_success(
"{name} is a {pal_type} colour palette"
)

Check warning on line 230 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L228-L230

Added lines #L228 - L230 were not covered by tests

TRUE

Check warning on line 232 in R/paleta_brewer.R

View check run for this annotation

Codecov / codecov/patch

R/paleta_brewer.R#L232

Added line #L232 was not covered by tests
}
}
53 changes: 53 additions & 0 deletions R/theme_acdc.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,59 @@ acdc_palettes <- list(
acdc_teals = c("#CCE9E6", "#99D3CD", "#66BEB4", "#33A89B", acdc_teal)
)

#'
#' @examples
#' acdc_brewer_palettes
#'
#' @rdname acdc_palette
#' @export
#'
acdc_brewer_palettes <- list(
blues = rev(c(acdc_blue, acdc_blue_grey, acdc_palettes$acdc_blues[2])),
bugn = c(acdc_blue, acdc_lime, acdc_teal),
bupu = c(acdc_blue, acdc_blue_grey, acdc_purple),
gnbu = c(acdc_teal, acdc_lime, acdc_blue),
pubu = c(acdc_purple, acdc_blue_grey, acdc_blue),
pubugn = c(acdc_purple, acdc_blue, acdc_lime),
purd = c(acdc_purple, acdc_plum, acdc_pink),
rdpu = c(acdc_red, acdc_pink, acdc_purple),
ylgn = c(acdc_amber, acdc_teal, acdc_green),
ylgnbu = c(acdc_amber, acdc_lime, acdc_blue),
ylorrd = c(acdc_amber, acdc_deep_orange, acdc_red),
piylgn = c(acdc_pink, acdc_amber, acdc_lime),
prgn = c(acdc_purple, acdc_mauve, acdc_green),
puor = c(acdc_purple, acdc_amber, acdc_deep_orange),
rdbu = c(acdc_red, acdc_plum, acdc_blue),
rdylbu = c(acdc_red, acdc_amber, acdc_blue),
rdylgn = c(acdc_red, acdc_amber, acdc_lime),
pastel1 = c(
acdc_palettes$acdc_blues[3],
acdc_palettes$acdc_plums[3],
acdc_palettes$acdc_blue_grey[3],
acdc_palettes$acdc_amber[3],
acdc_palettes$acdc_cyan[3],
acdc_palettes$acdc_deep_orange[3],
acdc_palettes$acdc_purple[3],
acdc_palettes$acdc_lime[3],
acdc_palettes$acdc_mauve[3],
acdc_palettes$acdc_pink[3],
acdc_palettes$acdc_teal[3]
),
pastel2 = c(
acdc_palettes$acdc_blues[2],
acdc_palettes$acdc_plums[2],
acdc_palettes$acdc_blue_grey[2],
acdc_palettes$acdc_amber[2],
acdc_palettes$acdc_cyan[2],
acdc_palettes$acdc_deep_orange[2],
acdc_palettes$acdc_purple[2],
acdc_palettes$acdc_lime[2],
acdc_palettes$acdc_mauve[2],
acdc_palettes$acdc_pink[2],
acdc_palettes$acdc_teal[2]
)
)


#'
#' Africa CDC fonts
Expand Down
5 changes: 2 additions & 3 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
Acumin
Aleo
CMD
Calibri
CodeFactor
Codecov
ColorBrewer
Frutiger
IBRD
ICSID
Expand All @@ -19,12 +18,12 @@ Rudis
Univers
WBG
WIP
acdc
bl
cmyk
computing's
ernest
guevarra
heirarchical
hrbrthemes
io
organisations’
Expand Down
7 changes: 7 additions & 0 deletions man/acdc_palette.Rd

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

Loading

0 comments on commit a8bc1dc

Please sign in to comment.