-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Barebones support for <GridPattern>
fills.
#5299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
594c011
Write pattern utilities
teunbrand 1aca809
Intercept non-list patterns
teunbrand 48b3a2a
Support pattern fills in geoms
teunbrand d47f0a1
Support pattern fills in keys
teunbrand 44dd11b
Note that `geom_raster()` cannot use pattern fills
teunbrand 4e535ce
More informative call in error message
teunbrand 23d08f8
Write tests
teunbrand 4baf67a
Document
teunbrand 3672fb8
Some version protections
teunbrand 54014f7
Merge branch 'main' into pattern_fills
teunbrand e54329c
Use device checker
teunbrand 96736f5
Set white alpha mask
teunbrand 2ad2474
Clarify error message
teunbrand 1512f29
deal with unavailable functions/arguments
teunbrand 11f20d6
typo
teunbrand 6d65104
Also handle unlisted pattern
teunbrand a4455f9
Invert viewport backport
teunbrand dd5b9f0
Merge branch 'main' into pattern_fills
teunbrand d84b519
Merge branch 'main' into pattern_fills
teunbrand b1e0126
`geom_raster()` throws error when fill is pattern
teunbrand 8ed0564
device check warns instead of aborts
teunbrand 3f9d4ac
reimplement `pattern_alpha` as S3 generic + methods
teunbrand 036ce01
accept new snapshot
teunbrand c44efb5
Add news bullet
teunbrand 1e500e8
Merge branch 'main' into pattern_fills
teunbrand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
|
||
#' Modify fill transparency | ||
#' | ||
#' This works much like [alpha()][scales::alpha] in that it modifies the | ||
#' transparency of fill colours. It differs in that `fill_alpha()` also attempts | ||
#' to set the transparency of `<GridPattern>` objects. | ||
#' | ||
#' @param fill A fill colour given as a `character` or `integer` vector, or as a | ||
#' (list of) `<GridPattern>` object(s). | ||
#' @param alpha A transparency value between 0 (transparent) and 1 (opaque), | ||
#' parallel to `fill`. | ||
#' | ||
#' @return A `character` vector of colours, or list of `<GridPattern>` objects. | ||
#' @export | ||
#' @keywords internal | ||
#' | ||
#' @examples | ||
#' # Typical colour input | ||
#' fill_alpha("red", 0.5) | ||
#' | ||
#' if (utils::packageVersion("grid") > "4.2") { | ||
#' # Pattern input | ||
#' fill_alpha(list(grid::linearGradient()), 0.5) | ||
#' } | ||
fill_alpha <- function(fill, alpha) { | ||
if (!is.list(fill)) { | ||
# Happy path for no patterns | ||
return(alpha(fill, alpha)) | ||
} | ||
if (is_pattern(fill) || any(vapply(fill, is_pattern, logical(1)))) { | ||
check_device("patterns", action = "warn") | ||
fill <- pattern_alpha(fill, alpha) | ||
return(fill) | ||
} else { | ||
# We are either dealing with faulty fill specification, or we have a legend | ||
# key that is trying to draw a single colour. It can be given that colour | ||
# as a list due to patterns in other keys. | ||
msg <- paste0( | ||
"{.field fill} must be a vector of colours or list of ", | ||
"{.cls GridPattern} objects." | ||
) | ||
# If single colour list, try applying `alpha()` | ||
fill <- try_fetch( | ||
Map(alpha, colour = fill, alpha = alpha), | ||
error = function(cnd) { | ||
cli::cli_abort(msg, call = expr(fill_alpha())) | ||
} | ||
) | ||
# `length(input)` must be same as `length(output)` | ||
if (!all(lengths(fill) == 1)) { | ||
cli::cli_abort(msg) | ||
} | ||
return(unlist(fill)) | ||
} | ||
} | ||
|
||
# Similar to grid:::is.pattern | ||
is_pattern <- function(x) { | ||
inherits(x, "GridPattern") | ||
} | ||
|
||
#' Modify transparency for patterns | ||
#' | ||
#' This generic allows you to add your own methods for adding transparency to | ||
#' pattern-like objects. | ||
#' | ||
#' @param x Object to be interpreted as pattern. | ||
#' @param alpha A `numeric` vector between 0 and 1. If `NA`, alpha values | ||
#' are preserved. | ||
#' | ||
#' @return `x` with modified transparency | ||
#' @export | ||
#' @keywords internal | ||
pattern_alpha <- function(x, alpha) { | ||
teunbrand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
UseMethod("pattern_alpha") | ||
} | ||
|
||
#' @export | ||
pattern_alpha.default <- function(x, alpha) { | ||
if (!is.atomic(x)) { | ||
cli::cli_abort("Can't apply {.arg alpha} to {obj_type_friendly(x)}.") | ||
} | ||
pattern(rectGrob(), gp = gpar(fill = alpha(x, alpha))) | ||
} | ||
|
||
#' @export | ||
pattern_alpha.GridPattern <- function(x, alpha) { | ||
x$colours <- alpha(x$colours, alpha[1]) | ||
x | ||
} | ||
|
||
#' @export | ||
pattern_alpha.GridTilingPattern <- function(x, alpha) { | ||
if (all(is.na(alpha) | alpha == 1)) { | ||
return(x) | ||
} | ||
check_device("alpha_masks", "warn") | ||
grob <- env_get(environment(x$f), "grob") | ||
mask <- as.mask(rectGrob(gp = gpar(fill = alpha("white", alpha)))) | ||
if (is.null(grob$vp)) { | ||
grob$vp <- viewport(mask = mask) | ||
} else { | ||
grob$vp <- editViewport(grob$vp, mask = mask) | ||
} | ||
new_env <- new.env(parent = environment(x$f)) | ||
env_bind(new_env, grob = grob) | ||
environment(x$f) <- new_env | ||
x | ||
} | ||
|
||
#' @export | ||
pattern_alpha.list <- function(x, alpha) { | ||
Map(pattern_alpha, x = x, alpha = alpha) | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.