Skip to content

Commit 44a4d08

Browse files
committed
FEAT: ScaleContinuous can return native colour format
Some geoms benefit from using native colour format instead of the character based colour format. This commit lets the geoms specify that they prefer native format for colours, and gives the responsibility of converting into that format to ScaleContinuous. Since today it is not mandatory for all scales to honor scale_params, the geom that requests this will have to verify that the color is given in native format anyway, and do the conversion if it has not been done here. However by optionally shifting the responsibility of the conversion to the scale we have potential to further optimizations
1 parent 6513a17 commit 44a4d08

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Depends:
3131
R (>= 3.3)
3232
Imports:
3333
cli,
34+
farver,
3435
glue,
3536
grDevices,
3637
grid,

R/scale-.r

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,20 +640,30 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
640640
))
641641
mapping_method <- "unique"
642642
}
643+
# Geom prefers native color format:
644+
geom_prefers_native <- identical(scale_params[["color_fmt"]], "native")
645+
if (geom_prefers_native) {
646+
palette <- function(x) farver::encode_native(self$palette(x))
647+
na.value <- farver::encode_native(self$na.value)
648+
} else {
649+
palette <- self$palette
650+
na.value <- self$na.value
651+
}
652+
643653
if (mapping_method == "unique") {
644654
uniq <- unique0(x)
645-
pal <- self$palette(uniq)
655+
pal <- palette(uniq)
646656
scaled <- pal[match(x, uniq)]
647657
} else if (mapping_method == "raw") {
648-
scaled <- self$palette(x)
658+
scaled <- palette(x)
649659
} else if (mapping_method == "binned") {
650660
mapping_method_bins <- scale_params[["mapping_method_bins"]]
651661
if (is.null(mapping_method_bins)) {
652662
mapping_method_bins <- 1024L
653663
}
654664
mapping_method_bins <- as.integer(mapping_method_bins[1L])
655665
breaks <- seq(from = 0, to = 1, length.out = mapping_method_bins + 1L)
656-
colormap <- c(self$na.value, self$palette(breaks), self$na.value)
666+
colormap <- c(na.value, palette(breaks), na.value)
657667
# values below 0 belong to the first bucket, but zero belongs to the second bucket:
658668
breaks[1] <- -.Machine$double.eps
659669
scaled <- colormap[findInterval(x, breaks, rightmost.closed = TRUE) + 1L]
@@ -663,7 +673,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
663673
# If it has such attribute, we will skip the ifelse(!is.na(scaled), ...)
664674
pal_may_return_na <- ggproto_attr(self$palette, "may_return_NA", default = TRUE)
665675
if (pal_may_return_na) {
666-
scaled <- ifelse(!is.na(scaled), scaled, self$na.value)
676+
scaled <- ifelse(!is.na(scaled), scaled, na.value)
667677
}
668678

669679
scaled

0 commit comments

Comments
 (0)