Debian Codegraph @

R/divergingx_hcl.R

raw
1 #' (More) Flexible Diverging HCL Palettes
2 #'
3 #' Diverging HCL color palettes generated through combination of two fully
4 #' flexible (and possibly unbalanced) multi-hue sequential palettes.
5 #'
6 #' The \code{divergingx_hcl} function simply calls \code{\link{sequential_hcl}}
7 #' twice with a prespecified set of hue, chroma, and luminance parameters. This is
8 #' similar to \code{\link{diverging_hcl}} but allows for more flexibility:
9 #' \code{diverging_hcl} employs two \emph{single-hue} sequential palettes,
10 #' always uses zero chroma for the neutral/central color, and restricts the
11 #' chroma/luminance path to be the same in both \dQuote{arms} of the palette.
12 #' In contrast, \code{divergingx_hcl} relaxes this to two full \emph{multi-hue}
13 #' palettes that can thus go through a non-gray neutral color (typically light
14 #' yellow). Consequently, the chroma/luminance paths can be rather unbalanced
15 #' between the two arms.
16 #'
17 #' With this additional flexibility various diverging palettes suggested by
18 #' \url{https://ColorBrewer2.org/} and CARTO (\url{https://carto.com/carto-colors/}),
19 #' can be emulated along with the Zissou 1 palette from \pkg{wesanderson},
20 #' Cividis from \pkg{viridis}, and Roma from \pkg{scico}.
21 #'
22 #' Available CARTO palettes: ArmyRose, Earth, Fall, Geyser, TealRose, Temps, and
23 #' Tropic (with Tropic also available in \code{diverging_hcl}).
24 #'
25 #' Available ColorBrewer.org palettes: PuOr, RdBu, RdGy, PiYG, PRGn, BrBG, RdYlBu,
26 #' RdYlGn, Spectral.
27 #'
28 #' @param n the number of colors (\eqn{\ge 1}{>= 1}) to be in the palette.
29 #' @param palette character with the name (see details).
30 #' @param \dots arguments passed to \code{\link{hex}}.
31 #' @param fixup logical. Should the color be corrected to a valid RGB value?
32 #' @param alpha numeric vector of values in the range \code{[0, 1]} for alpha
33 #' transparency channel (0 means transparent and 1 means opaque).
34 #' @param rev logical. Should the palette be reversed?
35 #' @param h1 numeric. Starting hue coordinate.
36 #' @param h2 numeric. Center hue coordinate.
37 #' @param h3 numeric. Ending hue coordinate.
38 #' @param c1 numeric. Chroma coordinate corresponding to \code{h1}.
39 #' @param c2 numeric. Chroma coordinate corresponding to \code{h2} (if \code{NA}, set to 0).
40 #' @param c3 numeric. Chroma coordinate corresponding to \code{h3} (if \code{NA}, \code{c1} is used).
41 #' @param l1 numeric. Luminance coordinate corresponding to \code{h1}.
42 #' @param l2 numeric. Luminance coordinate corresponding to \code{h2} (if \code{NA}, \code{l1} is used).
43 #' @param l3 numeric. Luminance coordinate corresponding to \code{h3} (if \code{NA}, \code{l1} is used).
44 #' @param p1 numeric. Power parameter for chroma coordinates in first sequential palette (if \code{NA}, \code{1} is used).
45 #' @param p2 numeric. Power parameter for luminance coordinates in first sequential palette (if \code{NA}, \code{p1} is used).
46 #' @param p3 numeric. Power parameter for chroma coordinates in second sequential palette (if \code{NA}, \code{p1} is used).
47 #' @param p4 numeric. Power parameter for luminance coordinates in second sequential palette (if \code{NA}, \code{p2} is used).
48 #' @param cmax1 numeric. Maximum chroma coordinate in first sequential palette (not used if \code{NA}).
49 #' @param cmax2 numeric. Maximum chroma coordinate in second sequential palette (not used if \code{NA}).
50 #' @param plot logical. Should the selected HCL color palettes be visualized?
51 #'
52 #' @return A character vector with (s)RGB codings of the colors in the palette.
53 #' @seealso \code{\link[colorspace]{sequential_hcl}}, \code{\link[colorspace]{diverging_hcl}}
54 #' @references Zeileis A, Fisher JC, Hornik K, Ihaka R, McWhite CD, Murrell P, Stauffer R, Wilke CO (2020).
55 #' \dQuote{colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.}
56 #' \emph{Journal of Statistical Software}, \bold{96}(1), 1--49. \doi{10.18637/jss.v096.i01}
57 #' @keywords color
58 #' @examples
59 #' ## show emulated CARTO/ColorBrewer.org palettes
60 #' divergingx_palettes(plot = TRUE)
61 #'
62 #' ## compared to diverging_hcl() the diverging CARTO palettes are typically warmer
63 #' ## but also less balanced with respect to chroma/luminance, see e.g.,
64 #' specplot(divergingx_hcl(7, "ArmyRose"))
65 #' @rdname divergingx_hcl
66
67 #' @export
68 divergingx_hcl <- function(n, palette = "Geyser", ...,
69 fixup = TRUE, alpha = 1, rev = FALSE,
70 h1, h2, h3, c1, c2, c3, l1, l2, l3, p1, p2, p3, p4, cmax1, cmax2)
71 {
72 ## empty palette
73 if(n < 1L) return(character(0L))
74
75 ## obtained stored coordinates
76 pals <- as.matrix(divergingx_palettes(palette = palette)[, -1L])[1L, ]
77
78 ## replace coordinates (if specified)
79 if(!missing(h1)) pals["h1"] <- h1
80 if(!missing(h2)) pals["h2"] <- h2
81 if(!missing(h3)) pals["h3"] <- h3
82 if(!missing(c1)) pals["c1"] <- c1
83 if(!missing(c2)) pals["c2"] <- c2
84 if(!missing(c3)) pals["c3"] <- c3
85 if(!missing(l1)) pals["l1"] <- l1
86 if(!missing(l2)) pals["l2"] <- l2
87 if(!missing(l3)) pals["l3"] <- l3
88 if(!missing(p1)) pals["p1"] <- p1
89 if(!missing(p2)) pals["p2"] <- p2
90 if(!missing(p3)) pals["p3"] <- p3
91 if(!missing(p4)) pals["p4"] <- p4
92 if(!missing(cmax1)) pals["cmax1"] <- cmax1
93 if(!missing(cmax2)) pals["cmax2"] <- cmax2
94
95 ## resolve NAs
96 ## first coordinate
97 if(any(is.na(pals[c("h1", "c1", "l1")]))) stop("first hue/chroma/luminance coordinate must be specified")
98 if(is.na(pals["p1"])) pals["p1"] <- 1
99 ## second coordinate
100 if(is.na(pals["c2"])) pals["c2"] <- 0
101 if(is.na(pals["l2"])) pals["l2"] <- pals["l1"]
102 if(is.na(pals["p2"])) pals["p2"] <- pals["p1"]
103 ## third coordinate
104 if(is.na(pals["h3"])) stop("third hue coordinate must be specified")
105 if(is.na(pals["c3"])) pals["c3"] <- pals["c1"]
106 if(is.na(pals["l3"])) pals["l3"] <- pals["l1"]
107 if(is.na(pals["p3"])) pals["p3"] <- pals["p1"]
108 if(is.na(pals["p4"])) pals["p4"] <- pals["p2"]
109
110 ## call sequential_hcl() once or twice
111 n2 <- ceiling(n/2)
112 rval <- if(n == 1L) 0 else pmax(0, seq.int(1, by = -2/(n - 1), length.out = n2))
113 rval <- c(seqhcl(rval, pals["h1"], if(is.na(pals["h2"])) pals["h1"] else pals["h2"], pals["c1"], pals["c2"],
114 pals["l1"], pals["l2"], pals["p1"], pals["p2"], pals["cmax1"], fixup, ...),
115 rev(seqhcl(rval, pals["h3"], if(is.na(pals["h2"])) pals["h3"] else pals["h2"], pals["c3"], pals["c2"],
116 pals["l3"], pals["l2"], pals["p3"], pals["p4"], pals["cmax2"], fixup, ...)))
117 if(floor(n/2) < n2) rval <- rval[-n2]
118
119 ## alpha transparency
120 if(!missing(alpha)) {
121 alpha <- pmax(pmin(alpha, 1), 0)
122 alpha <- format(as.hexmode(round(alpha * 255 + 0.0001)), width = 2L, upper.case = TRUE)
123 rval <- ifelse(is.na(rval), NA, paste(rval, alpha, sep = ""))
124 }
125
126 ## return value
127 if(rev) rval <- rev(rval)
128 return(rval)
129 }
130
131 #' @rdname divergingx_hcl
132 #' @usage NULL
133 #' @export
134 divergex_hcl <- divergingx_hcl
135
136 #' @rdname divergingx_hcl
137 #' @export
138 divergingx_palettes <- function(palette = NULL, plot = FALSE, n = 7L, ...)
139 {
140 ## collect all divergingx palettex
141 pals <- as.data.frame(do.call("rbind", divex_pals))
142 rownames(pals) <- names(divex_pals)
143 pals$type <- factor(rep.int("Diverging (flexible)", nrow(pals)))
144 names(pals) <- c("h1", "h2", "h3", "c1", "c2", "c3", "l1", "l2", "l3", "p1", "p2", "p3", "p4", "cmax1", "cmax2", "type")
145 pals$fixup <- TRUE
146 pals <- pals[, c(16L, 1L:15L)]
147
148 ## subset by type and name (by flexible matching)
149 if(!is.null(palette)) {
150 fx <- function(n) tolower(gsub("[-, _, \\,, (, ), \\ , \\.]", "", n))
151 namtab <- fx(rownames(pals))
152 palette <- sapply(fx(palette), function(n) {
153 if(n %in% namtab) return(n)
154 n <- startsWith(namtab, n)
155 if(all(!n)) stop("Named 'palette' should be one of: ", paste(rownames(pals), collapse = ", "))
156 namtab[which(n)[1L]]
157 })
158 pals <- pals[fx(rownames(pals)) %in% palette, , drop = FALSE]
159 }
160
161 ## add class and show selection
162 class(pals) <- c("hcl_palettes", "data.frame")
163 if(plot) {
164 plot(pals, n = n, ...)
165 invisible(pals)
166 } else {
167 return(pals)
168 }
169 }
170
171 divex_pals <- list()
172
173 ## CARTO
174 divex_pals[["ArmyRose"]] <- c( 0, NA, 93, 73, 18, 47, 58, 98, 52, 1.5, 0.8, 0.8, 1.0, NA, 55)
175 divex_pals[["Earth"]] <- c( 43, 82, 221, 61, 30, 45, 50, 92, 52, 1.0, 1.0, 0.8, 1.0, NA, 10)
176 divex_pals[["Fall"]] <- c(133, 77, 21, 20, 35, 100, 35, 95, 50, 1.0, NA, 1.5, NA, NA, NA)
177 divex_pals[["Geyser"]] <- c(192, 77, 21, 40, 35, 100, 50, 95, 50, 1.0, 1.0, 1.2, 1.0, 20, NA)
178 divex_pals[["TealRose"]] <- c(190, 77, 0, 50, 25, 80, 55, 92, 55, 1.5, 1.0, 1.8, 1.0, 15, NA)
179 divex_pals[["Temps"]] <- c(191, 80, -4, 43, 50, 78, 55, 89, 54, 1.6, 1.0, 1.0, 1.0, 57, 85)
180 divex_pals[["Tropic"]] <- c(195, NA, 325, 70, NA, NA, 55, 95, NA, 1.0, NA, NA, NA, NA, NA)
181
182 ## ColorBrewer.org
183 divex_pals[["PuOr"]] <- c( 40, NA, 270, 70, 0, 30, 30, 98, 10, 0.6, 1.4, 1.5, 1.3, 100, 65)
184 divex_pals[["RdBu"]] <- c( 20, NA, 230, 60, 0, 50, 20, 98, 15, 1.4, 1.2, 1.5, 1.5, 125, 90)
185 divex_pals[["RdGy"]] <- c( 5, 50, 50, 60, 0, 0, 20, 98, 20, 1.2, 1.2, 1.0, 1.2, 125, NA)
186 divex_pals[["PiYG"]] <- c(340, NA, 115, 75, 0, 50, 30, 98, 35, 1.3, 1.4, 0.8, 1.5, 100, 80)
187 divex_pals[["PRGn"]] <- c(300, NA, 128, 30, 0, 30, 15, 97, 25, 1.3, 1.2, 0.9, 1.5, 65, 65)
188 divex_pals[["BrBG"]] <- c( 55, NA, 180, 40, 0, 30, 25, 97, 20, 0.8, 1.4, 0.8, 1.4, 75, 45)
189 divex_pals[["RdYlBu"]] <- c( 10, 85, 260, 105, 45, 70, 35, 98, 35, 1.5, 1.2, 0.6, 1.2, 150, 10)
190 divex_pals[["RdYlGn"]] <- c( 10, 85, 140, 105, 45, 50, 35, 98, 35, 1.5, 1.2, 0.8, 1.2, 150, 75)
191 divex_pals[["Spectral"]] <- c( 0, 85, 270, 90, 45, 65, 37, 98, 37, 1.0, 1.2, 1.0, 1.2, 120, NA)
192
193 ## wesanderson
194 divex_pals[["Zissou 1"]] <- c(218, 71, 12, 46, 88, 165, 59, 82, 52, 0.2, 1.0, 3.0, 1.0, 33, NA)
195
196 ## Cividis
197 divex_pals[["Cividis"]] <- c(255, NA, 75, 30, 0, 95, 13, 52, 92, 1.1, 1.0, 1.0, NA, 47, NA)
198
199 ## scico
200 divex_pals[["Roma"]] <- c( 10, 120, 265, 80, 25, 80, 25, 92, 25, 0.4, 1.5, 1.0, 1.2, NA, NA)