Debian Codegraph @

R/rainbow_hcl.R

raw
1 #' HCL (and HSV) Color Palettes Corresponding to Base R Palettes
2 #'
3 #' Color palettes based on the HCL (and HSV) color space to replace
4 #' base R palettes.
5 #'
6 #' Based on the general qualitative, sequential, and diverging \code{\link{hcl_palettes}}
7 #' within the colorspace package, convenience functions are provided as
8 #' alternatives to standard base R palettes (which are highly saturated
9 #' and too flashy).
10 #'
11 #' \code{rainbow_hcl} computes a rainbow of colors via \code{\link{qualitative_hcl}}
12 #' defined by different hues given a single value of each chroma and luminance.
13 #' It corresponds to \code{\link{rainbow}} which computes a rainbow in HSV
14 #' space.
15 #'
16 #' \code{heat_hcl} is an implementation of \code{\link[grDevices]{heat.colors}} in
17 #' HCL space based on a call to \code{\link{sequential_hcl}}. Similarly,
18 #' \code{terrain_hcl} palette also calls \code{sequential_hcl} with different
19 #' parameters, providing colors similar in spirit to \code{terrain.colors}
20 #' in HCL space.
21 #'
22 #' \code{diverging_hsv} (and equivalently its alias \code{diverge_hsv})
23 #' provides an HSV-based version of \code{\link{diverging_hcl}}. Its purpose
24 #' is mainly didactic to show that HSV-based diverging palettes are less
25 #' appealing, more difficult to read and more flashy than HCL-based diverging
26 #' palettes. \code{diverging_hsv} is similar to \code{\link[grDevices]{cm.colors}}.
27 #'
28 #' @param n the number of colors (\eqn{\ge 1}{>= 1}) to be in the palette.
29 #' @param c,c. chroma value in the HCL color description.
30 #' @param l luminance value in the HCL color description.
31 #' @param start the hue at which the rainbow begins.
32 #' @param end the hue at which the rainbow ends.
33 #' @param h hue value in the HCL or HSV color description, has to be in [0, 360]
34 #' for HCL and in [0, 1] for HSV colors.
35 #' @param s saturation value in the HSV color description.
36 #' @param v value value in the HSV color description.
37 #' @param power control parameter determining how chroma and luminance should
38 #' be increased (1 = linear, 2 = quadratic, etc.).
39 #' @param gamma Deprecated.
40 #' @param fixup logical. Should the color be corrected to a valid RGB value
41 #' before correction?
42 #' @param alpha numeric vector of values in the range \code{[0, 1]} for alpha
43 #' transparency channel (0 means transparent and 1 means opaque).
44 #' @param \dots Other arguments passed to \code{\link{hex}}.
45 #' @return A character vector with (s)RGB codings of the colors in the palette.
46 #' @seealso \code{\link[colorspace]{polarLUV}}, \code{\link[colorspace]{HSV}},
47 #' \code{\link[colorspace]{hex}}
48 #' @references Zeileis A, Hornik K, Murrell P (2009). Escaping RGBland:
49 #' Selecting Colors for Statistical Graphics. \emph{Computational Statistics &
50 #' Data Analysis}, \bold{53}, 3259--3270.
51 #' \doi{10.1016/j.csda.2008.11.033}
52 #' Preprint available from
53 #' \url{https://www.zeileis.org/papers/Zeileis+Hornik+Murrell-2009.pdf}.
54 #'
55 #' Stauffer R, Mayr GJ, Dabernig M, Zeileis A (2015). Somewhere over the
56 #' Rainbow: How to Make Effective Use of Colors in Meteorological
57 #' Visualizations. \emph{Bulletin of the American Meteorological Society},
58 #' \bold{96}(2), 203--216.
59 #' \doi{10.1175/BAMS-D-13-00155.1}
60 #' @keywords color
61 #' @examples
62 #' ## convenience demo function
63 #' wheel <- function(col, radius = 1, ...)
64 #' pie(rep(1, length(col)), col = col, radius = radius, ...)
65 #'
66 #' ## compare base and colorspace palettes
67 #' ## (in color and desaturated)
68 #' par(mar = rep(0, 4), mfrow = c(2, 2))
69 #' ## rainbow color wheel
70 #' wheel(rainbow_hcl(12))
71 #' wheel(rainbow(12))
72 #' wheel(desaturate(rainbow_hcl(12)))
73 #' wheel(desaturate(rainbow(12)))
74 #'
75 #' ## diverging red-blue colors
76 #' swatchplot(
77 #' diverging_hsv(7),
78 #' desaturate(diverging_hsv(7)),
79 #' diverging_hcl(7, c = 100, l = c(50, 90)),
80 #' desaturate(diverging_hcl(7, c = 100, l = c(50, 90))),
81 #' nrow = 2
82 #' )
83 #'
84 #' ## diverging cyan-magenta colors
85 #' swatchplot(
86 #' cm.colors(7),
87 #' desaturate(cm.colors(7)),
88 #' diverging_hcl(7, "Cyan-Magenta"), ## or, similarly: Tropic
89 #' desaturate(diverging_hcl(7, "Cyan-Magenta")),
90 #' nrow = 2
91 #' )
92 #'
93 #' ## heat colors
94 #' swatchplot(
95 #' heat.colors(12),
96 #' desaturate(heat.colors(12)),
97 #' heat_hcl(12),
98 #' desaturate(heat_hcl(12)),
99 #' nrow = 2
100 #' )
101 #'
102 #' ## terrain colors
103 #' swatchplot(
104 #' terrain.colors(12),
105 #' desaturate(terrain.colors(12)),
106 #' terrain_hcl(12),
107 #' desaturate(terrain_hcl(12)),
108 #' nrow = 2
109 #' )
110 #' @rdname rainbow_hcl
111
112 #' @export
113 rainbow_hcl <- function(n, c = 50, l = 70, start = 0, end = 360 * (n - 1)/n,
114 gamma = NULL, fixup = TRUE, alpha = 1, ...)
115 {
116 if (!is.null(gamma)) warning("'gamma' is deprecated and has no effect")
117 if(n < 1L) return(character(0L))
118 substr(qualitative_hcl(n, h = c(start, end), c = c, l = l, fixup = fixup, alpha = alpha, ...),
119 1L, if(missing(alpha)) 7L else 9L)
120 }
121
122 #' @rdname rainbow_hcl
123 #' @export
124 heat_hcl <- function(n, h = c(0, 90), c. = c(100, 30), l = c(50, 90),
125 power = c(1/5, 1), gamma = NULL, fixup = TRUE, alpha = 1, ...)
126 {
127 if (!is.null(gamma)) warning("'gamma' is deprecated and has no effect")
128 if(n < 1L) return(character(0L))
129 substr(sequential_hcl(n, h = h, c = c., l = l, power = power, fixup = fixup, alpha = alpha, ...),
130 1L, if(missing(alpha)) 7L else 9L)
131 }
132
133 #' @rdname rainbow_hcl
134 #' @export
135 terrain_hcl <- function(n, h = c(130, 0), c. = c(80, 0), l = c(60, 95),
136 power = c(1/10, 1), gamma = NULL, fixup = TRUE, alpha = 1, ...)
137 {
138 if (!is.null(gamma)) warning("'gamma' is deprecated and has no effect")
139 if(n < 1L) return(character(0L))
140 substr(sequential_hcl(n, h = h, c = c., l = l, power = power, fixup = fixup, alpha = alpha, ...),
141 1L, if(missing(alpha)) 7L else 9L)
142 }
143
144 #' @rdname rainbow_hcl
145 #' @export
146 diverging_hsv <- function(n, h = c(240, 0), s = 1, v = 1, power = 1,
147 gamma = NULL, fixup = TRUE, alpha = 1, ...)
148 {
149 if (!is.null(gamma))
150 warning("'gamma' is deprecated and has no effect")
151 if(n < 1L) return(character(0L))
152 h <- rep(h, length.out = 2L)
153 s <- s[1L]
154 v <- v[1L]
155 power <- power[1L]
156 rval <- seq(-s, s, length.out = n)
157 rval <- hex(as(HSV(H = ifelse(rval > 0, h[2L], h[1L]),
158 S = abs(rval)^power, V = v, ...), "RGB"),
159 fixup = fixup, ...)
160
161 if(!missing(alpha)) {
162 alpha <- pmax(pmin(alpha, 1), 0)
163 alpha <- format(as.hexmode(round(alpha * 255 + 0.0001)),
164 width = 2L, upper.case = TRUE)
165 rval <- paste(rval, alpha, sep = "")
166 }
167
168 return(rval)
169 }
170
171 #' @rdname rainbow_hcl
172 #' @usage NULL
173 #' @export
174 diverge_hsv <- diverging_hsv
175
176
177 ## Analogous to sp::bpy.colors
178 ## (currently not exported, just for hclwizard)
179 bpy <- function(n) {
180 i <- 0.05 + 0.9 * 0:(n-1)/(n-1)
181 r <- -0.78125 + 3.125 * i
182 g <- -0.84 + 2 * i
183 b <- 1 + as.numeric(i > 0.3) + as.numeric(i > 0.92)
184 b <- c(0, 1.84, -11.5)[b] + c(4, -2, 12.5)[b] * i
185 hex(sRGB(
186 pmax(0, pmin(1, r)),
187 pmax(0, pmin(1, g)),
188 pmax(0, pmin(1, b))
189 ))
190 }