| 1 |
#' HCL Color Palettes |
| 2 |
#' |
| 3 |
#' Qualitative, sequential (single-hue and multi-hue), and diverging |
| 4 |
#' color palettes based on the HCL (hue-chroma-luminance) color model. |
| 5 |
#' |
| 6 |
#' The HCL (hue-chroma-luminance) color model is a perceptual color model |
| 7 |
#' obtained by using polar coordinates in CIE \code{\link{LUV}} space |
| 8 |
#' (i.e., \code{\link{polarLUV}}), where steps of equal size correspond to |
| 9 |
#' approximately equal perceptual changes in color. By taking polar coordinates |
| 10 |
#' the resulting three dimensions capture the three perceptual axes very well: |
| 11 |
#' hue is the type of color, chroma the colorfulness compared |
| 12 |
#' to the corresponding gray, and luminance the brightness. This makes it relatively |
| 13 |
#' easy to create balanced palettes through trajectories in this HCL space. |
| 14 |
#' In contrast, in the more commonly-used \code{\link{HSV}} (hue-saturation-value) |
| 15 |
#' model (a simple transformation of \code{\link{RGB}}), the three axes are |
| 16 |
#' confounded so that luminance changes along with the hue leading to very |
| 17 |
#' unbalanced palettes (see \code{\link{rainbow_hcl}} for further illustrations). |
| 18 |
#' |
| 19 |
#' Three types of palettes are derived based on the HCL model: \itemize{ |
| 20 |
#' \item Qualitative: Designed for coding categorical information, i.e., |
| 21 |
#' where no particular ordering of categories is available and every color |
| 22 |
#' should receive the same perceptual weight. |
| 23 |
#' \item Sequential: Designed for coding ordered/numeric information, i.e., |
| 24 |
#' where colors go from high to low (or vice versa). |
| 25 |
#' \item Diverging: Designed for coding numeric information around a central |
| 26 |
#' neutral value, i.e., where colors diverge from neutral to two extremes. |
| 27 |
#' } |
| 28 |
#' The corresponding functions are \code{qualitative_hcl}, \code{sequential_hcl}, |
| 29 |
#' and \code{diverging_hcl}. Their construction principles are explained in more detail below. |
| 30 |
#' At the core is the luminance axis (i.e., light-dark contrasts): |
| 31 |
#' These are easily decoded by humans and matched to high-low differences in the underlying |
| 32 |
#' data. Therefore, \code{sequential_hcl} palettes are always based on a \emph{monotonic} |
| 33 |
#' luminance sequence whereas the colors in a \code{qualitative_hcl} palette all have the |
| 34 |
#' \emph{same} luminance. Finally, \code{diverging_hcl} palettes use the same monotonic |
| 35 |
#' luminance sequence in both \dQuote{arms} of the palette, i.e., correspond to |
| 36 |
#' two balanced sequential palettes diverging from the same neutral value. |
| 37 |
#' The other two axes, hue and chroma, are used to enhance the luminance information and/or |
| 38 |
#' to further discriminate the color. |
| 39 |
#' |
| 40 |
#' All three palette functions specify trajectories in HCL space and hence need either |
| 41 |
#' single values or intervals of the coordinates \code{h}, \code{c}, \code{l}. Their |
| 42 |
#' interfaces are always designed such that \code{h}, \code{c}, \code{l} can take vector |
| 43 |
#' arguments (as needed) but alternatively or additionally \code{h1}/\code{h2}, |
| 44 |
#' \code{c1}/\code{cmax}/\code{c2}, and \code{l1}/\code{l2} can be specified. If so, |
| 45 |
#' the latter coordinates overwrite the former. |
| 46 |
#' |
| 47 |
#' \code{qualitative_hcl} distinguishes the underlying categories by a sequence of |
| 48 |
#' hues while keeping both chroma and luminance constant to give each color in the |
| 49 |
#' resulting palette the same perceptual weight. Thus, \code{h} should be a pair of |
| 50 |
#' hues (or equivalently \code{h1} and \code{h2} can be used) with the starting and |
| 51 |
#' ending hue of the palette. Then, an equidistant sequence between these hues is |
| 52 |
#' employed, by default spanning the full color wheel (i.e, the full 360 degrees). |
| 53 |
#' Chroma \code{c} (or equivalently \code{c1}) and luminance \code{l} (or equivalently |
| 54 |
#' \code{l1}) are constants. |
| 55 |
#' |
| 56 |
#' \code{sequential_hcl} codes the underlying numeric values by a monotonic sequence |
| 57 |
#' of increasing (or decreasing) luminance. Thus, the \code{l} argument should provide |
| 58 |
#' a vector of length 2 with starting and ending luminance (equivalently, \code{l1} and |
| 59 |
#' \code{l2} can be used). Without chroma (i.e., \code{c = 0}), this simply corresponds |
| 60 |
#' to a grayscale palette like \code{\link[grDevices]{gray.colors}}. For adding chroma, a simple |
| 61 |
#' strategy would be to pick a single hue (via \code{h} or \code{h1}) and then decrease |
| 62 |
#' chroma from some value (\code{c} or \code{c1}) to zero (i.e., gray) along with |
| 63 |
#' increasing luminance. For bringing out the extremes (a dark high-chroma color vs. |
| 64 |
#' a light gray) this is already very effective. For distinguishing also colors in the |
| 65 |
#' middle two strategies can be employed: (a) Hue can be varied as well by specifying an |
| 66 |
#' interval of hues in \code{h} (or beginning hue \code{h1} and ending hue \code{h2}). |
| 67 |
#' (b) Instead of a decreasing chroma a triangular chroma trajectory can be employed |
| 68 |
#' from \code{c1} over \code{cmax} to \code{c2} (or equivalently a vector \code{c} of |
| 69 |
#' length 3). This yields high-chroma colors in the middle of the palette that are |
| 70 |
#' more easily distinguished from the dark and light extremes. Finally, instead of |
| 71 |
#' employing linear trajectories, power transformations are supported in chroma and |
| 72 |
#' luminance via a vector \code{power} (or separate \code{p1} and \code{p2}). If |
| 73 |
#' \code{power[2]} (or \code{p2}) for the luminance trajectory is missing, it defaults |
| 74 |
#' to \code{power[1]}/\code{p1} from the chroma trajectory. |
| 75 |
#' |
| 76 |
#' \code{diverging_hcl} codes the underlying numeric values by a triangular luminance |
| 77 |
#' sequence with different hues in the left and in the right arm of the palette. Thus, |
| 78 |
#' it can be seen as a combination of two sequential palettes with some restrictions: |
| 79 |
#' (a) a single hue is used for each arm of the palette, (b) chroma and luminance trajectory |
| 80 |
#' are balanced between the two arms, (c) the neutral central value has zero chroma. |
| 81 |
#' To specify such a palette a vector of two hues \code{h} (or equivalently \code{h1} |
| 82 |
#' and \code{h2}), either a single chroma value \code{c} (or \code{c1}) or a vector |
| 83 |
#' of two chroma values \code{c} (or \code{c1} and \code{cmax}), a vector of two |
| 84 |
#' luminances \code{l} (or \code{l1} and \code{l2}), and power parameter(s) \code{power} |
| 85 |
#' (or \code{p1} and \code{p2}) are used. For more flexible diverging palettes without |
| 86 |
#' the restrictrictions above (and consequently more parameters) |
| 87 |
#' \code{\link{divergingx_hcl}} is available. For backward compatibility, \code{diverge_hcl} |
| 88 |
#' is a copy of \code{diverging_hcl}. |
| 89 |
#' |
| 90 |
#' To facilitate using HCL-based palettes a wide range of example palettes are |
| 91 |
#' provided in the package and can be specified by a name instead of a set of |
| 92 |
#' parameters/coordinates. The examples have been taken from the literature and many |
| 93 |
#' approximate color palettes from other software packages such as ColorBrewer.org |
| 94 |
#' (\pkg{RColorBrewer}), CARTO colors (\pkg{rcartocolor}), \pkg{scico}, or \pkg{virids}. The |
| 95 |
#' function \code{hcl_palettes} can be used to query the available pre-specified palettes. It |
| 96 |
#' comes with a \code{print} method (listing names and types), a \code{summary} method |
| 97 |
#' (additionally listing the underlying parameters/coordinates), and a \code{plot} |
| 98 |
#' method that creates a \code{\link{swatchplot}} with suitable labels. |
| 99 |
#' |
| 100 |
#' The named HCL-based palettes have also been included in the \code{\link[grDevices]{hcl.colors}} |
| 101 |
#' function in base R in order to facilitate adoption in practice and in R packages. |
| 102 |
#' However, in \code{hcl.colors} it is only possible to obtain \code{n} colors from the |
| 103 |
#' given palette without the possibility for further modifications. |
| 104 |
#' |
| 105 |
#' @param n the number of colors (\eqn{\ge 1}{>= 1}) to be in the palette. |
| 106 |
#' @param h,h1,h2 hue value in the HCL color description, has to be in [0, 360]. |
| 107 |
#' @param c,c.,c1,c2 chroma value in the HCL color description. |
| 108 |
#' @param l,l1,l2 luminance value in the HCL color description. |
| 109 |
#' @param power,p1,p2 control parameter determining how chroma and luminance should |
| 110 |
#' be increased (1 = linear, 2 = quadratic, etc.). |
| 111 |
#' @param cmax Maximum chroma value in the HCL color description. |
| 112 |
#' @param gamma Deprecated. |
| 113 |
#' @param fixup logical. Should the color be corrected to a valid RGB value? |
| 114 |
#' @param off numeric. Vector of length 2 indicating horizontal and vertical |
| 115 |
#' offsets between the color rectangles displayed. |
| 116 |
#' @param border character. Color of rectangle borders. |
| 117 |
#' @param alpha numeric vector of values in the range \code{[0, 1]} for alpha |
| 118 |
#' transparency channel (0 means transparent and 1 means opaque). |
| 119 |
#' @param palette character. Name of HCL color palette. |
| 120 |
#' @param rev logical. Should the color palette vector be returned in reverse order? |
| 121 |
#' @param register character. If set to a non-empty character string, the corresponding |
| 122 |
#' palette is registered with that name for subsequent use (within the same session). |
| 123 |
#' @param \dots Other arguments passed to \code{\link{hex}}. |
| 124 |
#' @param type character indicating type of HCL palette. |
| 125 |
#' @param plot logical. Should the selected HCL color palettes be visualized? |
| 126 |
#' @param x,object A \code{hcl_palettes} object. |
| 127 |
#' |
| 128 |
#' @return \code{qualitative_hcl}, \code{sequential_hcl}, \code{diverging_hcl} return |
| 129 |
#' a vector of \code{n} color strings (hex codes). |
| 130 |
#' |
| 131 |
#' The function \code{hcl_palettes} returns a data frame of class \code{"hcl_palettes"} |
| 132 |
#' where each row contains information about one of the requested palettes (name, type, |
| 133 |
#' HCL trajectory coordinates). Suitable \code{print}, \code{summary}, and \code{plot} |
| 134 |
#' methods are available. |
| 135 |
#' @seealso \code{\link{divergingx_hcl}} \code{\link[grDevices]{hcl.colors}} |
| 136 |
#' @references Zeileis A, Hornik K, Murrell P (2009). Escaping RGBland: |
| 137 |
#' Selecting Colors for Statistical Graphics. \emph{Computational Statistics & |
| 138 |
#' Data Analysis}, \bold{53}, 3259--3270. |
| 139 |
#' \doi{10.1016/j.csda.2008.11.033} |
| 140 |
#' Preprint available from |
| 141 |
#' \url{https://www.zeileis.org/papers/Zeileis+Hornik+Murrell-2009.pdf}. |
| 142 |
#' |
| 143 |
#' Stauffer R, Mayr GJ, Dabernig M, Zeileis A (2015). Somewhere Over the |
| 144 |
#' Rainbow: How to Make Effective Use of Colors in Meteorological |
| 145 |
#' Visualizations. \emph{Bulletin of the American Meteorological Society}, |
| 146 |
#' \bold{96}(2), 203--216. |
| 147 |
#' \doi{10.1175/BAMS-D-13-00155.1} |
| 148 |
#' |
| 149 |
#' Zeileis A, Fisher JC, Hornik K, Ihaka R, McWhite CD, Murrell P, Stauffer R, Wilke CO (2020). |
| 150 |
#' \dQuote{colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.} |
| 151 |
#' \emph{Journal of Statistical Software}, \bold{96}(1), 1--49. \doi{10.18637/jss.v096.i01} |
| 152 |
#' |
| 153 |
#' Zeileis A, Murrell P (2023). |
| 154 |
#' \dQuote{Coloring in R's Blind Spot.} |
| 155 |
#' \emph{The R Journal}, \bold{15}(3), 240--256. \doi{10.32614/RJ-2023-071} |
| 156 |
#' @keywords color |
| 157 |
#' @examples |
| 158 |
#' ## overview of all _named_ HCL palettes |
| 159 |
#' hcl_palettes() |
| 160 |
#' |
| 161 |
#' ## visualize |
| 162 |
#' hcl_palettes("qualitative", plot = TRUE) |
| 163 |
#' hcl_palettes("sequential (single-hue)", n = 7, plot = TRUE) |
| 164 |
#' hcl_palettes("sequential (multi-hue)", n = 7, plot = TRUE) |
| 165 |
#' hcl_palettes("diverging", n = 7, plot = TRUE) |
| 166 |
#' |
| 167 |
#' ## inspect a specific palette |
| 168 |
#' ## (upper-case, spaces, etc. are ignored for matching) |
| 169 |
#' hcl_palettes(palette = "Dark 2") |
| 170 |
#' hcl_palettes(palette = "dark2") |
| 171 |
#' |
| 172 |
#' ## set up actual colors |
| 173 |
#' qualitative_hcl(4, h = c(0, 288), c = 50, l = 60) ## by hand |
| 174 |
#' qualitative_hcl(4, palette = "dark2") ## by name |
| 175 |
#' qualitative_hcl(4, palette = "dark2", c = 80) ## by name plus modification |
| 176 |
#' |
| 177 |
#' ## how HCL palettes are constructed: |
| 178 |
#' ## by varying the perceptual properties via hue/chroma/luminance |
| 179 |
#' swatchplot( |
| 180 |
#' "Hue" = sequential_hcl(5, h = c(0, 300), c = c(60, 60), l = 65), |
| 181 |
#' "Chroma" = sequential_hcl(5, h = 0, c = c(100, 0), l = 65, rev = TRUE, power = 1), |
| 182 |
#' "Luminance" = sequential_hcl(5, h = 260, c = c(25, 25), l = c(25, 90), rev = TRUE, power = 1), |
| 183 |
#' off = 0 |
| 184 |
#' ) |
| 185 |
#' |
| 186 |
#' ## for qualitative palettes luminance and chroma are fixed, varying only hue |
| 187 |
#' hclplot(qualitative_hcl(9, c = 50, l = 70)) |
| 188 |
#' |
| 189 |
#' ## single-hue sequential palette (h = 260) with linear vs. power-transformed trajectory |
| 190 |
#' hclplot(sequential_hcl(7, h = 260, c = 80, l = c(35, 95), power = 1)) |
| 191 |
#' hclplot(sequential_hcl(7, h = 260, c = 80, l = c(35, 95), power = 1.5)) |
| 192 |
#' |
| 193 |
#' ## advanced single-hue sequential palette with triangular chroma trajectory |
| 194 |
#' ## (piecewise linear vs. power-transformed) |
| 195 |
#' hclplot(sequential_hcl(7, h = 245, c = c(40, 75, 0), l = c(30, 95), power = 1)) |
| 196 |
#' hclplot(sequential_hcl(7, h = 245, c = c(40, 75, 0), l = c(30, 95), power = c(0.8, 1.4))) |
| 197 |
#' |
| 198 |
#' ## multi-hue sequential palette with small hue range and triangular chroma vs. |
| 199 |
#' ## large hue range and linear chroma trajectory |
| 200 |
#' hclplot(sequential_hcl(7, h = c(260, 220), c = c(50, 75, 0), l = c(30, 95), power = 1)) |
| 201 |
#' hclplot(sequential_hcl(7, h = c(260, 60), c = 60, l = c(40, 95), power = 1)) |
| 202 |
#' |
| 203 |
#' ## balanced diverging palette constructed from two simple single-hue sequential |
| 204 |
#' ## palettes (for hues 260/blue and 0/red) |
| 205 |
#' hclplot(diverging_hcl(7, h = c(260, 0), c = 80, l = c(35, 95), power = 1)) |
| 206 |
#' |
| 207 |
#' ## to register a particular adapted palette for re-use in the same session |
| 208 |
#' ## with a new name the register=... argument can be used once, e.g., |
| 209 |
#' diverging_hcl(7, palette = "Tropic", h2 = 0, register = "mytropic") |
| 210 |
#' |
| 211 |
#' ## subsequently palete="mytropic" is available in diverging_hcl() and the diverging |
| 212 |
#' ## ggplot2 scales such as scale_color_continuous_diverging() etc. |
| 213 |
#' demoplot(diverging_hcl(11, "mytropic"), type = "map") |
| 214 |
#' |
| 215 |
#' ## to register this palette in all R sessions you could place the following |
| 216 |
#' ## code in a startup script (e.g., .Rprofile): |
| 217 |
#' ## colorspace::diverging_hcl(7, palette = "Tropic", h2 = 0, register = "mytropic") |
| 218 |
#' |
| 219 |
#' @export |
| 220 |
hcl_palettes <- function(type = NULL, palette = NULL, plot = FALSE, n = 5L, ...) |
| 221 |
{ |
| 222 |
## subset by type and name (by flexible matching) |
| 223 |
fx <- function(n) tolower(gsub("[-, _, \\,, (, ), \\ , \\.]", "", n)) |
| 224 |
pals <- .colorspace_get_info("hcl_pals") |
| 225 |
if(!is.null(type)) { |
| 226 |
tytab <- c("sequential", fx(levels(pals$type))) |
| 227 |
type <- lapply(type, function(ty) { |
| 228 |
ty <- startsWith(tytab, fx(ty)) |
| 229 |
if(all(!ty)) stop("Palette 'type' should be one of: ", paste(levels(pals$type), collapse = ", ")) |
| 230 |
ty <- tytab[which(ty)[1L]] |
| 231 |
if(ty == "sequential") ty <- c("sequentialsinglehue", "sequentialmultihue") |
| 232 |
return(ty) |
| 233 |
}) |
| 234 |
type <- unlist(type) |
| 235 |
type <- levels(pals$type)[tytab[-1L] %in% type] |
| 236 |
pals <- pals[as.character(pals$type) %in% type, , drop = FALSE] |
| 237 |
} else { |
| 238 |
pals <- pals |
| 239 |
} |
| 240 |
if(!is.null(palette)) { |
| 241 |
namtab <- fx(rownames(pals)) |
| 242 |
palette <- sapply(fx(palette), function(n) { |
| 243 |
if(n %in% namtab) return(n) |
| 244 |
n <- startsWith(namtab, n) |
| 245 |
if(all(!n)) stop("Named 'palette' should be one of: ", paste(rownames(pals), collapse = ", ")) |
| 246 |
namtab[which(n)[1L]] |
| 247 |
}) |
| 248 |
pals <- pals[fx(rownames(pals)) %in% palette, , drop = FALSE] |
| 249 |
} |
| 250 |
|
| 251 |
## add class and show selection |
| 252 |
class(pals) <- c("hcl_palettes", "data.frame") |
| 253 |
if(plot) { |
| 254 |
plot(pals, n = n, ...) |
| 255 |
invisible(pals) |
| 256 |
} else { |
| 257 |
return(pals) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
#' @rdname hcl_palettes |
| 263 |
#' @export |
| 264 |
print.hcl_palettes <- function(x, ...) { |
| 265 |
if(nrow(x) > 1L) { |
| 266 |
type <- unique(as.character(x$type)) |
| 267 |
cat("HCL palettes\n") |
| 268 |
for(ty in type) { |
| 269 |
cat("\nType: ", ty, "\n") |
| 270 |
cat("Names: ") |
| 271 |
writeLines(strwrap(paste(rownames(x)[as.character(x$type) %in% ty], collapse = ", "), exdent = 7), ...) |
| 272 |
} |
| 273 |
} else { |
| 274 |
cat("HCL palette") |
| 275 |
cat("\nName:", rownames(x)) |
| 276 |
cat("\nType:", as.character(x$type)) |
| 277 |
cat("\nParameter ranges:\n") |
| 278 |
print.data.frame(x[ , 2L:11L, drop = FALSE], row.names = FALSE, ...) |
| 279 |
} |
| 280 |
invisible(x) |
| 281 |
} |
| 282 |
|
| 283 |
#' @rdname hcl_palettes |
| 284 |
#' @export |
| 285 |
summary.hcl_palettes <- function(object, ...) { |
| 286 |
type <- unique(as.character(object$type)) |
| 287 |
cat("HCL palettes\n") |
| 288 |
for(ty in type) { |
| 289 |
cat("\nType:", ty, "\n") |
| 290 |
cat("Parameter ranges:\n") |
| 291 |
print.data.frame(object[as.character(object$type) %in% ty, -1L, drop = FALSE], ...) |
| 292 |
} |
| 293 |
invisible(object) |
| 294 |
} |
| 295 |
|
| 296 |
#' @rdname hcl_palettes |
| 297 |
#' @method plot hcl_palettes |
| 298 |
#' @export |
| 299 |
plot.hcl_palettes <- function(x, n = 5L, fixup = TRUE, off = NULL, border = NULL, ...) |
| 300 |
{ |
| 301 |
typ <- c("Qualitative", "Sequential (single-hue)", "Sequential (multi-hue)", "Diverging", "Diverging (flexible)") |
| 302 |
x$type <- as.character(x$type) |
| 303 |
xx <- as.matrix(x[, -1L]) |
| 304 |
|
| 305 |
qcol <- sapply(which(x$type == typ[1L]), function(i) { |
| 306 |
qualitative_hcl(n = n, h1 = xx[i, "h1"], h2 = xx[i, "h2"], c1 = xx[i, "c1"], l1 = xx[i, "l1"], fixup = fixup) |
| 307 |
}) |
| 308 |
qcol <- if(length(qcol) < 1L) NULL else matrix(t(qcol), ncol = n, |
| 309 |
dimnames = list(rownames(x)[x$type == typ[1L]], paste("Color", 1L:n))) |
| 310 |
|
| 311 |
scol <- sapply(which(x$type == typ[2L]), function(i) { |
| 312 |
sequential_hcl(n = n, h1 = xx[i, "h1"], c1 = xx[i, "c1"], c2 = xx[i, "c2"], l1 = xx[i, "l1"], l2 = xx[i, "l2"], |
| 313 |
p1 = xx[i, "p1"], p2 = xx[i, "p2"], cmax = xx[i, "cmax"], fixup = fixup) |
| 314 |
}) |
| 315 |
scol <- if(length(scol) < 1L) NULL else matrix(t(scol), ncol = n, |
| 316 |
dimnames = list(rownames(x)[x$type == typ[2L]], paste("Color", 1L:n))) |
| 317 |
|
| 318 |
mcol <- sapply(which(x$type == typ[3L]), function(i) { |
| 319 |
sequential_hcl(n = n, h1 = xx[i, "h1"], h2 = xx[i, "h2"], c1 = xx[i, "c1"], c2 = xx[i, "c2"], l1 = xx[i, "l1"], l2 = xx[i, "l2"], |
| 320 |
p1 = xx[i, "p1"], p2 = xx[i, "p2"], cmax = xx[i, "cmax"], fixup = fixup) |
| 321 |
}) |
| 322 |
mcol <- if(length(mcol) < 1L) NULL else matrix(t(mcol), ncol = n, |
| 323 |
dimnames = list(rownames(x)[x$type == typ[3L]], paste("Color", 1L:n))) |
| 324 |
|
| 325 |
dcol <- sapply(which(x$type == typ[4L]), function(i) { |
| 326 |
diverging_hcl(n = n, h1 = xx[i, "h1"], h2 = xx[i, "h2"], c1 = xx[i, "c1"], l1 = xx[i, "l1"], l2 = xx[i, "l2"], |
| 327 |
p1 = xx[i, "p1"], p2 = xx[i, "p2"], cmax = xx[i, "cmax"], fixup = fixup) |
| 328 |
}) |
| 329 |
dcol <- if(length(dcol) < 1L) NULL else matrix(t(dcol), ncol = n, |
| 330 |
dimnames = list(rownames(x)[x$type == typ[4L]], paste("Color", 1L:n))) |
| 331 |
|
| 332 |
xcol <- sapply(which(x$type == typ[5L]), function(i) { |
| 333 |
divergingx_hcl(n = n, h1 = xx[i, "h1"], h2 = xx[i, "h2"], h3 = xx[i, "h3"], |
| 334 |
c1 = xx[i, "c1"], c2 = xx[i, "c2"], c3 = xx[i, "c3"], |
| 335 |
l1 = xx[i, "l1"], l2 = xx[i, "l2"], l3 = xx[i, "l3"], |
| 336 |
p1 = xx[i, "p1"], p2 = xx[i, "p2"], p3 = xx[i, "p3"], p4 = xx[i, "p4"], |
| 337 |
cmax1 = xx[i, "cmax1"], cmax2 = xx[i, "cmax2"], |
| 338 |
fixup = fixup) |
| 339 |
}) |
| 340 |
xcol <- if(length(xcol) < 1L) NULL else matrix(t(xcol), ncol = n, |
| 341 |
dimnames = list(rownames(x)[x$type == typ[5L]], paste("Color", 1L:n))) |
| 342 |
|
| 343 |
## collect colors |
| 344 |
col <- list(qcol, scol, mcol, dcol, xcol) |
| 345 |
names(col) <- typ |
| 346 |
col <- col[!sapply(col, is.null)] |
| 347 |
|
| 348 |
swatchplot(col, off = off, border = border, ...) |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
#' @rdname hcl_palettes |
| 353 |
#' @export |
| 354 |
qualitative_hcl <- function(n, h = c(0, 360 * (n - 1)/n), c = 80, l = 60, |
| 355 |
fixup = TRUE, alpha = 1, palette = NULL, rev = FALSE, register = "", ..., h1, h2, c1, l1) |
| 356 |
{ |
| 357 |
## edge cases |
| 358 |
if(n < 1L) return(character(0L)) |
| 359 |
|
| 360 |
## process HCL coordinates: (1) palette, (2) h/c/l, (3) h1/h2/... |
| 361 |
## (1) palette |
| 362 |
if(is.character(h)) palette <- h |
| 363 |
pals <- if(!is.null(palette)) { |
| 364 |
as.matrix(hcl_palettes(type = "Qualitative", palette = palette)[, 2L:11L])[1L, ] |
| 365 |
} else { |
| 366 |
structure(c(if(length(h) < 2L) c(h, NA) else rep_len(h, 2L), c[1L], NA, l[1L], NA, NA, NA, NA, 1), names = vars.pal[1L:10L]) |
| 367 |
} |
| 368 |
## (2) h/c/l |
| 369 |
if(!missing(h) && !is.character(h)) { |
| 370 |
h <- if(length(h) < 2L) c(h, NA) else rep_len(h, 2L) |
| 371 |
pals["h1"] <- h[1L] |
| 372 |
pals["h2"] <- h[2L] |
| 373 |
} |
| 374 |
if(!missing(c)) pals["c1"] <- c |
| 375 |
if(!missing(l)) pals["l1"] <- l |
| 376 |
if(!missing(fixup)) pals["fixup"] <- as.logical(fixup) |
| 377 |
## (3) h1/h2/... |
| 378 |
if(!missing(h1)) pals["h1"] <- h1 |
| 379 |
if(!missing(h2)) pals["h2"] <- h2 |
| 380 |
if(!missing(c1)) pals["c1"] <- c1 |
| 381 |
if(!missing(l1)) pals["l1"] <- l1 |
| 382 |
|
| 383 |
## register custom palette? |
| 384 |
if(is.character(register) && nchar(register) > 0L) { |
| 385 |
add_hcl_pals(palette = register, type = "Qualitative", parameters = pals) |
| 386 |
register <- TRUE |
| 387 |
} else { |
| 388 |
register <- FALSE |
| 389 |
} |
| 390 |
|
| 391 |
## explicitly expand h2 if still NA |
| 392 |
if(is.na(pals["h2"])) pals["h2"] <- pals["h1"] + 360 * (n - 1)/n |
| 393 |
|
| 394 |
## HCL trajectory |
| 395 |
rval <- hex(polarLUV( |
| 396 |
L = pals["l1"], |
| 397 |
C = pals["c1"], |
| 398 |
H = seq(pals["h1"], pals["h2"], length.out = n)), |
| 399 |
fixup = as.logical(pals["fixup"]), ...) |
| 400 |
|
| 401 |
## alpha transparency |
| 402 |
if(!missing(alpha)) { |
| 403 |
alpha <- pmax(pmin(alpha, 1), 0) |
| 404 |
alpha <- format(as.hexmode(round(alpha * 255 + 0.0001)), width = 2L, upper.case = TRUE) |
| 405 |
rval <- ifelse(is.na(rval), NA, paste(rval, alpha, sep = "")) |
| 406 |
} |
| 407 |
|
| 408 |
## return value |
| 409 |
if(rev) rval <- rev(rval) |
| 410 |
if(register) invisible(rval) else return(rval) |
| 411 |
} |
| 412 |
|
| 413 |
## trajectories |
| 414 |
lintrj <- function(i, p1, p2) p2 - (p2 - p1) * i |
| 415 |
tritrj <- function(i, j, p1, p2, pm) ifelse(i <= j, |
| 416 |
p2 - (p2 - pm) * i/j, |
| 417 |
pm - (pm - p1) * abs((i - j)/(1 - j))) |
| 418 |
|
| 419 |
## HCL sequence |
| 420 |
seqhcl <- function(i, h1, h2, c1, c2, l1, l2, p1, p2, cmax, fixup, ...) { |
| 421 |
j <- 1/(1 + abs(cmax - c1) / abs(cmax - c2)) |
| 422 |
if (!is.na(j) && (j <= 0 | j >= 1)) j <- NA |
| 423 |
hex(polarLUV( |
| 424 |
L = lintrj(i^p2, l1, l2), |
| 425 |
C = if(is.na(j)) lintrj(i^p1, c1, c2) else tritrj(i^p1, j, c1, c2, cmax), |
| 426 |
H = lintrj(i, h1, h2)), |
| 427 |
fixup = fixup, ... |
| 428 |
) |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
#' @rdname hcl_palettes |
| 433 |
#' @export |
| 434 |
sequential_hcl <- function(n, h = 260, c = 80, l = c(30, 90), power = 1.5, |
| 435 |
gamma = NULL, fixup = TRUE, alpha = 1, palette = NULL, rev = FALSE, register = "", ..., |
| 436 |
h1, h2, c1, c2, l1, l2, p1, p2, cmax, c.) |
| 437 |
{ |
| 438 |
## edge cases |
| 439 |
if (!is.null(gamma)) warning("'gamma' is deprecated and has no effect") |
| 440 |
if(n < 1L) return(character(0L)) |
| 441 |
if(!missing(c.)) c <- c. |
| 442 |
if(length(c) == 3L) c <- c[c(1L, 3L, 2L)] |
| 443 |
|
| 444 |
## process HCL coordinates: (1) palette, (2) h/c/l, (3) h1/h2/... |
| 445 |
## (1) palette |
| 446 |
if(is.character(h)) palette <- h |
| 447 |
pals <- if(!is.null(palette)) { |
| 448 |
as.matrix(hcl_palettes(type = "Sequential", palette = palette)[, 2L:11L])[1L, ] |
| 449 |
} else { |
| 450 |
structure(c( |
| 451 |
if(length(h) < 2L) c(h, NA) else rep_len(h, 2L), |
| 452 |
if(length(c) < 2L) c(c, 0) else rep_len(c, 2L), |
| 453 |
rep_len(l, 2L), |
| 454 |
if(length(power) < 2L) c(power, NA) else rep_len(power, 2L), |
| 455 |
if(length(c) < 3L) NA else c[3L], |
| 456 |
1), names = vars.pal) |
| 457 |
} |
| 458 |
## (2) h/c/l |
| 459 |
if(!missing(h) && !is.character(h)) { |
| 460 |
h <- if(length(h) < 2L) c(h, NA) else rep_len(h, 2L) |
| 461 |
pals["h1"] <- h[1L] |
| 462 |
pals["h2"] <- h[2L] |
| 463 |
} |
| 464 |
if(!missing(c) || !missing(c.)) { |
| 465 |
if(length(c) < 2L) c <- c(c, 0) |
| 466 |
pals["c1"] <- c[1L] |
| 467 |
pals["c2"] <- c[2L] |
| 468 |
if(length(c) == 3L) pals["cmax"] <- c[3L] |
| 469 |
} |
| 470 |
if(!missing(l)) { |
| 471 |
l <- rep_len(l, 2L) |
| 472 |
pals["l1"] <- l[1L] |
| 473 |
pals["l2"] <- l[2L] |
| 474 |
} |
| 475 |
if(!missing(power)) { |
| 476 |
power <- if(length(power) < 2L) c(power, NA) else rep_len(power, 2L) |
| 477 |
pals["p1"] <- power[1L] |
| 478 |
pals["p2"] <- power[2L] |
| 479 |
} |
| 480 |
if(!missing(fixup)) pals["fixup"] <- as.logical(fixup) |
| 481 |
## (3) h1/h2/... |
| 482 |
if(!missing(h1)) pals["h1"] <- h1 |
| 483 |
if(!missing(h2)) pals["h2"] <- h2 |
| 484 |
if(!missing(c1)) pals["c1"] <- c1 |
| 485 |
if(!missing(c2)) pals["c2"] <- c2 |
| 486 |
if(!missing(l1)) pals["l1"] <- l1 |
| 487 |
if(!missing(l2)) pals["l2"] <- l2 |
| 488 |
if(!missing(p1)) pals["p1"] <- p1 |
| 489 |
if(!missing(p2)) pals["p2"] <- p2 |
| 490 |
if(!missing(cmax)) pals["cmax"] <- cmax |
| 491 |
if(!is.na(pals["h2"]) && pals["h1"] == pals["h2"]) pals["h2"] <- NA |
| 492 |
|
| 493 |
## register custom palette? |
| 494 |
if(is.character(register) && nchar(register) > 0L) { |
| 495 |
add_hcl_pals(palette = register, |
| 496 |
type = if(is.na(pals["h2"])) "Sequential (single-hue)" else "Sequential (multi-hue)", |
| 497 |
parameters = pals) |
| 498 |
register <- TRUE |
| 499 |
} else { |
| 500 |
register <- FALSE |
| 501 |
} |
| 502 |
|
| 503 |
## expand parameters that are potentially NA |
| 504 |
if(is.na(pals["h2"])) pals["h2"] <- pals["h1"] |
| 505 |
if(is.na(pals["c2"])) pals["c2"] <- 0 |
| 506 |
if(is.na(pals["p2"])) pals["p2"] <- pals["p1"] |
| 507 |
|
| 508 |
## HCL trajectory |
| 509 |
rval <- seq(1, 0, length.out = n) |
| 510 |
rval <- seqhcl(rval, pals["h1"], pals["h2"], pals["c1"], pals["c2"], pals["l1"], pals["l2"], pals["p1"], pals["p2"], pals["cmax"], as.logical(pals["fixup"]), ...) |
| 511 |
|
| 512 |
## alpha transparency |
| 513 |
if(!missing(alpha)) { |
| 514 |
alpha <- pmax(pmin(alpha, 1), 0) |
| 515 |
alpha <- format(as.hexmode(round(alpha * 255 + 0.0001)), width = 2L, upper.case = TRUE) |
| 516 |
rval <- ifelse(is.na(rval), NA, paste(rval, alpha, sep = "")) |
| 517 |
} |
| 518 |
|
| 519 |
## return value |
| 520 |
if(rev) rval <- rev(rval) |
| 521 |
if(register) invisible(rval) else return(rval) |
| 522 |
} |
| 523 |
|
| 524 |
#' @rdname hcl_palettes |
| 525 |
#' @export |
| 526 |
diverging_hcl <- function(n, h = c(260, 0), c = 80, l = c(30, 90), power = 1.5, |
| 527 |
gamma = NULL, fixup = TRUE, alpha = 1, palette = NULL, rev = FALSE, register = "", ..., |
| 528 |
h1, h2, c1, l1, l2, p1, p2, cmax) |
| 529 |
{ |
| 530 |
## edge cases |
| 531 |
if (!is.null(gamma)) warning("'gamma' is deprecated and has no effect") |
| 532 |
if(n < 1L) return(character(0L)) |
| 533 |
|
| 534 |
## process HCL coordinates: (1) palette, (2) h/c/l, (3) h1/h2/... |
| 535 |
## (1) palette |
| 536 |
if(is.character(h)) palette <- h |
| 537 |
pals <- if(!is.null(palette)) { |
| 538 |
as.matrix(hcl_palettes(type = "Diverging", palette = palette)[, 2L:11L])[1L, ] |
| 539 |
} else { |
| 540 |
structure(c( |
| 541 |
rep_len(h, 2L), |
| 542 |
c(c[1L], NA), |
| 543 |
rep_len(l, 2L), |
| 544 |
if(length(power) < 2L) c(power, NA) else rep_len(power, 2L), |
| 545 |
if(length(c) > 1L) c[2L] else NA, |
| 546 |
1), names = vars.pal) |
| 547 |
} |
| 548 |
## (2) h/c/l |
| 549 |
if(!missing(h) && !is.character(h)) { |
| 550 |
h <- rep_len(h, 2L) |
| 551 |
pals["h1"] <- h[1L] |
| 552 |
pals["h2"] <- h[2L] |
| 553 |
} |
| 554 |
if(!missing(c)) { |
| 555 |
pals["c1"] <- c[1L] |
| 556 |
if(length(c) > 1L) pals["cmax"] <- c[2L] |
| 557 |
} |
| 558 |
if(!missing(l)) { |
| 559 |
l <- rep_len(l, 2L) |
| 560 |
pals["l1"] <- l[1L] |
| 561 |
pals["l2"] <- l[2L] |
| 562 |
} |
| 563 |
if(!missing(power)) { |
| 564 |
power <- if(length(power) < 2L) c(power, NA) else rep_len(power, 2L) |
| 565 |
pals["p1"] <- power[1L] |
| 566 |
pals["p2"] <- power[2L] |
| 567 |
} |
| 568 |
if(!missing(fixup)) pals["fixup"] <- as.logical(fixup) |
| 569 |
## (3) h1/h2/... |
| 570 |
if(!missing(h1)) pals["h1"] <- h1 |
| 571 |
if(!missing(h2)) pals["h2"] <- h2 |
| 572 |
if(!missing(c1)) pals["c1"] <- c1 |
| 573 |
if(!missing(l1)) pals["l1"] <- l1 |
| 574 |
if(!missing(l2)) pals["l2"] <- l2 |
| 575 |
if(!missing(p1)) pals["p1"] <- p1 |
| 576 |
if(!missing(p2)) pals["p2"] <- p2 |
| 577 |
if(!missing(cmax)) pals["cmax"] <- cmax |
| 578 |
pals["c2"] <- NA |
| 579 |
|
| 580 |
## register custom palette? |
| 581 |
if(is.character(register) && nchar(register) > 0L) { |
| 582 |
add_hcl_pals(palette = register, type = "Diverging", parameters = pals) |
| 583 |
register <- TRUE |
| 584 |
} else { |
| 585 |
register <- FALSE |
| 586 |
} |
| 587 |
|
| 588 |
## expand parameters that are potentially NA |
| 589 |
if(is.na(pals["p2"])) pals["p2"] <- pals["p1"] |
| 590 |
|
| 591 |
## HCL trajectory |
| 592 |
n2 <- ceiling(n/2) |
| 593 |
rval <- if(n == 1L) 0 else pmax(0, seq.int(1, by = -2/(n - 1), length.out = n2)) |
| 594 |
rval <- c(seqhcl(rval, pals["h1"], pals["h1"], pals["c1"], 0, pals["l1"], pals["l2"], pals["p1"], pals["p2"], pals["cmax"], as.logical(pals["fixup"]), ...), |
| 595 |
rev(seqhcl(rval, pals["h2"], pals["h2"], pals["c1"], 0, pals["l1"], pals["l2"], pals["p1"], pals["p2"], pals["cmax"], as.logical(pals["fixup"]), ...))) |
| 596 |
if(floor(n/2) < n2) rval <- rval[-n2] |
| 597 |
|
| 598 |
## alpha transparency |
| 599 |
if(!missing(alpha)) { |
| 600 |
alpha <- pmax(pmin(alpha, 1), 0) |
| 601 |
alpha <- format(as.hexmode(round(alpha * 255 + 0.0001)), width = 2L, upper.case = TRUE) |
| 602 |
rval <- ifelse(is.na(rval), NA, paste(rval, alpha, sep = "")) |
| 603 |
} |
| 604 |
|
| 605 |
## return value |
| 606 |
if(rev) rval <- rev(rval) |
| 607 |
if(register) invisible(rval) else return(rval) |
| 608 |
} |
| 609 |
|
| 610 |
#' @rdname hcl_palettes |
| 611 |
#' @usage NULL |
| 612 |
#' @export |
| 613 |
diverge_hcl <- diverging_hcl |
| 614 |
|
| 615 |
|
| 616 |
# ------------------------------------------------------------------- |
| 617 |
# Palette specifications |
| 618 |
# ------------------------------------------------------------------- |
| 619 |
|
| 620 |
vars.pal <- c("h1", "h2", "c1", "c2", "l1", "l2", "p1", "p2", "cmax", "fixup") |
| 621 |
# Inspired by: |
| 622 |
qual.pals <- list() |
| 623 |
qual.pals[["Pastel 1"]] <- c( 0, NA, 35, NA, 85, NA, NA, NA, NA, 1) # ColorBrewer.org: Pastel1 |
| 624 |
qual.pals[["Dark 2"]] <- c( 0, NA, 50, NA, 60, NA, NA, NA, NA, 1) # ColorBrewer.org: Dark2 |
| 625 |
qual.pals[["Dark 3"]] <- c( 0, NA, 80, NA, 60, NA, NA, NA, NA, 1) # JCF/Z: ~Dark2 with more chroma |
| 626 |
qual.pals[["Set 2"]] <- c( 0, NA, 60, NA, 70, NA, NA, NA, NA, 1) # ColorBrewer.org: Set2 |
| 627 |
qual.pals[["Set 3"]] <- c( 10, NA, 50, NA, 80, NA, NA, NA, NA, 1) # ColorBrewer.org: Set3 |
| 628 |
qual.pals[["Warm"]] <- c( 90, -30, 50, NA, 70, NA, NA, NA, NA, 1) # Z+KH+PM-09, Fig.4: Warm (based on Ihaka-03) |
| 629 |
qual.pals[["Cold"]] <- c(270, 150, 50, NA, 70, NA, NA, NA, NA, 1) # Z+KH+PM-09, Fig.4: Cold (based on Ihaka-03) |
| 630 |
qual.pals[["Harmonic"]] <- c( 60, 240, 50, NA, 70, NA, NA, NA, NA, 1) # Z+KH+PM-09, Fig.4: Harmonic (based on Ihaka-03) |
| 631 |
qual.pals[["Dynamic"]] <- c( 30, NA, 50, NA, 70, NA, NA, NA, NA, 1) # Z+KH+PM-09, Fig.4: Dynamic (based on Ihaka-03) |
| 632 |
|
| 633 |
seqs.pals <- list() |
| 634 |
seqs.pals[["Grays"]] <- c( 0, NA, 0, NA, 10, 98, 1.3, NA, NA, 1) # ColorBrewer.org: Greys |
| 635 |
seqs.pals[["Light Grays"]] <- c( 0, NA, 0, NA, 30, 90, 1.5, NA, NA, 1) # Z+KH+PM-09, Fig.5: Light Grays |
| 636 |
seqs.pals[["Blues 2"]] <- c(260, NA, 80, NA, 30, 90, 1.5, NA, NA, 1) # Z+KH+PM-09, Fig.5: Blues |
| 637 |
seqs.pals[["Blues 3"]] <- c(245, NA, 50, NA, 20, 98, 0.8, 1.4, 75, 1) # ColorBrewer.org: Blues |
| 638 |
seqs.pals[["Purples 2"]] <- c(270, NA, 70, NA, 25, 95, 1.2, NA, NA, 1) # ColorBrewer.org: Purples |
| 639 |
seqs.pals[["Purples 3"]] <- c(270, NA, 50, NA, 20, 98, 0.9, 1.4, 75, 1) # ColorBrewer.org: Purples |
| 640 |
seqs.pals[["Reds 2"]] <- c( 10, NA, 85, NA, 25, 95, 1.3, NA, NA, 1) # ColorBrewer.org: Reds |
| 641 |
seqs.pals[["Reds 3"]] <- c( 10, NA, 65, NA, 20, 97, 1.1, 1.3, 150, 1) # ColorBrewer.org: Reds |
| 642 |
seqs.pals[["Greens 2"]] <- c(135, NA, 45, NA, 35, 95, 1.3, NA, NA, 1) # ColorBrewer.org: Greens |
| 643 |
seqs.pals[["Greens 3"]] <- c(135, NA, 35, NA, 25, 98, 1.0, 1.5, 70, 1) # ColorBrewer.org: Greens |
| 644 |
seqs.pals[["Oslo"]] <- c(250, NA, 0, 0, 99, 1, 1.0, NA, 70, 1) # scico: oslo |
| 645 |
|
| 646 |
seqm.pals <- list() |
| 647 |
seqm.pals[["Purple-Blue"]] <- c(300, 200, 60, 0, 25, 95, 0.7, 1.3, NA, 1) # ColorBrewer.org: BuPu |
| 648 |
seqm.pals[["Red-Purple"]] <- c( 10, -80, 80, 5, 25, 95, 0.7, 1.3, NA, 1) # ColorBrewer.org: PuRd |
| 649 |
seqm.pals[["Red-Blue"]] <- c( 0, -100, 80, 40, 40, 75, 1.0, 1.0, NA, 1) # Z+KH+PM-09, Fig.5: Red-Blue |
| 650 |
seqm.pals[["Purple-Orange"]]<-c(-83, 20, 65, 18, 32, 90, 0.5, 1.0, NA, 1) # CARTO: PurpOr |
| 651 |
seqm.pals[["Purple-Yellow"]]<-c(320, 80, 60, 20, 30, 95, 0.7, 1.3, 65, 1) # RS+GM+MD+Z-15, similar to Fig.4: Precipitation |
| 652 |
seqm.pals[["Blue-Yellow"]] <- c(265, 80, 60, 10, 25, 95, 0.7, 2.0, NA, 1) # ColorBrewer.org: YlGnBu |
| 653 |
seqm.pals[["Green-Yellow"]]<- c(140, 80, 50, 10, 40, 97, 0.7, 1.8, NA, 1) # ColorBrewer.org: YlGn |
| 654 |
seqm.pals[["Red-Yellow"]] <- c( 10, 85, 80, 10, 25, 95, 0.4, 1.3, NA, 1) # ColorBrewer.org: YlOrRd |
| 655 |
seqm.pals[["Heat"]] <- c( 0, 90, 80, 30, 30, 90, 0.2, 2.0, NA, 1) # JCF/Z: alternative to heat_hcl |
| 656 |
seqm.pals[["Heat 2"]] <- c( 0, 90, 100, 30, 50, 90, 0.2, 1.0, NA, 1) # Z+KH+PM-09, Fig.5: heat_hcl |
| 657 |
seqm.pals[["Terrain"]] <- c(130, 0, 80, 0, 60, 95, 0.1, 1.0, NA, 1) # Z+KH+PM-09, Fig.5: terrain_hcl |
| 658 |
seqm.pals[["Terrain 2"]] <- c(130, 30, 65, 0, 45, 90, 0.5, 1.5, NA, 1) # JCF/Z: alternative to terrain_hcl |
| 659 |
|
| 660 |
seqm.pals[["Viridis"]] <- c(300, 75, 40, 95, 15, 90, 1.0, 1.1, NA, 1) # viridis::viridis |
| 661 |
seqm.pals[["Plasma"]] <-c(-100, 100, 60,100, 15, 95, 2.0, 0.9, NA, 1) # viridis::plasma |
| 662 |
seqm.pals[["Inferno"]] <-c(-100, 85, 0, 65, 1, 98, 1.1, 0.9, 120, 1) # viridis::inferno |
| 663 |
seqm.pals[["Rocket"]] <- c(-70, 60, 0, 10, 2, 97, 0.8, 0.8, 130, 1) # viridis::rocket |
| 664 |
seqm.pals[["Mako"]] <- c(325, 130, 0, 18, 2, 95, 1.0, 1.0, 70, 1) # viridis::mako |
| 665 |
|
| 666 |
seqm.pals[["Dark Mint"]] <- c(240, 130, 30, 33, 25, 95, 1.0, NA, NA, 1) # CARTO: Dark Mint |
| 667 |
seqm.pals[["Mint"]] <- c(205, 140, 40, 12, 34, 94, 0.5, 1.0, NA, 1) # CARTO: Mint |
| 668 |
seqm.pals[["BluGrn"]] <- c(215, 120, 25, 30, 31, 88, 0.7, 1.1, 45, 1) # CARTO: BluGrn |
| 669 |
seqm.pals[["Teal"]] <- c(240, 180, 35, 15, 35, 92, 0.6, 1.1, 40, 1) # CARTO: Teal |
| 670 |
seqm.pals[["TealGrn"]] <- c(220, 125, 44, 50, 49, 90, 0.8, 1.2, 60, 1) # CARTO: TealGrn |
| 671 |
seqm.pals[["Emrld"]] <- c(224, 105, 23, 55, 25, 92, 1.5, 1.0, NA, 1) # CARTO: Emrld |
| 672 |
seqm.pals[["BluYl"]] <- c(250, 90, 40, 55, 33, 98, 0.5, 1.0, NA, 1) # CARTO: BluYl |
| 673 |
seqm.pals[["ag_GrnYl"]] <- c(225, 87, 27, 86, 34, 92, 0.9, NA, NA, 1) # CARTO: ag_GrnYl |
| 674 |
seqm.pals[["Peach"]] <- c( 15, 50, 128, 30, 55, 90, 1.1, NA, NA, 1) # CARTO: Peach |
| 675 |
seqm.pals[["PinkYl"]] <- c( -4, 80, 100, 47, 55, 96, 1.0, NA, NA, 1) # CARTO: PinkYl |
| 676 |
seqm.pals[["Burg"]] <- c(-10, 10, 40, 40, 25, 85, 1.2, 1.0, 75, 1) # CARTO: Burg |
| 677 |
seqm.pals[["BurgYl"]] <- c(-10, 55, 45, 30, 30, 90, 0.7, 1.0, 80, 1) # CARTO: BurgYl |
| 678 |
seqm.pals[["RedOr"]] <- c( -3, 53, 75, 42, 44, 86, 0.8, 1.0, 90, 1) # CARTO: RedOr |
| 679 |
seqm.pals[["OrYel"]] <- c( 5, 72, 120, 49, 56, 87, 1.0, NA, 125, 1) # CARTO: OrYel |
| 680 |
seqm.pals[["Purp"]] <- c(270, 300, 55, 20, 42, 92, 0.6, 1.0, 60, 1) # CARTO: Purp |
| 681 |
seqm.pals[["PurpOr"]] <- c(-83, 20, 55, 18, 32, 90, 0.6, 1.0, 65, 1) # CARTO: PurpOr |
| 682 |
seqm.pals[["Sunset"]] <- c(-80, 78, 60, 55, 40, 91, 0.8, 1.0, 75, 1) # CARTO: Sunset |
| 683 |
seqm.pals[["Magenta"]] <- c(312, 358, 50, 24, 27, 85, 0.6, 1.1, 65, 1) # CARTO: Magenta |
| 684 |
seqm.pals[["SunsetDark"]] <- c(-35, 50, 55, 60, 30, 90, 1.2, 1.0, 120, 1) # CARTO: SunsetDark |
| 685 |
seqm.pals[["ag_Sunset"]] <- c(-85, 70, 70, 45, 25, 85, 0.6, 1.0, 105, 1) # CARTO: ag_Sunset |
| 686 |
seqm.pals[["BrwnYl"]] <- c(-20, 70, 30, 20, 20, 90, 1.0, 1.1, 60, 1) # CARTO: BrwnYl |
| 687 |
|
| 688 |
seqm.pals[["YlOrRd"]] <- c( 5, 85, 75, 40, 25, 99, 1.6, 1.3, 180, 1) # ColorBrewer.org: YlOrRd |
| 689 |
seqm.pals[["YlOrBr"]] <- c( 20, 85, 50, 20, 25, 99, 1.3, 1.5, 150, 1) # ColorBrewer.org: YlOrBr |
| 690 |
seqm.pals[["OrRd"]] <- c( 0, 60, 90, 10, 25, 97, 1.0, 1.5, 135, 1) # ColorBrewer.org: OrRd |
| 691 |
seqm.pals[["Oranges"]] <- c( 20, 55, 70, 10, 30, 97, 1.2, 1.3, 150, 1) # ColorBrewer.org: Oranges |
| 692 |
seqm.pals[["YlGn"]] <- c(160, 85, 25, 20, 25, 99, 1.2, 1.6, 70, 1) # ColorBrewer.org: YlGn |
| 693 |
seqm.pals[["YlGnBu"]] <- c(270, 90, 40, 25, 15, 99, 2.0, 1.5, 90, 1) # ColorBrewer.org: YlGnBu |
| 694 |
seqm.pals[["Reds"]] <- c( 0, 35, 65, 5, 20, 97, 1.1, 1.3, 150, 1) # ColorBrewer.org: Reds |
| 695 |
seqm.pals[["RdPu"]] <- c(-70, 40, 45, 5, 15, 97, 1.0, 1.3, 100, 1) # ColorBrewer.org: RdPu |
| 696 |
seqm.pals[["PuRd"]] <- c( 20, -95, 60, 5, 20, 97, 1.6, 1.1, 140, 1) # ColorBrewer.org: PuRd |
| 697 |
seqm.pals[["Purples"]] <- c(275, 270, 55, 5, 20, 99, 1.3, 1.3, 70, 1) # ColorBrewer.org: Purples |
| 698 |
seqm.pals[["PuBuGn"]] <- c(160, 320, 25, 5, 25, 98, 1.4, 1.2, 70, 1) # ColorBrewer.org: PuBuGn |
| 699 |
seqm.pals[["PuBu"]] <- c(240, 260, 30, 5, 25, 98, 1.5, 1.2, 70, 1) # ColorBrewer.org: PuBu |
| 700 |
seqm.pals[["Greens"]] <- c(135, 115, 35, 5, 25, 98, 1.0, 1.5, 70, 1) # ColorBrewer.org: Greens |
| 701 |
seqm.pals[["BuGn"]] <- c(125, 200, 30, 5, 25, 98, 1.4, 1.6, 65, 1) # ColorBrewer.org: BuGn |
| 702 |
seqm.pals[["GnBu"]] <- c(265, 95, 55, 10, 25, 97, 1.3, 1.7, 80, 1) # ColorBrewer.org: GnBu |
| 703 |
seqm.pals[["BuPu"]] <- c(320, 200, 40, 5, 15, 98, 1.2, 1.3, 65, 1) # ColorBrewer.org: BuPu |
| 704 |
seqm.pals[["Blues"]] <- c(260, 220, 45, 5, 25, 98, 1.2, 1.3, 70, 1) # ColorBrewer.org: Blues |
| 705 |
seqm.pals[["Lajolla"]] <- c( 90, -20, 40, 5, 99, 5, 0.7, 0.8, 100, 1) # scico: lajolla |
| 706 |
seqm.pals[["Turku"]] <- c( 10, 120, 20, 0, 95, 1, 1.7, 0.8, 55, 1) # scico: turku |
| 707 |
seqm.pals[["Hawaii"]] <- c(-30, 200, 70, 35, 30, 92, 0.3, 1.0, 75, 1) # scico: hawaii |
| 708 |
seqm.pals[["Batlow"]] <- c(270, -40, 35, 35, 12, 88, 0.6, 1.1, 75, 1) # scico: batlow |
| 709 |
|
| 710 |
dive.pals <- list() |
| 711 |
dive.pals[["Blue-Red"]] <- c(260, 0, 80, NA, 30, 90, 1.5, NA, NA, 1) # Z+KH+PM-09, Fig.6: Blue-Red (high luminance contrast) |
| 712 |
dive.pals[["Blue-Red 2"]] <- c(260, 0, 100, NA, 50, 90, 1.0, NA, NA, 1) # Z+KH+PM-09, Fig.6: Blue-Red (medium luminance contrast) |
| 713 |
dive.pals[["Blue-Red 3"]] <- c(255, 12, 50, NA, 20, 97, 1.0, 1.3, 80, 1) # ColorBrewer.org: RdBu |
| 714 |
dive.pals[["Red-Green"]] <- c(340, 128, 60, NA, 30, 97, 0.8, 1.5, 80, 1) # ColorBrewer.org: PiYG |
| 715 |
dive.pals[["Purple-Green"]]<- c(300, 128, 30, NA, 20, 95, 1.0, 1.4, 65, 1) # ColorBrewer.org: PRGn |
| 716 |
dive.pals[["Purple-Brown"]]<- c(270, 40, 30, NA, 20, 98, 0.8, 1.2, 70, 1) # ColorBrewer.org: PuOr |
| 717 |
dive.pals[["Green-Brown"]] <- c(180, 55, 40, NA, 25, 97, 0.8, 1.4, 65, 1) # ColorBrewer.org: BrBG |
| 718 |
dive.pals[["Blue-Yellow 2"]]<-c(265, 80, 80, NA, 40, 95, 1.2, NA, NA, 1) # Z+COW |
| 719 |
dive.pals[["Blue-Yellow 3"]]<-c(265, 80, 80, NA, 70, 95, 0.5, 2.0, NA, 1) # Z+COW |
| 720 |
dive.pals[["Green-Orange"]]<- c(130, 43, 100, NA, 70, 90, 1.0, NA, NA, 1) # Z+KH+PM-09, Fig.6: Green-Orange (low luminance contrast) |
| 721 |
dive.pals[["Cyan-Magenta"]]<- c(180, 330, 59, NA, 75, 95, 1.5, NA, NA, 1) # Z+KH+PM-09, Fig.6: Blue-Red (low luminance contrast) |
| 722 |
dive.pals[["Tropic"]] <- c(195, 325, 70, NA, 55, 95, 1.0, NA, NA, 1) # CARTO: Tropic |
| 723 |
dive.pals[["Broc"]] <- c(240, 85, 30, NA, 15, 98, 0.9, NA, 45, 1) # scico: broc |
| 724 |
dive.pals[["Cork"]] <- c(245, 125, 30, NA, 15, 95, 0.9, 1.1, 55, 1) # scico: cork |
| 725 |
dive.pals[["Vik"]] <- c(240, 55, 45, NA, 15, 95, 0.8, 1.1, 65, 1) # scico: vik |
| 726 |
dive.pals[["Berlin"]] <- c(240, 15, 60, NA, 75, 5, 1.2, 1.5, 80, 1) # scico: berlin |
| 727 |
dive.pals[["Lisbon"]] <- c(240, 85, 30, NA, 98, 8, 1.0, NA, 45, 1) # scico: lisbon |
| 728 |
dive.pals[["Tofino"]] <- c(260, 120, 45, NA, 90, 5, 0.8, 1.0, 55, 1) # scico: tofino |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
base.pals <- list() |
| 733 |
base.pals[["rainbow"]] <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1) # Default RGB rainbow |
| 734 |
base.pals[["heat.colors"]] <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1) # Default heatmap |
| 735 |
base.pals[["topo.colors"]] <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1) # Default topo colors |
| 736 |
base.pals[["terrain.colors"]] <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1) # Default terrain colors |
| 737 |
base.pals[["cm.colors"]] <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1) # Default cyan magenta colors |
| 738 |
base.pals[["bpy"]] <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1) # Analogous to sp::bpy.colors |
| 739 |
|
| 740 |
## collect all hcl palettes |
| 741 |
make_hcl_pals <- function() { |
| 742 |
## collect all palettes by group |
| 743 |
qpals <- as.data.frame(do.call("rbind", qual.pals)) |
| 744 |
rownames(qpals) <- names(qual.pals) |
| 745 |
qpals$type <- "Qualitative" |
| 746 |
|
| 747 |
spals <- as.data.frame(do.call("rbind", seqs.pals)) |
| 748 |
rownames(spals) <- names(seqs.pals) |
| 749 |
spals$type <- "Sequential (single-hue)" |
| 750 |
|
| 751 |
mpals <- as.data.frame(do.call("rbind", seqm.pals)) |
| 752 |
rownames(mpals) <- names(seqm.pals) |
| 753 |
mpals$type <- "Sequential (multi-hue)" |
| 754 |
|
| 755 |
dpals <- as.data.frame(do.call("rbind", dive.pals)) |
| 756 |
rownames(dpals) <- names(dive.pals) |
| 757 |
dpals$type <- "Diverging" |
| 758 |
|
| 759 |
## combine and rearrange |
| 760 |
pals <- rbind(qpals, spals, mpals, dpals) |
| 761 |
names(pals) <- c(vars.pal, "type") |
| 762 |
pals$type <- factor(pals$type, levels = c("Qualitative", |
| 763 |
"Sequential (single-hue)", "Sequential (multi-hue)", "Diverging")) |
| 764 |
pals$fixup <- as.logical(pals$fixup) |
| 765 |
pals <- pals[, c("type", names(pals)[!names(pals) %in% "type"])] |
| 766 |
return(pals) |
| 767 |
} |
| 768 |
|
| 769 |
.colorspace_set_info( |
| 770 |
hcl_pals = make_hcl_pals() |
| 771 |
) |
| 772 |
|
| 773 |
add_hcl_pals <- function(palette, type, parameters) { |
| 774 |
pals <- .colorspace_get_info("hcl_pals") |
| 775 |
p <- data.frame(type = factor(type, levels = levels(pals$type))) |
| 776 |
p <- cbind(p, as.data.frame(as.list(parameters))) |
| 777 |
p$fixup <- as.logical(p$fixup) |
| 778 |
pals[palette, ] <- p |
| 779 |
pals <- pals[order(pals$type), ] |
| 780 |
.colorspace_set_info(hcl_pals = pals) |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
# ------------------------------------------------------------------- |
| 785 |
# Character vector specifying the example plots. For each element |
| 786 |
# the function plot_<name> will be called. |
| 787 |
# ------------------------------------------------------------------- |
| 788 |
example.plots <- c("Map", "Heatmap", "Scatter", "Spine", "Bar", |
| 789 |
"Pie", "Perspective", "Mosaic", "Lines", "Spectrum", "HCL Plot") |
| 790 |
|
| 791 |
# ------------------------------------------------------------------- |
| 792 |
# Helper function: returns a data.frame containing all |
| 793 |
# palettes specified above. Used for hclwizard and tcltk interface. |
| 794 |
# @param gui, `NULL` or logical. If ``NULL` all palettes will be |
| 795 |
# returned. If TRUE or FALSE the palettes will be subsetted and the |
| 796 |
# data.frame will be slightly modified to fulfill the requirements |
| 797 |
# for the graphical user interfaces (tcltk and shiny). |
| 798 |
# ------------------------------------------------------------------- |
| 799 |
GetPaletteConfig <- function(gui = NULL) { |
| 800 |
|
| 801 |
res <- NULL |
| 802 |
palettes <- list( |
| 803 |
qual = qual.pals, |
| 804 |
seqs = seqs.pals, |
| 805 |
seqm = seqm.pals, |
| 806 |
dive = dive.pals, |
| 807 |
base = base.pals |
| 808 |
) |
| 809 |
res <- lapply(names(palettes), function(type, palettes) { |
| 810 |
# Palette parameters to data.frame |
| 811 |
x <- as.data.frame(do.call(rbind, palettes[[type]])) |
| 812 |
names(x) <- toupper(vars.pal) |
| 813 |
# Append type |
| 814 |
cbind(data.frame("type" = rep(type, nrow(x))), x) |
| 815 |
}, palettes = palettes) |
| 816 |
# Return data.frame |
| 817 |
pals <- do.call(rbind, res) |
| 818 |
pals$type <- as.character(pals$type) |
| 819 |
|
| 820 |
if ( inherits(gui, "logical") ) { |
| 821 |
take <- c( |
| 822 |
# Qualitative |
| 823 |
"Pastel 1", "Dark 2", "Dark 3", "Set 2", "Set 3", "Warm", "Cold", "Harmonic", "Dynamic", |
| 824 |
# Diverging |
| 825 |
"Blue-Red", "Blue-Red 2", "Blue-Yellow 2", "Blue-Yellow 3", |
| 826 |
"Green-Orange", "Cyan-Magenta", "Tropic", |
| 827 |
# Diverging advanced |
| 828 |
"Blue-Red 3", "Red-Green", "Purple-Green", "Purple-Brown", |
| 829 |
"Green-Brown", "Cork", "Berlin", "Lisbon", "Tofino", |
| 830 |
# Sequential single-hue |
| 831 |
"Grays", "Light Grays", "Blues 2", "Purples 2", "Reds 2", "Greens 2", |
| 832 |
# Sequential single-hue advanced |
| 833 |
"Blues 3", "Purples 3", "Reds 3", "Greens 3", "Oslo", |
| 834 |
# Sequential multiple-hues |
| 835 |
"Purple-Blue", "Purple-Orange", "Red-Blue", "Red-Purple", |
| 836 |
"Red-Yellow", "Heat", "PinkYl", "Green-Yellow", "Terrain 2", |
| 837 |
"Dark Mint", "BluYl", "Blue-Yellow", "Viridis", "Plasma", |
| 838 |
# Sequential multiple-hues advanced |
| 839 |
"Purple-Yellow", "YlGnBu", "Greens", "BuGn", "Teal", "Peach", "Blues", "BuPu", "Purples", |
| 840 |
"Purp", "Burg", "Reds", "YlOrRd", "Sunset", "RdPu", "Inferno", "Rocket", "Mako", |
| 841 |
"Lajolla", "Turku", "Hawaii", "Batlow", |
| 842 |
# Base color maps (for shiny) |
| 843 |
"rainbow", "heat.colors", "topo.colors", "terrain.colors", "cm.colors", "bpy" |
| 844 |
) # end of variable definition for "take" |
| 845 |
|
| 846 |
# For qualitative: set h2 if h2 is NA (else the sliders will |
| 847 |
# be disabled on the graphical user interfaces). |
| 848 |
idx <- which(pals$type == "qual" & is.na(pals$H2)) |
| 849 |
pals$H2[idx] <- ifelse((pals$H1[idx] + 360) > 360, pals$H1[idx] - 360, pals$H1[idx] + 360) |
| 850 |
|
| 851 |
# Subset |
| 852 |
mtch <- match(take, rownames(pals)) |
| 853 |
pals <- pals[mtch,] |
| 854 |
#pals <- pals[which(rownames(pals) %in% take),] |
| 855 |
|
| 856 |
# Extending the type names for use in GUIs |
| 857 |
idx <- which(with(pals, (type == "dive" & !is.na(CMAX) | |
| 858 |
(type == "seqs" & (!is.na(CMAX) | !is.na(P2))) | |
| 859 |
(type == "seqm" & (!is.na(CMAX) | C1 > 100 | C2 > 100))))) |
| 860 |
if ( length(idx) > 0 ) pals$type[idx] <- sprintf("%s_advanced", pals$type[idx]) |
| 861 |
} |
| 862 |
|
| 863 |
return(pals) |
| 864 |
} |
| 865 |
|