| 1 |
#' Compute Maximum Chroma for Given Hue and Luminance in HCL |
| 2 |
#' |
| 3 |
#' Compute approximately the maximum chroma possible for a given hue and |
| 4 |
#' luminance combination in the HCL color space. |
| 5 |
#' |
| 6 |
#' As the possible combinations of chroma and luminance depend on hue, it is |
| 7 |
#' not obvious which maximum chroma can be used for a given combination of hue |
| 8 |
#' and luminance prior to calling \code{\link{polarLUV}}. To avoid having to |
| 9 |
#' \code{fixup} the color upon conversion to RGB \code{\link{hex}} codes, the |
| 10 |
#' \code{max_chroma} function computes (approximately) the maximum chroma |
| 11 |
#' possible. The computations are based on interpolations of pre-computed |
| 12 |
#' maxima in \code{max_chroma_table}, containing the maximum chroma for a given |
| 13 |
#' hue-luminance combination (both in integers). Hence, the result may sometimes |
| 14 |
#' still be very slightly larger than the actual maximum which can be avoided |
| 15 |
#' by taking the \code{floor} of the approximate value. |
| 16 |
#' |
| 17 |
#' @param h hue value in the HCL color description, has to be in [0, 360]. |
| 18 |
#' @param l luminance value in the HCL color description, has to be in [0, 100]. |
| 19 |
#' @param floor logical. Should the chroma value be rounded down to the next |
| 20 |
#' lower integer? |
| 21 |
#' @return A numeric vector with the maximum chroma coordinates. |
| 22 |
#' @seealso \code{\link[colorspace]{polarLUV}}, \code{\link[colorspace]{hex}} |
| 23 |
#' @keywords color |
| 24 |
#' @examples |
| 25 |
#' max_chroma(h = 0:36 * 10, l = 50) |
| 26 |
#' max_chroma(h = 120, l = 0:10 * 10) |
| 27 |
#' @export |
| 28 |
max_chroma <- function(h, l, floor = FALSE) { |
| 29 |
## align h and l |
| 30 |
n <- max(c(length(h), length(l))) |
| 31 |
h <- rep_len(h, n) |
| 32 |
l <- rep_len(l, n) |
| 33 |
|
| 34 |
## create output vector (may contain NAs) |
| 35 |
c0 <- h * 0 + l * 0 |
| 36 |
na <- is.na(c0) |
| 37 |
h <- h[!na] |
| 38 |
l <- l[!na] |
| 39 |
|
| 40 |
## assure h in [0, 360] |
| 41 |
while(any(h < 0)) h[h < 0] <- h[h < 0] + 360 |
| 42 |
while(any(h >= 360)) h[h >= 360] <- h[h >= 360] - 360 |
| 43 |
|
| 44 |
## assure l in [0, 100] |
| 45 |
l <- pmin(100, pmax(0, l)) |
| 46 |
|
| 47 |
## obtain surrounding h/l coordinates |
| 48 |
hmin <- floor(h + 1e-8) |
| 49 |
hmax <- ceiling(h + 1e-8) |
| 50 |
lmin <- floor(l + 1e-8) |
| 51 |
lmax <- ceiling(l + 1e-8) |
| 52 |
|
| 53 |
## average |
| 54 |
c <- (hmax - h) * (lmax - l) * colorspace::max_chroma_table[paste(hmin, lmin, sep = "-")] + |
| 55 |
(hmax - h) * (l - lmin) * colorspace::max_chroma_table[paste(hmin, lmax, sep = "-")] + |
| 56 |
(h - hmin) * (lmax - l) * colorspace::max_chroma_table[paste(hmax, lmin, sep = "-")] + |
| 57 |
(h - hmin) * (l - lmin) * colorspace::max_chroma_table[paste(hmax, lmax, sep = "-")] |
| 58 |
|
| 59 |
## catch border cases |
| 60 |
c <- as.numeric(c) # pmin(c, 100) |
| 61 |
c[l <= 0 | l >= 100] <- 0 |
| 62 |
|
| 63 |
## take floor to be "on the safe side" |
| 64 |
if(floor) c <- floor(c) |
| 65 |
|
| 66 |
## insert non-NA values |
| 67 |
c0[!na] <- c |
| 68 |
return(c0) |
| 69 |
} |
| 70 |
|
| 71 |
#' @rdname max_chroma |
| 72 |
#' @format NULL |
| 73 |
"max_chroma_table" |