@@ -227,15 +227,29 @@ plot_scheme <- function(scheme) {
227
227
)
228
228
}
229
229
230
+ # Color scheme level names
231
+ scheme_level_names <- function () {
232
+ c(" light" ,
233
+ " light_highlight" ,
234
+ " mid" ,
235
+ " mid_highlight" ,
236
+ " dark" ,
237
+ " dark_highlight" )
238
+ }
230
239
231
- # @param scheme A string (length 1) naming a scheme
240
+ # ' Return a color scheme based on `scheme` argument specified as a string
241
+ # '
242
+ # ' @noRd
243
+ # ' @param scheme A string (length 1) naming a scheme
232
244
scheme_from_string <- function (scheme ) {
233
245
stopifnot(length(scheme ) == 1 )
234
246
if (identical(substr(scheme , 1 , 4 ), " mix-" )) {
247
+ # user specified a mixed scheme (e.g., "mix-blue-red")
235
248
to_mix <- unlist(strsplit(scheme , split = " -" ))[2 : 3 ]
236
249
x <- setNames(mixed_scheme(to_mix [1 ], to_mix [2 ]), scheme_level_names())
237
250
return (structure(x , mixed = TRUE , scheme_name = scheme ))
238
251
} else if (identical(substr(scheme , 1 , 7 ), " brewer-" )) {
252
+ # user specified a ColorBrewer scheme (e.g., "brewer-Blues")
239
253
if (! requireNamespace(" RColorBrewer" , quietly = TRUE )) {
240
254
stop(" Please install the 'RColorBrewer' package to use a ColorBrewer scheme." ,
241
255
call. = FALSE )
@@ -244,23 +258,41 @@ scheme_from_string <- function(scheme) {
244
258
x <- setNames(as.list(clrs ), scheme_level_names())
245
259
return (structure(x , mixed = FALSE , scheme_name = scheme ))
246
260
} else {
261
+ # check for scheme in master_color_list
247
262
scheme <- match.arg(scheme , choices = names(master_color_list ))
248
- x <- prepare_colors( scheme )
263
+ x <- setNames( master_color_list [[ scheme ]], scheme_level_names() )
249
264
return (structure(x , mixed = FALSE , scheme_name = scheme ))
250
265
}
251
266
}
252
267
253
- # check if object returned by color_scheme_get is a mixed scheme
254
- # @param x object returned by color_scheme_get
268
+ # create mixed scheme from two existing schemes
269
+ mixed_scheme <- function (scheme1 , scheme2 ) {
270
+ scheme1 <- color_scheme_get(scheme1 )
271
+ scheme2 <- color_scheme_get(scheme2 )
272
+ scheme <- unname(list (
273
+ scheme1 $ light ,
274
+ scheme2 $ light_highlight ,
275
+ scheme2 $ mid ,
276
+ scheme1 $ mid_highlight ,
277
+ scheme1 $ dark ,
278
+ scheme2 $ dark_highlight
279
+ ))
280
+ attr(scheme , " mixed" ) <- TRUE
281
+ return (scheme )
282
+ }
283
+
284
+ # ' Check if object returned by color_scheme_get() is a mixed scheme
285
+ # ' @noRd
286
+ # ' @param x object returned by color_scheme_get()
287
+ # ' @return T/F
255
288
is_mixed_scheme <- function (x ) {
256
289
stopifnot(is.list(x ))
257
290
isTRUE(attr(x , " mixed" ))
258
291
}
259
292
260
- # ' Access a subset of the scheme colors
261
- # '
293
+ # ' Access a subset of the current scheme colors
262
294
# ' @noRd
263
- # ' @param level A character vector of level names (see ` scheme_level_names()` ).
295
+ # ' @param level A character vector of level names scheme_level_names().
264
296
# ' The abbreviations "l", "lh", "m", "mh", "d", and "dh" can also be used
265
297
# ' instead of the full names.
266
298
# ' @return A character vector of color values.
@@ -273,6 +305,7 @@ get_color <- function(levels) {
273
305
color_vals <- color_scheme_get()[levels ]
274
306
unlist(color_vals , use.names = FALSE )
275
307
}
308
+
276
309
full_level_name <- function (x ) {
277
310
switch (x ,
278
311
l = " light" , lh = " light_highlight" ,
@@ -281,37 +314,28 @@ full_level_name <- function(x) {
281
314
)
282
315
}
283
316
284
-
285
- # Color scheme level names
286
- scheme_level_names <- function () {
287
- c(" light" ,
288
- " light_highlight" ,
289
- " mid" ,
290
- " mid_highlight" ,
291
- " dark" ,
292
- " dark_highlight" )
293
- }
294
-
295
- prepare_colors <- function (scheme ) {
296
- setNames(
297
- master_color_list [[scheme ]],
298
- scheme_level_names()
299
- )
300
- }
301
-
317
+ # Custom color scheme if 6 colors specified
302
318
prepare_custom_colors <- function (scheme ) {
303
- if (length(scheme ) != 6 )
319
+ if (length(scheme ) != 6 ) {
304
320
stop(" Custom color schemes must contain exactly 6 colors." ,
305
321
call. = FALSE )
322
+ }
306
323
307
324
not_found <- character (0 )
308
325
for (j in seq_along(scheme )) {
309
326
clr <- scheme [j ]
310
327
if (! is_hex_color(clr ) && ! clr %in% grDevices :: colors())
311
328
not_found <- c(not_found , clr )
312
329
}
313
- if (length(not_found ))
314
- STOP_bad_colors(not_found )
330
+ if (length(not_found )) {
331
+ stop(
332
+ " Each color must specified as either a hexidecimal color value " ,
333
+ " (e.g. '#C79999') or the name of a color (e.g. 'blue'). " ,
334
+ " The following provided colors were not found: " ,
335
+ paste(unlist(not_found ), collapse = " , " ),
336
+ call. = FALSE
337
+ )
338
+ }
315
339
316
340
x <- setNames(as.list(scheme ), scheme_level_names())
317
341
attr(x , " scheme_name" ) <- " custom"
@@ -324,34 +348,8 @@ is_hex_color <- function(x) {
324
348
isTRUE(nchar(x ) == 7 )
325
349
}
326
350
327
- # @param x character vector of bad color names
328
- STOP_bad_colors <- function (x ) {
329
- stop(
330
- " Each color must specified as either a hexidecimal color value " ,
331
- " (e.g. '#C79999') or the name of a color (e.g. 'blue'). " ,
332
- " The following provided colors were not found: " ,
333
- paste(unlist(x ), collapse = " , " ),
334
- call. = FALSE
335
- )
336
- }
337
351
338
352
# master color list -------------------------------------------------------
339
- # create mixed scheme
340
- mixed_scheme <- function (scheme1 , scheme2 ) {
341
- scheme1 <- color_scheme_get(scheme1 )
342
- scheme2 <- color_scheme_get(scheme2 )
343
- scheme <- unname(list (
344
- scheme1 $ light ,
345
- scheme2 $ light_highlight ,
346
- scheme2 $ mid ,
347
- scheme1 $ mid_highlight ,
348
- scheme1 $ dark ,
349
- scheme2 $ dark_highlight
350
- ))
351
- attr(scheme , " mixed" ) <- TRUE
352
- return (scheme )
353
- }
354
-
355
353
master_color_list <- list (
356
354
blue =
357
355
list (" #d1e1ec" , " #b3cde0" , " #6497b1" , " #005b96" , " #03396c" , " #011f4b" ),
0 commit comments