|
| 1 | + |
| 2 | +#' Modify fill transparency |
| 3 | +#' |
| 4 | +#' This works much like [alpha()][scales::alpha] in that it modifies the |
| 5 | +#' transparency of fill colours. It differs in that `fill_alpha()` also attempts |
| 6 | +#' to set the transparency of `<GridPattern>` objects. |
| 7 | +#' |
| 8 | +#' @param fill A fill colour given as a `character` or `integer` vector, or as a |
| 9 | +#' (list of) `<GridPattern>` object(s). |
| 10 | +#' @param alpha A transparency value between 0 (transparent) and 1 (opaque), |
| 11 | +#' parallel to `fill`. |
| 12 | +#' |
| 13 | +#' @return A `character` vector of colours, or list of `<GridPattern>` objects. |
| 14 | +#' @export |
| 15 | +#' @keywords internal |
| 16 | +#' |
| 17 | +#' @examples |
| 18 | +#' # Typical colour input |
| 19 | +#' fill_alpha("red", 0.5) |
| 20 | +#' |
| 21 | +#' if (utils::packageVersion("grid") > "4.2") { |
| 22 | +#' # Pattern input |
| 23 | +#' fill_alpha(list(grid::linearGradient()), 0.5) |
| 24 | +#' } |
| 25 | +fill_alpha <- function(fill, alpha) { |
| 26 | + if (!is.list(fill)) { |
| 27 | + # Happy path for no patterns |
| 28 | + return(alpha(fill, alpha)) |
| 29 | + } |
| 30 | + if (is_pattern(fill) || any(vapply(fill, is_pattern, logical(1)))) { |
| 31 | + check_device("patterns", action = "warn") |
| 32 | + fill <- pattern_alpha(fill, alpha) |
| 33 | + return(fill) |
| 34 | + } else { |
| 35 | + # We are either dealing with faulty fill specification, or we have a legend |
| 36 | + # key that is trying to draw a single colour. It can be given that colour |
| 37 | + # as a list due to patterns in other keys. |
| 38 | + msg <- paste0( |
| 39 | + "{.field fill} must be a vector of colours or list of ", |
| 40 | + "{.cls GridPattern} objects." |
| 41 | + ) |
| 42 | + # If single colour list, try applying `alpha()` |
| 43 | + fill <- try_fetch( |
| 44 | + Map(alpha, colour = fill, alpha = alpha), |
| 45 | + error = function(cnd) { |
| 46 | + cli::cli_abort(msg, call = expr(fill_alpha())) |
| 47 | + } |
| 48 | + ) |
| 49 | + # `length(input)` must be same as `length(output)` |
| 50 | + if (!all(lengths(fill) == 1)) { |
| 51 | + cli::cli_abort(msg) |
| 52 | + } |
| 53 | + return(unlist(fill)) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +# Similar to grid:::is.pattern |
| 58 | +is_pattern <- function(x) { |
| 59 | + inherits(x, "GridPattern") |
| 60 | +} |
| 61 | + |
| 62 | +#' Modify transparency for patterns |
| 63 | +#' |
| 64 | +#' This generic allows you to add your own methods for adding transparency to |
| 65 | +#' pattern-like objects. |
| 66 | +#' |
| 67 | +#' @param x Object to be interpreted as pattern. |
| 68 | +#' @param alpha A `numeric` vector between 0 and 1. If `NA`, alpha values |
| 69 | +#' are preserved. |
| 70 | +#' |
| 71 | +#' @return `x` with modified transparency |
| 72 | +#' @export |
| 73 | +#' @keywords internal |
| 74 | +pattern_alpha <- function(x, alpha) { |
| 75 | + UseMethod("pattern_alpha") |
| 76 | +} |
| 77 | + |
| 78 | +#' @export |
| 79 | +pattern_alpha.default <- function(x, alpha) { |
| 80 | + if (!is.atomic(x)) { |
| 81 | + cli::cli_abort("Can't apply {.arg alpha} to {obj_type_friendly(x)}.") |
| 82 | + } |
| 83 | + pattern(rectGrob(), gp = gpar(fill = alpha(x, alpha))) |
| 84 | +} |
| 85 | + |
| 86 | +#' @export |
| 87 | +pattern_alpha.GridPattern <- function(x, alpha) { |
| 88 | + x$colours <- alpha(x$colours, alpha[1]) |
| 89 | + x |
| 90 | +} |
| 91 | + |
| 92 | +#' @export |
| 93 | +pattern_alpha.GridTilingPattern <- function(x, alpha) { |
| 94 | + if (all(is.na(alpha) | alpha == 1)) { |
| 95 | + return(x) |
| 96 | + } |
| 97 | + check_device("alpha_masks", "warn") |
| 98 | + grob <- env_get(environment(x$f), "grob") |
| 99 | + mask <- as.mask(rectGrob(gp = gpar(fill = alpha("white", alpha)))) |
| 100 | + if (is.null(grob$vp)) { |
| 101 | + grob$vp <- viewport(mask = mask) |
| 102 | + } else { |
| 103 | + grob$vp <- editViewport(grob$vp, mask = mask) |
| 104 | + } |
| 105 | + new_env <- new.env(parent = environment(x$f)) |
| 106 | + env_bind(new_env, grob = grob) |
| 107 | + environment(x$f) <- new_env |
| 108 | + x |
| 109 | +} |
| 110 | + |
| 111 | +#' @export |
| 112 | +pattern_alpha.list <- function(x, alpha) { |
| 113 | + Map(pattern_alpha, x = x, alpha = alpha) |
| 114 | +} |
| 115 | + |
0 commit comments