| 1 |
#' Desaturate Colors by Chroma Removal in HCL Space |
| 2 |
#' |
| 3 |
#' Transform a vector of given colors to the corresponding colors with chroma |
| 4 |
#' reduced (by a tunable amount) in HCL space. |
| 5 |
#' |
| 6 |
#' If input \code{col} is a vector given colors are first transformed to RGB |
| 7 |
#' (either using \code{\link[colorspace]{hex2RGB}} or |
| 8 |
#' \code{\link[grDevices]{col2rgb}}) and then to HCL |
| 9 |
#' (\code{\link[colorspace]{polarLUV}}). In HCL, chroma is reduced |
| 10 |
#' and then the color is transformed back to a hexadecimal |
| 11 |
#' string. |
| 12 |
#' |
| 13 |
#' If input \code{col} is a matrix with three rows named \code{R}, \code{G}, and |
| 14 |
#' \code{B} (top down) they are interpreted as Red-Green-Blue values within the |
| 15 |
#' range \code{[0-255]}. The desaturation takes place in the HCL space as well. |
| 16 |
#' Instead of an (s)RGB color vector a matrix of the same size as the input |
| 17 |
#' \code{col} with desaturated Red-Green-Blue values will be returned. |
| 18 |
#' This can be handy to avoid too many conversions. |
| 19 |
#' |
| 20 |
#' Similarly, \code{col} can be a formal \code{\link[colorspace]{color-class}} object, in which |
| 21 |
#' case the desaturated colors are returned as a formal object of the same class as the input. |
| 22 |
#' |
| 23 |
#' @param col vector of R colors. Can be any of the three kinds of R colors, |
| 24 |
#' i.e., either a color name (an element of \code{\link[grDevices]{colors}}), a hexadecimal (hex) |
| 25 |
#' string of the form \code{"#rrggbb"} or \code{"#rrggbbaa"} (see |
| 26 |
#' \code{\link[grDevices]{rgb}}), or an integer \code{i} meaning |
| 27 |
#' \code{palette()[i]}. Additionally, \code{col} can be |
| 28 |
#' a formal \code{\link[colorspace]{color-class}} object or a matrix with three named |
| 29 |
#' rows (or columns) containing R/G/B (0-255) values. |
| 30 |
#' @param amount numeric specifying the amount of desaturation where \code{1} |
| 31 |
#' corresponds to complete desaturation, \code{0} to no desaturation, and |
| 32 |
#' values in between to partial desaturation. |
| 33 |
#' @param ... additional arguments. If \code{severity} is specified it will |
| 34 |
#' overrule the input argument \code{amount} (for convenience). |
| 35 |
#' @return A color object as specified in the input \code{col} (hexadecimal string, RGB matrix, |
| 36 |
#' or formal color class) with desaturated colors. |
| 37 |
#' @seealso \code{\link[colorspace]{polarLUV}}, \code{\link[colorspace]{hex}}, \code{\link[colorspace]{lighten}} |
| 38 |
#' @references Zeileis A, Fisher JC, Hornik K, Ihaka R, McWhite CD, Murrell P, Stauffer R, Wilke CO (2020). |
| 39 |
#' \dQuote{colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.} |
| 40 |
#' \emph{Journal of Statistical Software}, \bold{96}(1), 1--49. \doi{10.18637/jss.v096.i01} |
| 41 |
#' @keywords color |
| 42 |
#' @examples |
| 43 |
#' ## rainbow of colors and their desaturated counterparts |
| 44 |
#' rainbow_hcl(12) |
| 45 |
#' desaturate(rainbow_hcl(12)) |
| 46 |
#' |
| 47 |
#' ## convenience demo function |
| 48 |
#' wheel <- function(col, radius = 1, ...) |
| 49 |
#' pie(rep(1, length(col)), col = col, radius = radius, ...) |
| 50 |
#' |
| 51 |
#' ## compare base and colorspace palettes |
| 52 |
#' ## (in color and desaturated) |
| 53 |
#' par(mar = rep(0, 4), mfrow = c(2, 2)) |
| 54 |
#' ## rainbow color wheel |
| 55 |
#' wheel(rainbow_hcl(12)) |
| 56 |
#' wheel(rainbow(12)) |
| 57 |
#' wheel(desaturate(rainbow_hcl(12))) |
| 58 |
#' wheel(desaturate(rainbow(12))) |
| 59 |
#' |
| 60 |
#' ## apply desaturation directly on wide RGB matrix (with R/G/B channels in rows) |
| 61 |
#' RGB <- diag(3) * 255 |
| 62 |
#' rownames(RGB) <- c("R", "G", "B") |
| 63 |
#' desaturate(RGB) |
| 64 |
#' @export desaturate |
| 65 |
#' @importFrom grDevices col2rgb |
| 66 |
#' @importFrom stats setNames |
| 67 |
|
| 68 |
desaturate <- function(col, amount = 1, ...) { |
| 69 |
|
| 70 |
## convenience: interpret 'severity' argument in dots as 'amount' |
| 71 |
args <- as.list(match.call(expand.dots = TRUE)) |
| 72 |
if (!is.null(args$severity)) amount <- args$severity |
| 73 |
|
| 74 |
## determine input type |
| 75 |
input_type <- if (inherits(col, "color")) { |
| 76 |
## S4 colorspace class |
| 77 |
"colorspace" |
| 78 |
} else if (is.matrix(col)) { |
| 79 |
## named RGB matrix (0-255) |
| 80 |
"matrix" |
| 81 |
} else if (is.character(col) && (all(substr(col, 1L, 1L) == "#") & all(nchar(col) %in% c(7L, 9L)))) { |
| 82 |
## all hex |
| 83 |
"hex" |
| 84 |
} else { |
| 85 |
## assume built-in colors |
| 86 |
"other" |
| 87 |
} |
| 88 |
|
| 89 |
## prismatic colors class is character + class attribute |
| 90 |
prismatic <- input_type == "hex" && inherits(col, "colors") |
| 91 |
|
| 92 |
## convert input to color object (typically sRGB) |
| 93 |
if (input_type == "colorspace") { |
| 94 |
|
| 95 |
color_class <- class(col) |
| 96 |
|
| 97 |
} else if (input_type == "matrix") { |
| 98 |
|
| 99 |
if (NROW(col) != 3L && NCOL(col) == 3L && all(toupper(colnames(col)) == c("R", "G", "B"))) { |
| 100 |
col <- t(col) |
| 101 |
transpose <- FALSE |
| 102 |
} else { |
| 103 |
transpose <- TRUE |
| 104 |
} |
| 105 |
stopifnot(all(toupper(rownames(col)) == c("R", "G", "B"))) |
| 106 |
col <- sRGB(t(col[1L:3L, ])/255) |
| 107 |
|
| 108 |
} else if (input_type == "hex") { |
| 109 |
|
| 110 |
# keep indices of NA colors |
| 111 |
NAidx <- which(is.na(col)) |
| 112 |
|
| 113 |
## extract alpha (if any) and convert RGB to colorspace::sRGB |
| 114 |
alpha <- substr(col, 8L, 9L) |
| 115 |
col <- hex2RGB(setNames(substr(col, 1L, 7L), names(col))) |
| 116 |
|
| 117 |
} else { |
| 118 |
|
| 119 |
# keep indices of NA colors |
| 120 |
NAidx <- which(is.na(col)) |
| 121 |
col <- grDevices::col2rgb(col, alpha = TRUE) |
| 122 |
## extract alpha values (if non-FF) |
| 123 |
alpha <- format(as.hexmode(col[4L, ]), width = 2L, upper.case = TRUE) |
| 124 |
alpha[alpha == "FF"] <- "" |
| 125 |
## retain only RGB |
| 126 |
col <- sRGB(t(col[1L:3L, ])/255) |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
## convert to HCL and decrease chroma |
| 131 |
col <- as(col, "polarLUV") |
| 132 |
col@coords[, 2L] <- (1 - amount) * col@coords[, 2L] |
| 133 |
|
| 134 |
## fix-up extreme luminance cases |
| 135 |
col@coords[col@coords[, 1L] <= 0 | col@coords[, 1L] >= 100, 2L:3L] <- 0 |
| 136 |
|
| 137 |
## convert back to input type |
| 138 |
if (input_type == "colorspace") { |
| 139 |
## convert back to original class |
| 140 |
col <- as(col, color_class) |
| 141 |
} else if (input_type == "matrix") { |
| 142 |
## convert back to matrix (either long or wide) |
| 143 |
RGB <- as(col, "sRGB")@coords * 255 |
| 144 |
col <- if (transpose) t(RGB) else RGB |
| 145 |
} else { |
| 146 |
## convert back to hex and add alpha again (if any) |
| 147 |
col <- hex(col) |
| 148 |
col[] <- paste0(col[], alpha) |
| 149 |
if (length(NAidx) > 0L) col[NAidx] <- NA |
| 150 |
if (prismatic) attr(col, "class") <- "colors" |
| 151 |
} |
| 152 |
return(col) |
| 153 |
} |