| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/lighten.R |
| 3 |
\name{lighten} |
| 4 |
\alias{lighten} |
| 5 |
\alias{darken} |
| 6 |
\title{Algorithmically Lighten or Darken Colors} |
| 7 |
\usage{ |
| 8 |
lighten( |
| 9 |
col, |
| 10 |
amount = 0.1, |
| 11 |
method = c("relative", "absolute"), |
| 12 |
space = c("HCL", "HLS", "combined"), |
| 13 |
fixup = TRUE |
| 14 |
) |
| 15 |
|
| 16 |
darken(col, amount = 0.1, space = "combined", ...) |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{col}{vector of any of the three kind of R colors, i.e., either a color |
| 20 |
name (an element of \code{\link[grDevices]{colors}}), a hexadecimal string |
| 21 |
of the form \code{"#rrggbb"} or \code{"#rrggbbaa"} (see |
| 22 |
\code{\link[grDevices]{rgb}}), or an integer \code{i} meaning |
| 23 |
\code{palette()[i]}.} |
| 24 |
|
| 25 |
\item{amount}{numeric specifying the amount of lightening. This is applied either |
| 26 |
multiplicatively or additively to the luminance value, depending on the |
| 27 |
setting of \code{method} (either relative or absolute). Negative numbers |
| 28 |
cause darkening.} |
| 29 |
|
| 30 |
\item{method}{character string specifying the adjustment method. Can be either \code{"relative"} or \code{"absolute"}.} |
| 31 |
|
| 32 |
\item{space}{character string specifying the color space in which adjustment happens. Can be either \code{"HLS"} or \code{"HCL"}.} |
| 33 |
|
| 34 |
\item{fixup}{logical If set to \code{TRUE}, colors that fall outside of the RGB color gamut are slightly modified |
| 35 |
by translating individual primary values so they lie between 0 and 255. If set to \code{FALSE}, out-of-gamut colors |
| 36 |
are replaced by \code{NA}.} |
| 37 |
|
| 38 |
\item{...}{Other parameters handed to the function \code{lighten()}.} |
| 39 |
} |
| 40 |
\value{ |
| 41 |
A character vector with (s)RGB codings of the colors in the palette. |
| 42 |
} |
| 43 |
\description{ |
| 44 |
The functions \code{lighten} and \code{darken} take a vector of R colors and adjust the colors such that |
| 45 |
they appear lightened or darkened, respectively. |
| 46 |
} |
| 47 |
\details{ |
| 48 |
The color adjustment can be calculated in three different color spaces. \enumerate{ |
| 49 |
\item If \code{space = "HCL"}, the colors are transformed to HCL, (\code{\link[colorspace]{polarLUV}}), |
| 50 |
the luminance component L is adjusted, and then the colors are transformed back to a hexadecimal |
| 51 |
RGB string. |
| 52 |
\item If \code{space = "HLS"}, the colors are transformed to HLS, the lightness component L is adjusted, |
| 53 |
and then the color is transformed back to a hexadecimal RGB string. |
| 54 |
\item If \code{space = "combined"}, the colors are first adjusted in both the HCL and HLS spaces. Then, |
| 55 |
the adjusted HLS colors are converted into HCL, and then the chroma components of the adjusted HLS |
| 56 |
colors are copied to the adjusted HCL colors. Thus, in effect, the combined model adjusts luminance |
| 57 |
in HCL space but chroma in HLS space. |
| 58 |
} |
| 59 |
We have found that typically \code{space = "HCL"} performs best for lightening colors and \code{space = "combined"} |
| 60 |
performs best for darkening colors, and these are the default settings for \code{lighten} and \code{darken}, |
| 61 |
respectively. |
| 62 |
|
| 63 |
Regardless of the chosen color space, the adjustment of the L component can occur by two methods, relative |
| 64 |
(the default) and absolute. Under the absolute method, the adjustment is \code{L +/- 100 * amount} when |
| 65 |
lightening/darkening colors. Under the relative method, the adjustment is \code{100 - (100 - L) * (1 - amount)} when |
| 66 |
lightening colors and \code{L * (1 - amount)} when darkening colors. |
| 67 |
|
| 68 |
Programmatically lightening and darkening colors can yield unexpected results (see examples). In HCL space, |
| 69 |
colors can become either too gray or overly colorful. By contrast, in HLS space it can happen that the |
| 70 |
overall amount of lightening or darkening appears to be non-uniform among a group of colors that are |
| 71 |
lightened or darkened jointly, and again, colors can become either too gray or overly colorful. We |
| 72 |
recommend to try different color spaces if the default space for the chosen function (\code{lighten} |
| 73 |
or \code{darken}) does not look right in a specific application. |
| 74 |
} |
| 75 |
\examples{ |
| 76 |
# lighten dark colors, example 1 |
| 77 |
cl <- qualitative_hcl(5) |
| 78 |
swatchplot(list( |
| 79 |
HCL = rbind("0\%" = cl, |
| 80 |
"15\%" = lighten(cl, 0.15), |
| 81 |
"30\%" = lighten(cl, 0.3)), |
| 82 |
HLS = rbind("0\%" = cl, |
| 83 |
"15\%" = lighten(cl, 0.15, space = "HLS"), |
| 84 |
"30\%" = lighten(cl, 0.3, space = "HLS")), |
| 85 |
combined = rbind("0\%" = cl, |
| 86 |
"15\%" = lighten(cl, 0.15, space = "combined"), |
| 87 |
"30\%" = lighten(cl, 0.3, space = "combined"))), |
| 88 |
nrow = 4, line = 2.5 |
| 89 |
) |
| 90 |
|
| 91 |
# lighten dark colors, example 2 |
| 92 |
cl <- c("#61A9D9", "#ADD668", "#E6D152", "#CE6BAF", "#797CBA") |
| 93 |
swatchplot(list( |
| 94 |
HCL = rbind("0\%" = cl, |
| 95 |
"15\%" = lighten(cl, 0.15), |
| 96 |
"30\%" = lighten(cl, 0.3)), |
| 97 |
HLS = rbind("0\%" = cl, |
| 98 |
"15\%" = lighten(cl, 0.15, space = "HLS"), |
| 99 |
"30\%" = lighten(cl, 0.3, space = "HLS")), |
| 100 |
combined = rbind("0\%" = cl, |
| 101 |
"15\%" = lighten(cl, 0.15, space = "combined"), |
| 102 |
"30\%" = lighten(cl, 0.3, space = "combined"))), |
| 103 |
nrow = 4, line = 2.5 |
| 104 |
) |
| 105 |
|
| 106 |
# darken light colors, example 1 |
| 107 |
cl <- qualitative_hcl(5, "Pastel 1") |
| 108 |
swatchplot(list( |
| 109 |
combined = rbind("0\%" = cl, |
| 110 |
"15\%" = darken(cl, 0.15), |
| 111 |
"30\%" = darken(cl, 0.3)), |
| 112 |
HCL = rbind("0\%" = cl, |
| 113 |
"15\%" = darken(cl, 0.15, space = "HCL"), |
| 114 |
"30\%" = darken(cl, 0.3, space = "HCL")), |
| 115 |
HLS = rbind("0\%" = cl, |
| 116 |
"15\%" = darken(cl, 0.15, space = "HLS"), |
| 117 |
"30\%" = darken(cl, 0.3, space = "HLS"))), |
| 118 |
nrow = 4, line = 2.5 |
| 119 |
) |
| 120 |
|
| 121 |
# darken light colors, example 2 |
| 122 |
cl <- c("#CDE4F3","#E7F3D3","#F7F0C7","#EFCFE5","#D0D1E7") |
| 123 |
swatchplot(list( |
| 124 |
combined = rbind("0\%" = cl, |
| 125 |
"15\%" = darken(cl, 0.15), |
| 126 |
"30\%" = darken(cl, 0.3)), |
| 127 |
HCL = rbind("0\%" = cl, |
| 128 |
"15\%" = darken(cl, 0.15, space = "HCL"), |
| 129 |
"30\%" = darken(cl, 0.3, space = "HCL")), |
| 130 |
HLS = rbind("0\%" = cl, |
| 131 |
"15\%" = darken(cl, 0.15, space = "HLS"), |
| 132 |
"30\%" = darken(cl, 0.3, space = "HLS"))), |
| 133 |
nrow = 4, line = 2.5 |
| 134 |
) |
| 135 |
} |
| 136 |
\references{ |
| 137 |
Zeileis A, Fisher JC, Hornik K, Ihaka R, McWhite CD, Murrell P, Stauffer R, Wilke CO (2020). |
| 138 |
\dQuote{colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.} |
| 139 |
\emph{Journal of Statistical Software}, \bold{96}(1), 1--49. \doi{10.18637/jss.v096.i01} |
| 140 |
} |
| 141 |
\seealso{ |
| 142 |
\code{\link[colorspace]{polarLUV}}, \code{\link[colorspace]{hex}}, \code{\link[colorspace]{desaturate}} |
| 143 |
} |
| 144 |
\keyword{color} |