col2hsl function

convert R color to HSL color matrix

convert R color to HSL color matrix

col2hsl(x, ...)

Arguments

  • x: character vector with R compatible colors.
  • ...: additional arguments are ignored.

Returns

numeric matrix of H, S, L color values.

Details

This function takes an R color and converts to an HSL matrix, using the farver package farver::decode_colour()

the colorspace package, and RGB and polarLUV functions. It is also used to maintain alpha transparency, to enable interconversion via other color manipulation functions as well.

When model="hsl" this function uses farver::decode_colour()

and bypasses colorspace. In future the colorspace dependency will likely be removed in favor of using farver. In any event, model="hsl" is equivalent to using model="polarLUV" and fixup=TRUE, except that it should be much faster.

Examples

x <- c("#FF000044", "#FF0000", "firebrick"); names(x) <- x; showColors(x) xhsl <- col2hsl(x) xhsl xhex <- hsl2col(xhsl) showColors(list(x=x, xhex=xhex), groupCellnotes=FALSE) withr::with_par(list("mfrow"=c(4, 4), "mar"=c(0.2, 1, 4, 1)), { for (H in seq(from=0, to=360, length.out=17)[-17]) { S <- 75; Lseq <- seq(from=15, to=95, by=10); hsl_gradient <- hsl2col( H=H, S=85, L=Lseq); hcl_gradient <- hcl2col( H=H, C=85, L=Lseq); names(hsl_gradient) <- Lseq; names(hcl_gradient) <- Lseq; showColors(xaxt="n", list( hsl=hsl_gradient, hcl=hcl_gradient), main=paste0("Hue: ", round(H), "\nSat: ", S, "\nLum: (as labeled)"), groupCellnotes=FALSE) } })

See Also

Other jam color functions: alpha2col(), applyCLrange(), col2alpha(), col2hcl(), col2hsv(), color2gradient(), fixYellow(), fixYellowHue(), getColorRamp(), hcl2col(), hsl2col(), hsv2col(), isColor(), kable_coloring(), makeColorDarker(), rainbow2(), rgb2col(), setCLranges(), setTextContrastColor(), showColors(), unalpha(), warpRamp()

  • Maintainer: James M. Ward
  • License: MIT + file LICENSE
  • Last published: 2025-03-23