update_alpha() modifies the transparency of colours and/or patterns.
Details
This is a fork of pattern utilities mainly added to
{ggplot2}by Teun van den Brand.update_alpha()does not depend on{ggplot2}or{scales}.Like
ggplot2::fill_alpha()but unlikescales::alpha()it also attempts to set the transparency of<GridPattern>objects.Unlike
ggplot2::fill_alpha()it will work on a list of length one containing a vector of color strings.
Usage in other packages
To avoid taking a dependency on gridpattern you may copy the source of update_alpha()
into your own package under the permissive MIT license. Either use
usethis::use_standalone("trevorld/gridpattern", "standalone-update_alpha.R")
or copy the file update_alpha.R into your R directory and
add grDevices, grid, and rlang to the Imports of your DESCRIPTION file.
Examples
# Typical color input
update_alpha("red", 0.5)
#> [1] "#FF000080"
# Pattern input
if (getRversion() >= "4.2" && requireNamespace("grid", quietly = TRUE)) {
update_alpha(list(grid::linearGradient()), 0.5)
}
#> [[1]]
#> $x1
#> [1] 0npc
#>
#> $y1
#> [1] 0npc
#>
#> $x2
#> [1] 1npc
#>
#> $y2
#> [1] 1npc
#>
#> $stops
#> [1] 0 1
#>
#> $colours
#> [1] "#00000080" "#FFFFFF80"
#>
#> $extend
#> [1] "pad"
#>
#> $group
#> [1] TRUE
#>
#> attr(,"class")
#> [1] "GridLinearGradient" "GridPattern"
#>