Skip to content

Commit 8a18be6

Browse files
committed
PERF: Palette capability: accepts_native_output=TRUE
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.
1 parent 44a4d08 commit 8a18be6

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

R/scale-.r

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,17 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
642642
}
643643
# Geom prefers native color format:
644644
geom_prefers_native <- identical(scale_params[["color_fmt"]], "native")
645-
if (geom_prefers_native) {
645+
# Palette capability: Accepts native output
646+
# A specific palette can have as attribute "accepts_native_output = TRUE".
647+
# Then, self$palette must have an additional argument (besides x) named
648+
# `color_fmt = "character"`. If we pass `color_fmt = "native"`, it will
649+
# return colors in native format.
650+
pal_accepts_native <- ggproto_attr(self$palette, "accepts_native_output", default = FALSE)
651+
652+
if (geom_prefers_native && pal_accepts_native) {
653+
palette <- function(x) self$palette(x, color_fmt = "native")
654+
na.value <- farver::encode_native(self$na.value)
655+
} else if (geom_prefers_native) {
646656
palette <- function(x) farver::encode_native(self$palette(x))
647657
na.value <- farver::encode_native(self$na.value)
648658
} else {

0 commit comments

Comments
 (0)