Skip to content

Commit f061ad9

Browse files
Merge pull request #103 from katilingban:dev
fix tint and shade functions (fix #97); create brewer functions; fix #99; fix #100; fix #101; fix #102
2 parents 61128d0 + a8bc1dc commit f061ad9

24 files changed

+518
-119
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ License: GPL (>= 3)
1616
Depends:
1717
R (>= 4.1.0)
1818
Imports:
19+
cli,
1920
ggplot2,
2021
stringr,
2122
systemfonts,

NAMESPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ S3method(print,palette)
44
export(acdc_amber)
55
export(acdc_blue)
66
export(acdc_blue_grey)
7+
export(acdc_brewer_palettes)
78
export(acdc_corporate_green)
89
export(acdc_cyan)
910
export(acdc_deep_orange)
@@ -46,6 +47,10 @@ export(nhs_purple)
4647
export(nhs_warm_yellow)
4748
export(nhs_white)
4849
export(nhs_yellow)
50+
export(paleta_create_brewer)
51+
export(paleta_create_divergent)
52+
export(paleta_create_qualitative)
53+
export(paleta_create_sequential)
4954
export(paleta_fonts)
5055
export(set_acdc_font)
5156
export(set_nhs_font)
@@ -97,6 +102,9 @@ export(wb_light_aqua)
97102
export(wb_light_orange)
98103
export(wb_palettes)
99104
export(wb_white)
105+
importFrom(cli,cli_abort)
106+
importFrom(cli,cli_alert_success)
107+
importFrom(cli,cli_bullets)
100108
importFrom(ggplot2,element_blank)
101109
importFrom(ggplot2,element_line)
102110
importFrom(ggplot2,element_rect)

NEWS.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
* Changed Africa CDC colour palettes based on updated communication style guidelines
66

7+
* Created sequential and divergent Africa CDC colour palettes
8+
79
## General updates
810

9-
* refresh `pkgdown` website
10-
* refresh GitHub Actions workflows to include Netlify pull request deployment
11+
* refreshed `pkgdown` website
12+
* refreshed GitHub Actions workflows to include Netlify pull request deployment
1113
* added appropriate `fig.alt` specifications in all package documentation
1214

15+
## Bug fixes
16+
17+
* fixed issue with `tint_colour*()` and `shade_colour*()` functions in which they return the opposite percentage tint or shade of a colour or a set of colours
1318

1419
# paleta (version 0.0.0.9001)
1520

R/paleta.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
#' @importFrom grid unit
1818
#' @importFrom grDevices col2rgb rgb
1919
#' @importFrom graphics rect par image text
20+
#' @importFrom cli cli_abort cli_alert_success cli_bullets
2021
#'
2122
"_PACKAGE"

R/paleta_brewer.R

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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

Comments
 (0)