hcl: HCL color specification

View source: R/parse_hcl.R

hclR Documentation

HCL color specification

Description

Create a vector of colors from hue, chromacity, and lightness.

Usage

hcl(h = 0, c = 0.65, l = 0.65, alpha = NULL, compat = FALSE, ...)

lch(l = 0.65, c = 0.65, h = 0)

Arguments

h

hue, either an angle around the color wheel, in [0,360] (angles outside of the range are rotated back to within [0, 360]: 380 = 20, -15 = 345, etc.), or a color (hex or named) from which the hue is extracted (by function hue).

c

chromacity, number in [0,~1]; 0 is grey, ~1 is full color.

l

lightness, number in [0,1]; 0 is darkest, 1 is brightest.

alpha

transparency, number in [0,1]; 0 means fully transparent, 1 means fully opaque. See function alpha for another way to change the transparency after the fact.

compat

whether to make the conventions compatible with the built-in function grDevices::hcl: c and l should be in [0,100], not [0,1]. However, the mechanism for converting from HCL space to sRGB are different in the two functions, and they therefore produce different colors.

...

ignored, for compatibility with the built-in hcl function.

Details

The first argument can also be a data.frame or a matrix. In that case, its columns are considered as the color components, taken in order, and the other color components arguments are ignored.

When separate arguments are used for the color components and are vectors, values in shorter arguments are recycled to match the length of the longest argument. If the lengths are not compatible, an error is output.

In HCL space, the perceived color (hue) is completely separated from the perceived intensity (chromacity) and lightness of the color. This means that colors of various hues but same chromacity and lightness appear as the exact same grey when converted to greyscale. This makes the HCL space particularly suitable to create good color palettes:

  • For qualitative palettes (discrete variables): varying h at constant c and l avoids drawing attention to certain hues, as would happen if the same was done in HSV or HSL space. Indeed, some hues are perceived as brighter (yellow, light green, etc.), others as duller/darker (blues, etc.).

  • For sequential palettes (continuous variables): varying l (or possibly c) for a constant h gives a sense of direction and avoid the many perceptual pitfalls typical of 'rainbow'-like scales.

Value

A vector of colors specified as hex codes

See Also

HCL-based color scales: hue_scale, chroma_scale, and light_scale.

parse_color for the general function to parse colors in various specifications (which this function calls internally) and convert_color to convert parsed colors to another model.

Other color specifications: cmyk(), css(), hex(), hsi(), hsl(), hsv(), lab(), parse_color(), rgb(), ryb(), temperature(), wavelength()

Examples

hcl()
hcl(330, 1, 0.6)
hcl(data.frame(c(330, 340), c(1,1), c(0.6, 0.4)))
hcl(matrix(c(330, 340, 1, 1, 0.6, 0.4), ncol=3))
hcl(c(330, 340), 1, 0.6)

# Compare "rainbow"-like scales in various color spaces
ramp <- seq(0, 1, length.out=10)
show_col(
  hcl(h=ramp*360),
  hsv(h=ramp*360, s=0.9),
  hsl(h=ramp*360, s=0.9)
)
# Hue, chromacity, and lightness scales
show_col(
  hcl(h=ramp*360),
  hcl(c=ramp),
  hcl(l=ramp)
)

# Nice color palettes
show_col(
  hcl(h=80+ramp*240, c=0.2+ramp*0.4, l=0.9-ramp*0.6),
  hcl(h=240-ramp*120, c=0.5, l=ramp*0.9),
  hcl(h=0+ramp*80, c=0.6-ramp*0.4, l=0.1+ramp*0.8),
  hcl(h=210+ramp*150, c=0.3, l=0.1+ramp*0.5)
)

jiho/chroma documentation built on Nov. 26, 2022, 2:39 a.m.