Debian Codegraph @

man/adjust_transparency.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/adjust_transparency.R
3 \name{adjust_transparency}
4 \alias{adjust_transparency}
5 \alias{extract_transparency}
6 \title{Adjust or Extract Transparency of Colors}
7 \usage{
8 adjust_transparency(col, alpha = TRUE)
9
10 extract_transparency(col, mode = "numeric", default = 1)
11 }
12 \arguments{
13 \item{col}{vector of R colors. Can be any of the three kinds of R colors,
14 i.e., either a color name (an element of
15 \code{\link[grDevices]{colors}}), a hexadecimal (hex) string of the form
16 \code{"#rrggbb"} or \code{"#rrggbbaa"} (see \code{\link[grDevices]{rgb}}), or
17 an integer \code{i} meaning \code{palette()[i]}. Additionally, \code{col} can be
18 a formal \code{\link[colorspace]{color-class}} object or a matrix with three
19 rows containing R/G/B (0-255) values.}
20
21 \item{alpha}{either a new alpha transparency value or logical (to add/remove alpha)
22 or \code{NULL}. See details.}
23
24 \item{mode}{character specifying the output mode for the alpha transparency, can be
25 \code{"numeric"}, \code{"integer"}, \code{"character"} or \code{"hexmode"}. See details.}
26
27 \item{default}{vector of length 1 specifying the default alpha transparency that should
28 be returned for colors that do not specify any explicitly (defaulting to fully opaque).
29 Can either be numeric, integer, character, or hexmode.}
30 }
31 \value{
32 For \code{adjust_transparency} character vector with hexadecimal color strings with alpha transparency
33 corresponding to \code{alpha} argument. For \code{extract_transparency} a vector of
34 alpha transparency values with the indicated \code{mode}.
35 }
36 \description{
37 Adjust (i.e., add, remove, or modify) or extract alpha transparency of a vector of colors.
38 }
39 \details{
40 Alpha transparency is useful for making colors semi-transparent, e.g., for
41 overlaying different elements in graphics. An alpha value of 0 (or 00 in hex strings)
42 corresponds to fully transparent and an alpha value of 1 (or FF in hex strings)
43 corresponds to fully opaque. If a color hex string in R does not provide an explicit
44 alpha transparency, the color is assumed to be fully opaque.
45
46 The \code{adjust_transparency} function can be used to adjust the alpha transparency
47 of a set of colors. It always returns a hex color specification. This hex color
48 can have the alpha transparency added/removed/modified depending on the
49 specification of \code{alpha}: \itemize{
50 \item \code{alpha = NULL}: Returns a hex vector with alpha transparency only if needed.
51 Thus, it keeps the alpha transparency for the colors (if any) but only if
52 different from opaque.
53 \item \code{alpha = TRUE}: Returns a hex vector with alpha transparency
54 for all colors, using opaque (FF) as the default if missing.
55 \item \code{alpha = FALSE}: Returns a hex vector without alpha transparency for
56 all colors (even if the original colors had non-opaque alpha).
57 \item \code{alpha} numeric: Returns a hex vector with alpha transparency for
58 all colors set to the \code{alpha} argument (recycled if necessary).
59 }
60
61 The \code{extract_transparency} function can be used to extract the alpha transparency
62 from a set of colors. It allows to specify the \code{default} value - that should be used
63 for colors without an explicit alpha transparency (defaulting to fully opaque) - and
64 \code{mode} of the return value. This can either be numeric (in [0, 1]), integer
65 (0L, 1L, \dots, 255L), character (\dQuote{00}, \dQuote{01}, \dots, \dQuote{FF}),
66 or an object of class \code{\link[base]{hexmode}} (internally represented as integer
67 with printing as character). The \code{default} can use any of these modes as well
68 (independent of the output \code{mode}) or be \code{NA}.
69 }
70 \examples{
71 ## modify transparency of a color (in different formats)
72 adjust_transparency("black", alpha = c(0, 0.5, 1)) ## name
73 adjust_transparency("#000000", alpha = c(0, 0.5, 1)) ## hex string
74 adjust_transparency(1, alpha = c(0, 0.5, 1)) ## palette() integer
75
76 ## three shades of gray (in different formats:
77 ## name/opaque, hex/opaque, hex/semi-transparent)
78 x <- c("gray", "#BEBEBE", "#BEBEBE80")
79
80 ## adjust transparency
81 adjust_transparency(x, alpha = NULL) ## only if necessary
82 adjust_transparency(x, alpha = TRUE) ## add
83 adjust_transparency(x, alpha = FALSE) ## remove
84 adjust_transparency(x, alpha = 0.8) ## modify
85
86 ## extract transparency in different formats
87 extract_transparency(x, mode = "numeric") ## default
88 extract_transparency(x, mode = "integer")
89 extract_transparency(x, mode = "character")
90 extract_transparency(x, mode = "hexmode")
91
92 ## extract transparency with different default values
93 extract_transparency(x, default = NA)
94 extract_transparency(x, default = 0.5)
95 extract_transparency(x, default = 128L)
96 extract_transparency(x, default = "80", mode = "integer")
97 }
98 \references{
99 Zeileis A, Fisher JC, Hornik K, Ihaka R, McWhite CD, Murrell P, Stauffer R, Wilke CO (2020).
100 \dQuote{colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.}
101 \emph{Journal of Statistical Software}, \bold{96}(1), 1--49. \doi{10.18637/jss.v096.i01}
102 }
103 \seealso{
104 \code{\link[grDevices]{rgb}}, \code{\link[colorspace]{desaturate}}, \code{\link[colorspace]{lighten}}
105 }
106 \keyword{color}