grepal returns a vector of colors whose names match a regular expression (regex).

grepal(pattern, x = colors(), ignore_case = TRUE)

Arguments

pattern

A regular expression (specified as a string/character object).

x

A vector of R color names or a data frame of named colors (i.e., whose names can be searched). Default: x = colors().

ignore_case

Should the case of pattern be ignored (passed to ignore.case of the grep function)? Default: ignore_case = TRUE.

Details

By default, the base R vector of named colors (i.e., colors()) is searched for names matching a pattern (which can be a simple string or regular expression).

If x (i.e., the object to be searched) is provided, it is must be a vector of color names or a data frame of named color objects (i.e., a color palette).

This function facilitates searching colors by name and yields (a vector of) colors of similar color hue (provided that the color's hue is expressed in a color's name). Its name grepal is an abbreviation of grep and "pal".

See also

seepal to plot color palettes; usecol to use a color palette; defpal to define new color palettes; shades_of to define shades of a given color; pal_unikn for the default uni.kn color palette.

Other color functions: newpal(), seecol(), shades_of(), usecol()

Examples

grepal("cyan")
#> [1] "cyan" "cyan1" "cyan2" "cyan3" "cyan4" #> [6] "darkcyan" "lightcyan" "lightcyan1" "lightcyan2" "lightcyan3" #> [11] "lightcyan4"
# With regular expressions: some_grey <- grepal("gr(a|e)y") start_grey <- grepal("^gr(a|e)y") only_grey <- grepal("^gr(a|e)y$") length(some_grey)
#> [1] 224
length(only_grey)
#> [1] 2
# With other color objects (df as x): grepal("blau", x = pal_unikn)
#> seeblau5 seeblau4 seeblau3 seeblau2 seeblau1 #> 1 #008ECE #00A9E0 #59C7EB #A6E1F4 #CCEEF9
grepal("SEE", x = pal_unikn_pref)
#> Seeblau Seegruen #> Seeblau #59C7EB #0A9086
# Applications: seecol(grepal("white"), col_bg = "lightblue2", title = "See 'white' colors()")
olives <- grepal("olive") oranges <- grepal("orange") seecol(list(olives, oranges), pal_names = c("olives", "oranges"), title = "Comparing olives and oranges")
seecol(grepal("SEE", pal_unikn), title = "All 'SEE' colors in pal_unikn")
seecol(grepal("blau", pal_unikn_pref), title = "All 'blau' colors in pal_unikn_pref")