-
Notifications
You must be signed in to change notification settings - Fork 2
/
palette_goldenBlack.R
154 lines (135 loc) · 5.32 KB
/
palette_goldenBlack.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#' @name gb_cols
#' @title Function to extract colors as hex codes.
#'
#' @param ... A character vector indicating the names of colors. Options includes "gold", "black", "orange", "red", "light gree", "green", "light blue", "blue", "light grey", "grey", and "dark grey".
#'
#' @importFrom grDevices colorRampPalette dev.size
#'
#' @return A character of hex codes.
#'
#' @export
gb_cols <- function(...) {
cols <- c(...)
if (is.null(cols))
return (gb_colors)
gb_colors[cols]
}
gb_colors <- c(
`gold` = "#FFCD00",
`orange` = "#FF8200",
`red` = "#BD472A",
`light green` = "#00AF66",
`green` = "#00664F",
`light blue` = "#00A9E0",
`blue` = "#00558C",
`light grey` = "#BBBCBC",
`dark grey` = "#63666A",
`black` = "#000000"
)
#' @name gb_pal
#' @title Function to interpolate a gold & black color palette.
#'
#' @param palette A character vector indicating the name of palette in gb_palettes. Available palettes:
#' \itemize{
#' \item \code{main}: Gold and black colors.
#' \item \code{tricol}: Gold, black, and dark grey to create a gradual effect.
#' \item \code{digitMixed}: Five-pack colors specified for digital publications.
#' \item \code{printMixed}: Five-pack colors specified for printed publications.
#' \item \code{full}: A palette including all the colors \code{gb_cols} can call.
#' }
#'
#' @param reverse A logic vector indicating whether the palette should be reversed; the default is FALSE.
#' @param ... Additional arguments to pass to \code{colorRampPalette()}
#'
#' @return A function that takes an integer argument (the required number of colors) and returns a character vector of colors interpolating the given sequence.
#'
#' @export
gb_pal <- function(palette = "main",
reverse = FALSE,
...) {
pal <- gb_palettes[[palette]]
if (reverse)
pal <- rev(pal)
colorRampPalette(pal, ...)
}
gb_palettes <- list(
`tricol` = gb_cols("gold", "dark grey", "black"),
`main` = gb_cols("gold", "black"),
`digitMixed` = gb_cols("gold", "red", "green", "blue", "dark grey", "black"),
`printMixed` = gb_cols(
"gold",
"orange",
"light green",
"light blue",
"light grey",
"black"
),
`full` = gb_cols(
"gold",
"orange",
"red",
"light green",
"green",
"light blue",
"blue",
"light grey",
"dark grey",
"black"
)
)
#' @name scale_color_gb
#' @title Fill scale constructors for gold & black colors
#'
#' @param palette A character vector indicating the name of palette in \code{gb_palettes}. Available palettes:
#' \itemize{
#' \item \code{main}: Gold and black colors.
#' \item \code{tricol}: Gold, black, and dark grey to create a gradual effect.
#' \item \code{digitMixed}: Five-pack colors specified for digital publications.
#' \item \code{printMixed}: Five-pack colors specified for printed publications.
#' \item \code{full}: A palette including all the colors \code{gb_cols} can call.
#' }
#' @param discrete A logic vector indicating whether color aesthetic is discrete or not; the default is "main".
#' @param reverse A logic vector indicating whether the palette should be reversed.
#' @param ... Additional arguments passed to \code{discrete_scale()} or \code{scale_color_gradientn()}, used respectively when discrete is TRUE or FALSE
#'
#' @export
scale_color_gb <-
function(palette = "main",
discrete = TRUE,
reverse = FALSE,
...) {
pal <- gb_pal(palette = palette, reverse = reverse)
if (discrete) {
discrete_scale("colour", paste0("gb_", palette), palette = pal, ...)
} else {
scale_color_gradientn(colours = pal(256), ...)
}
}
#' @name scale_fill_gb
#' @title Color scale constructors for gold & black colors
#'
#' @param palette A character vector indicating the name of palette in \code{gb_palettes}. Available palettes:
#' \itemize{
#' \item \code{main}: Gold and black colors.
#' \item \code{tricol}: Gold, black, and dark grey to create a gradual effect.
#' \item \code{digitMixed}: Five-pack colors specified for digital publications.
#' \item \code{printMixed}: Five-pack colors specified for printed publications.
#' \item \code{full}: A palette including all the colors \code{gb_cols} can call.
#' }
#' @param discrete A logic vector indicating whether color aesthetic is discrete or not; the default is "main".
#' @param reverse A logic vector indicating whether the palette should be reversed.
#' @param ... Additional arguments passed to \code{discrete_scale()} or \code{scale_color_gradientn()}, used respectively when discrete is TRUE or FALSE
#'
#' @export
scale_fill_gb <-
function(palette = "main",
discrete = TRUE,
reverse = FALSE,
...) {
pal <- gb_pal(palette = palette, reverse = reverse)
if (discrete) {
discrete_scale("fill", paste0("gb_", palette), palette = pal, ...)
} else {
scale_fill_gradientn(colours = pal(256), ...)
}
}