Skip to content

Commit

Permalink
PERF: Palette capability: accepts_native_output=TRUE
Browse files Browse the repository at this point in the history
If a palette has accepts_native_output=TRUE set as an attribute,
ScaleContinuous assumes the palette has an optional argument named
`color_fmt` which can be set to either "character" or "native".

If the geom prefers a native output format and the palette supports
it, we let the palette take care of it.

The conversion goes from value -> native colour, which is much faster
than going through an intermediate character representation of
the colours.
  • Loading branch information
zeehio committed Nov 6, 2022
1 parent 80b4a32 commit 1abc808
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,17 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
}
# Geom prefers native color format:
geom_prefers_native <- identical(scale_params[["color_fmt"]], "native")
if (geom_prefers_native) {
# Palette capability: Accepts native output
# A specific palette can have as attribute "accepts_native_output = TRUE".
# Then, self$palette must have an additional argument (besides x) named
# `color_fmt = "character"`. If we pass `color_fmt = "native"`, it will
# return colors in native format.
pal_accepts_native <- ggproto_attr(self$palette, "accepts_native_output", default = FALSE)

if (geom_prefers_native && pal_accepts_native) {
palette <- function(x) self$palette(x, color_fmt = "native")
na.value <- farver::encode_native(self$na.value)
} else if (geom_prefers_native) {
palette <- function(x) farver::encode_native(self$palette(x))
na.value <- farver::encode_native(self$na.value)
} else {
Expand Down

0 comments on commit 1abc808

Please sign in to comment.